Home > Java > javaTutorial > body text

Implement web page login and registration using Java

一个新手
Release: 2017-09-07 09:26:20
Original
6155 people have browsed it

1. There are html, jsp, and servlet for writing web pages. However, the three have their own advantages and disadvantages. HTML is suitable for writing some static displays, jsp is suitable for writing dynamic and variable displays, and servlet is suitable for processing business logic, distribution steering, and DAO. Data transfer.

2. This project uses jsp+servlet to implement a simple web page registration and login, which requires some basic jsp syntax, such as <% insert java code%>, <a herf= ""></a> means forwarding. form represents a form just like html. <!---- -----> indicates comments.
<%! %> Basic syntax such as declaring global variables, and several instructions page include taglib

3. Create a web page and generate a web.xml file by default. The project name is webServletTest. Create a webservlet.java in the java Resources directory.

package webJspDemo.com;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;/**
 * Servlet implementation class LoginServlet
 */@WebServlet("/servlet/loginservlet")public class LoginServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {        super();        // TODO Auto-generated constructor stub
    }    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {        // TODO Auto-generated method stub
        response.getWriter().append("Served at: ").append(request.getContextPath());        //获取表单数据
        response.setContentType("text/html; charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
        String userName = request.getParameter("userName");
        String pwd = request.getParameter("pwd");        if("tom".equals(userName)&&"123".equals(pwd)){
            request.getSession().setAttribute("name", userName);
            request.getRequestDispatcher("/register.jsp").forward(request, response);
        }else{
            request.setAttribute("msg", "用户名或者密码不正确");
            request.getRequestDispatcher("/login.jsp").forward(request, response);
        }        //分发转向
    }    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {        // TODO Auto-generated method stub
        doGet(request, response);
    }

}
Copy after login

4. Modify the mapping relationship and class name of web.xml

  <servlet>
    <servlet-name>LoginServlet</servlet-name>
    <servlet-class>webJspDemo.com.LoginServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>LoginServlet</servlet-name>
    <url-pattern>/login</url-pattern>
  </servlet-mapping>
Copy after login

5. Create 3 jsps in the WEB-INF directory, namely home.jsp, login.jsp, register .jsp file.

home.jsp code is

<body>
    <h1>欢迎来到本站!</h1>
    <%        
    String userName =(String)session.getAttribute("name");
    out.print(userName);
    %>
</body>
Copy after login

login.jsp code is:

<body>
    <%        
    String msg = (String)request.getAttribute("msg");      
    if(msg !=null){
          out.print(msg);     
      }

    %>
    <form action="/webJspDemo1/servlet/loginservlet" method="get">
        用户名:<input type="text" name="userName"/><br/>
        密码:<input type="password" name="pwd"/><br/>
        <input type="submit" value="登录"/><br/>

    </form></body>
Copy after login

register.jsp code is:

<body>
    欢迎你登录:    <% 
        String userName =(String)session.getAttribute("name");
        out.print(userName);        
    %>
    <a href="/webJspDemo1/home.jsp">跳到主页 </a></body>
Copy after login

5. Experimental results
Enter in the browser: http://localhost:8080/webJspDemo1/login
After entering tom and 123 in the form, it will jump to the register.jsp interface, and then to the home.jsp interface, basically completing a simple registration process.


The above is the detailed content of Implement web page login and registration using Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template