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 98a199e2b434149b4b49cce37a5985d8, 38db2913860440c5805a754204ae4a315db79b134e9f6b82c0b36e0489ee08ed
means forwarding. form represents a form just like html. 117befdeba1fd957497e82f8445cb3d1
indicates comments.
a51d2e2da6e7069c0f0d8111919fc496 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); } }
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>
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>
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>
register.jsp code is:
<body> 欢迎你登录: <% String userName =(String)session.getAttribute("name"); out.print(userName); %> <a href="/webJspDemo1/home.jsp">跳到主页 </a></body>
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!