Servlet handling POST request as GET
I wrote a servlet to handle both POST and GET requests, based on the example given here. I have the following:
A html with the following form:
form method="POST" action="servlet/RequestType"
and input:
input type="submit" value="POST"
The following doGet
and doPost
methods:
public void do开发者_如何学PythonGet(HttpServletRequest req, HttpServletResponse rsp) throws ServletException, IOException {
rsp.setContentType("text/html");
PrintWriter out = rsp.getWriter();
String requestType = req.getMethod();
out.println("<html>");
out.println("<head><title> Request Type: " + requestType
+ " </title></head>");
out.println("<body>");
out.println("<p>This page is the result of a " + requestType
+ " request.</p>");
out.println("</body></html>");
}
public void doPost(HttpServletRequest req, HttpServletResponse rsp) throws ServletException, IOException {
doGet(req, rsp);
}
The output should be:
This page is the result of a POST request.
But I'm getting:
This page is the result of a GET request.
Does anyone know why this might be happening?
I know, that isn't solve, but try to check request method in doPost() before call doGet(). Use System.out.println() - you will see what will be written. If nothing will be written it will mean, that your request is always GET.
You need to press the submit button of the form to send a POST request.
That said, this tutorial from 2001 gives me a lot of itch. I'd suggest to go read a more recent/decent one.
精彩评论