关于servlet,我是一个新手,我希望有人能帮上我一点。
我需要编写一个简单的方法,调用println,并根据所使用的doPost
或doGet
提供不同的信息,例如:
if (doPost was used) {
out.println("The doPost method was used);
}
else if (doGet was used) {
out.println("The doGet method was used);
}
else
{
out.println("Neither doPost nor doGet was used");
}
有人可以帮我吗? :)
提前致谢!
最佳答案
一个简单的servlet的示例,它将执行您想要的类似操作:
public class ServletDemo1 extends HttpServlet{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException{
// do something with GET petitions
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException{
// do something with POST petitions
}
}
这段代码会根据GET或POST请求的类型进行不同的操作。或者,您可以使用服务方法:
protected void service(HttpServletRequest req, HttpServletResponse resp) {...}
并根据请求方法值(
request.getMethod()
)进行过滤。您可以管理的不仅仅是GET或POST(例如PUT,DELETE ...)