Servlet实现用户的登录功能
Posted lin1216
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Servlet实现用户的登录功能相关的知识,希望对你有一定的参考价值。
首先,前面我们已经实现与数据库的连接和用户的注册功能,接下来就是用户通过已经注册的信息进行登录功能的实现
1.接前两篇博客,首先先编写一个简单的登录页面,代码如下:
1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2 pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>用户登录页面</title> 8 </head> 9 <body> 10 <h2>$remind</h2> 11 <h1>登录页面</h1> 12 13 <form action="login" method="post"> 14 登录名:<input type="text" name="name"><br/> 15 密码为:<input type="password" name="password"><br/> 16 <input type="submit" value="登录"> 17 </form> 18 19 <h5>还未注册,快来注册</h5> 20 <a href="register">注册</a> 21 </body> 22 </html>
2.编写实现用户登录的Servlet,在此处新建一个Servlet,命名为LoginServlet.java ,同时在web.xml配置文件中实现对该Servlet的配置,在web.xml的配置如下所示:
代码如下:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> 3 <display-name>Test1</display-name> 4 <welcome-file-list> 5 <welcome-file>index.html</welcome-file> 6 <welcome-file>index.htm</welcome-file> 7 <welcome-file>index.jsp</welcome-file> 8 <welcome-file>default.html</welcome-file> 9 <welcome-file>default.htm</welcome-file> 10 <welcome-file>default.jsp</welcome-file> 11 </welcome-file-list> 12 <servlet> 13 <servlet-name>RegisterServlet</servlet-name> 14 <servlet-class>lin.Servlet.RegisterServlet</servlet-class> 15 </servlet> 16 <servlet-mapping> 17 <servlet-name>RegisterServlet</servlet-name> 18 <url-pattern>/register</url-pattern> 19 </servlet-mapping> 20 <servlet> 21 <servlet-name>LoginServlet</servlet-name> 22 <servlet-class>lin.Servlet.LoginServlet</servlet-class> 23 </servlet> 24 <servlet-mapping> 25 <servlet-name>LoginServlet</servlet-name> 26 <url-pattern>/login</url-pattern> 27 </servlet-mapping> 28 </web-app>
3.编写LoginServlet.java的代码,代码如下:
1 package lin.Servlet; 2 3 import java.io.IOException; 4 import java.sql.Connection; 5 import java.sql.PreparedStatement; 6 import java.sql.ResultSet; 7 import java.sql.SQLException; 8 9 import javax.servlet.ServletException; 10 import javax.servlet.annotation.WebServlet; 11 import javax.servlet.http.HttpServlet; 12 import javax.servlet.http.HttpServletRequest; 13 import javax.servlet.http.HttpServletResponse; 14 15 import lin.db.DBServlet; 16 import lin.entity.User; 17 18 /** 19 * Servlet implementation class LoginServlet 20 */ 21 /*@WebServlet("/LoginServlet")*/ 22 public class LoginServlet extends HttpServlet 23 private static final long serialVersionUID = 1L; 24 25 /** 26 * @see HttpServlet#HttpServlet() 27 */ 28 public LoginServlet() 29 super(); 30 // TODO Auto-generated constructor stub 31 32 33 /** 34 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 35 */ 36 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 37 // TODO Auto-generated method stub 38 request.getRequestDispatcher("/WEB-INF/page/login.jsp").forward(request, response); 39 40 41 /** 42 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) 43 */ 44 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 45 // TODO Auto-generated method stub 46 //设置utf-8,防止出现乱码情况 47 request.setCharacterEncoding("UTF-8"); 48 response.setCharacterEncoding("text/html;charset=UTF-8"); 49 50 51 //获取实体User中的变量,将变量插入到数据库中,完成注册 52 User user=new User(); 53 //获取注册的用户名,"name"要与jsp页面中的变量名一致 54 String name=request.getParameter("name"); 55 //获取注册的密码 56 String password=request.getParameter("password"); 57 58 59 try 60 //sql 语句用于查询name和password是否与数据库中存储的一致 61 String sql="select * from t_users where name=? and password=?;"; 62 DBServlet db=new DBServlet(); 63 //与数据库获得连接 64 Connection conn=db.getConn(); 65 66 //SQL语句被预编译并存储在PreparedStatement对象中 67 PreparedStatement statement=conn.prepareStatement(sql); 68 //将sql语句的问号值添加进去 69 statement.setString(1, name); 70 statement.setString(2, password); 71 72 //执行sql语句 73 ResultSet rs=statement.executeQuery(); 74 if(rs.next()) 75 76 System.out.println("登录成功,欢迎你:"+name); 77 request.setAttribute("remind", "登录成功"); 78 request.getRequestDispatcher("/WEB-INF/page/login.jsp").forward(request, response); 79 else 80 81 System.out.println("登录失败,密码或用户名错误"); 82 request.setAttribute("remind", "登录成功"); 83 request.getRequestDispatcher("/WEB-INF/page/login.jsp").forward(request, response); 84 85 86 //关闭数据库 87 rs.close(); 88 statement.close(); 89 conn.close(); 90 91 catch (SQLException e) 92 // TODO Auto-generated catch block 93 System.out.println("数据库连接失败"); 94 e.printStackTrace(); 95 96 97 98 99
4.测试:先注册一个用户,例如,用户名:易烊千玺1 密码:123456
点击登录,之后
控制台输出如下所示:
这里对用户的登录功能的实现已经成功,拜拜了,,,,,,
还不知道接下来要写什么,好好学习了,对于servlet的理解应该也差不多了,试着写写前端和框架吧
以上是关于Servlet实现用户的登录功能的主要内容,如果未能解决你的问题,请参考以下文章
jsp+servlet+mysql 实现简单的银行登录转账功能