0

I am building a simple web app and attempting to create a login page. The page consists of a JSP with a form which loads a Servlet.

I have got the form working using the GET method:

JSP looks like this:

<form method="get" action="Login">
Email:<input name="email"/>
Password:<input name="password"/>
<input type="Submit" value="Log in"/>

And in the Servlet:

@WebServlet(name = "Login", urlPatterns = {"/Login"})
public class Login extends HttpServlet {

/**
* Processes requests for both HTTP
* <code>GET</code> and
* <code>POST</code> methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");

//Assign variables from the request parameters
String loginFormEmail = request.getParameter("email");
String loginFormPassword = request.getParameter("password");

This code works but it includes the username and password in the URL string, so it's obviously not good practice. I have attempted to do this using POST instead but I've been getting an error. (HTTP Status 405 - HTTP method POST is not supported by this URL)

I need to know how to send parameters from the JSP to the Servlet using POST. I think this may involve using RequestDispatcher object, but all the tutorials I've found explain using RequestDispatcher to send data from the Servlet to the JSP, not the other way around. Can you/should you use Request Dispatcher to send POST data from the JSP to the Servlet? And how to you access these parameters from the Servlet? (Is there an equivalent of request.getParameter() for POST?)

I understand that using POST still won't be secure, but it is a lot better practice than including the password in the query string, and I will think about security later.

Apologies for the basic question, I have found lots of tutorials online but none of them seem to answer this specific question. Thank you.

Jon
  • 379
  • 1
  • 7
  • 18
  • What error do you get when using POST? –  May 21 '13 at 12:58
  • See answer for this question: http://stackoverflow.com/questions/2349633/servlets-doget-and-dopost – Eugene Loy May 21 '13 at 12:58
  • The error I get when using POST is HTTP Status 405 - HTTP method POST is not supported by this URL – Jon May 21 '13 at 13:05
  • 1
    Please put a little bit more research effort. You could just have copypasted *" HTTP Status 405 - HTTP method POST is not supported by this URL"* into the Google search input field or even just here on Stack Overflow (on the right top). – BalusC May 21 '13 at 16:25

5 Answers5

4

Try

<form method="POST" action="Login>

Note: method instead of type for specifying GET/POST.

But it's not really any more "secure" than using GET. They are still available in clear text in the post body. If you want it to be secure, make sure you use HTTPS.

Edit

You have edited your question now, and it appears that you are using method, not type. So if you still have errors after changing it to POST, specify what error you are getting.

Edit2

You specify that you are getting a HTTP method POST is not supported by this URL error. This means that your servlet does not accept the POST method. Which most likely means that you are inheriting some base servlet that only accepts GET. Seeing all of the code for the servlet would be helpful.

NilsH
  • 13,705
  • 4
  • 41
  • 59
  • Thank you, I have corrected this but still getting the error when using POST. The error I get on submitting the form is: HTTP Status 405 - HTTP method POST is not supported by this URL – Jon May 21 '13 at 13:03
  • You're probably not showing all of your code. Are you inheriting another servlet? – NilsH May 21 '13 at 13:06
  • I am using the NetBeans Servlet template which extends HttpServlet. I have updated the question to show the full code of the Servlet. Thank you. – Jon May 21 '13 at 13:16
  • That can't be all of the code. There is no `processRequest` in the `HttpServlet` class, so it has to be invoked from somewhere else. I'm pretty certain that you have a `doGet` method in there somewhere. Make sure your editor doesn't have anything collapsed. – NilsH May 21 '13 at 13:26
  • You're right, there are HttpServlet methods collapsed in an "editor fold" at the bottom of the Servlet template. I think I had a missing } somewhere which was causing the problem. Sorry to waste your time. – Jon May 21 '13 at 14:01
0
<form type="get" action="Login" method="POST">
 Email:<input name="email"/>
 Password:<input name="password"/>
<input type="Submit" value="Log in"/>

I suggest you instead of processRequest(), use doPost() method.

KhAn SaAb
  • 5,248
  • 5
  • 31
  • 52
0

Use method="POST" attribute in your element

Alex
  • 210
  • 2
  • 13
  • When I do this I get this error when submitting the form: HTTP Status 405 - HTTP method POST is not supported by this URL – Jon May 21 '13 at 13:08
0

Override the HttpServlet#doPost() method in your Login class

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException {
    String loginFormEmail = request.getParameter("email");
    String loginFormPassword = request.getParameter("password");
    // do something to produce a response
}

This might require you to change the service() method that might be overridden to call your processRequest() method regardless of the HTTP method. This depends on the rest of your Login class implementation which you haven't shown.

Then change your <form> to make a POST request.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
0

Try to override the HttpServlet methods doPost() and doGet():

public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException,IOException {
    processRequest(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException,IOException {
    processRequest(request,response);
}
Mayday
  • 4,680
  • 5
  • 24
  • 58