0

I'm writing Blog in java servlet/jsp. And now I have problem with registration.

That's what I have in RegisterServlet

@WebServlet("/register")
public class RegisterServlet extends HttpServlet {
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, java.io.IOException {

}

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws
            ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String login = request.getParameter("login");
        String pass = request.getParameter("pass");
        RegistrationHelper registrationHelper = new RegistrationHelper();
        registrationHelper.setLogin(login);

        boolean isError = false;
        if (login == null || login.trim().equals("")) {
            String loginMessage = "It's empy";
            registrationHelper.setLoginMessage(loginMessage);
            isError = true;
        }
        if (pass == null || pass.trim().equals("")) {
            String passMessage = "It's empty.";
            registrationHelper.setPassMessage(passMessage);
            isError = true;
        }
        if(!isError) {



            UsersDAO dao = (UsersDAO) request.getServletContext().getAttribute("usersDAO");
            if(dao.checkIfLoginExists(login)) {
              registrationHelper.setLoginMessage("User with this name is already registered");
            } else {
                dao.createUser(new User(login, Encryption.md5(pass)));
                registrationHelper.setSuccess("Succesfully registered");
            }
        }
        request.setAttribute("registrationHelper", registrationHelper);
        RequestDispatcher dis = request.getRequestDispatcher("/");
        dis.forward(request, response);
    }
}

And my register.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title></title>
</head>
<body>

<form action="./register" method="POST">
  Podaj login: <input name="login" type="text" value="${registrationHelper.login}"  size="20"/> ${registrationHelper.loginMessage} <br/>
  Podaj hasło: <input name="pass" type="password" size="20"/>${registrationHelper.passMessage}<br/>
  <input type="submit" value="Register"/>
  <br>${registrationHelper.success}
</form>


</body>
</html>

So , if I get to registration page using this link

<a href="register.jsp">Reg</a><br/>

It's all good and I have my registration form because doPost is used.

But , if I try

 <a href="/register">Reg</a><br/>

I have nothing because doGet is used and no difference what I will write in doGet nothing changes and registration form doesn't appear.

Thus , my question is , what should I wrire in doGet to have my registration form ?

Aleksander Monk
  • 2,787
  • 2
  • 18
  • 31

3 Answers3

2

In the doGet method, forward to the desired page. This will do it:

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, java.io.IOException {
    //make sure this is the path of the page you want/need to show
    String path = "/register.jsp";
    RequestDispatcher dis = request.getRequestDispatcher(path);
    dis.forward(request, response);
}
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
0

Add the following to your doGet-Method:

response.sendRedirect("/register.jsp");

That should redirect to your register-page (if that is what you want), when /register is opened.

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
ariganis
  • 300
  • 2
  • 7
-1

You can write this in your doGet method:

doPost(request,response);

In this case, you process get and post form by the same way.

Regards

musef
  • 9
  • 1
  • -1. `doGet` and `doPost` may not do the same. `doGet` has to provide the info to only get the result .e.g. display a list of results. On the other hand, `doPost` is for processing info from the request e.g. file upload. Probably they have similar lines of code but that doesn't mean you should execute the same actions in both, even if in some specific cases you have, it's not a general rule. – Luiggi Mendoza Feb 02 '15 at 16:54
  • I do not agree at all. The processing of a form can be performed by these both methods . Another separate issue is whether it is appropriate or not for security or programing reasons. – musef Feb 02 '15 at 22:19
  • The fact that *you can* doesn't mean *you should*. That's the reason of my downvote, because this misguides readers. – Luiggi Mendoza Feb 02 '15 at 22:25