servlet / jsp,doGet和doPost,注册表

时间:2015-02-02 16:04:47

标签: java jsp servlets

我正在java servlet / jsp中编写Blog。现在我有注册问题。

这就是我在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);
    }
}

我的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>

所以,如果我使用此链接进入注册页面

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

这很好,我有我的注册表,因为使用了doPost。

但是,如果我尝试

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

我什么都没有,因为使用了doGet并且没有区别我将在doGet中写什么没有任何更改和注册表单没有出现。

因此,我的问题是,我应该在doGet中写下我的注册表单?

3 个答案:

答案 0 :(得分:2)

doGet方法中,转到所需的页面。这样就可以了:

@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);
}

答案 1 :(得分:0)

将以下内容添加到您的doGet-Method:

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

当/ register打开时,应该重定向到你的寄存器页面(如果这是你想要的)。

答案 2 :(得分:-1)

您可以在doGet方法中编写:

doPost(request,response);

在这种情况下,您以相同的方式处理get和post表单。

此致