You should use doGet()
when you want to intercept on HTTP GET
requests. You should use doPost()
when you want to intercept on HTTP POST
requests. That's all. Do not port the one to the other or vice versa. This makes no utter sense.
Usually, the servlet doGet()
is used to preprocess a request. I.e. doing some business stuff before displaying the JSP page, such as gathering data for display in a table. The servlet doPost()
is used to postprocess a request. I.e. gathering data from a submitted HTML form and doing some business stuff with it (conversion, validation, saving in DB, etcetera). Finally usually the result is displayed in a JSP page.
Clicking a link, clicking a bookmark, entering raw URL in browser address bar, etcetera will all fire a HTTP GET
request. If a Servlet is listening on the URL in question, then its doGet()
method will be called. The HTTP POST
requests are usually only fired by a <form>
whose method
attribute is set to post
:
<form action="login" method="post">
<input type="text" name="username">
<input type="password" name="password">
<input type="submit" value="login">
<span class="error">${error}</span>
</form>
...which can be used in combination with this piece of Servlet:
public class LoginServlet extends HttpServlet {
private UserDAO userDAO;
public void init() {
this.userDAO = Config.getInstance(getServletContext()).getDAOFactory().getUserDAO();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
User user = userDAO.find(username, password);
if (user != null) {
request.getSession().setAttribute("user", user);
response.sendRedirect("home");
} else {
request.setAttribute("error", "Unknown user, please try again");
request.getRequestDispatcher("/login.jsp").forward(request, response);
}
}
}
You see, if the User
is found in DB (i.e. username and password are valid), then the User
will be put in session scope (i.e. "logged in") and the servlet will redirect to some main page (this example goes to http://example.com/contextname/home
), else it will set an error message and forward the request back to the same JSP page so that the message get displayed by ${error}
.
You can if necessary also "hide" the login.jsp
in /WEB-INF/login.jsp
so that the users can only access it by the servlet. This keeps the URL clean http://example.com/contextname/login
. All you need to do is to add a doGet()
to the servlet like this:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.getRequestDispatcher("/WEB-INF/login.jsp").forward(request, response);
}
(and update the same line in doPost()
accordingly). And yes, this servlet example is to be mapped on an url-pattern
of /login
.
That said, I am not sure if it is just playing around and shooting in the dark, but the code which you posted doesn't look good (such as using compareTo()
instead of equals()
and digging in the parameternames instead of just using getParameter()
and the id
and password
seems to be declared as servlet instance variables --which is NOT threadsafe). So I would strongly recommend to learn a bit more about basic Java SE API using the Sun tutorials (check the chapter "Trails Covering the Basics") and how to use JSP/Servlets the right way using those tutorials.
Update: as per the update of your question (which is pretty major, you should not remove parts of your original question, this would make the answers worthless .. rather add the information in a new block) , it turns out that you're unnecessarily setting form's encoding type to multipart/form-data
. This will send the request parameters in a different composition than the (default) application/x-www-form-urlencoded
which sends the request parameters as a query string (e.g. name1=value1&name2=value2&name3=value3
). You only need multipart/form-data
whenever you have a <input type="file">
element in the form to upload files which may be non-character data (binary data). This is not the case in your case, so just remove it and it will work as expected. If you ever need to upload files, then you'll have to set the encoding type so and parse the request body yourself. Usually you use the Apache Commons FileUpload there for, but if you're already on fresh new Servlet 3.0 API, then you can just use builtin facilities starting with HttpServletRequest#getParts()
.