Java中的表单填写验证

时间:2011-02-13 10:22:30

标签: java servlets validation

我确实使用servlet登记,但它确实有效。

如何验证表格填写? 例如,如果用户名已经注册,请将用户重新发送到.jsp文件?

抱歉英文不好。

1 个答案:

答案 0 :(得分:2)

public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {

    // fetch the username that was sent in the request
    String username = request.getParameter("username");

    // TODO: verify if the username is taken in the database

    // based on the results set the value
    request.setAttribute("isUsernameTaken", "true");

    RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/register.jsp");

    dispatcher.forward(request, response);      
}