数据库准备—创建db_login数据库  t_user表

1、创建web工程

2、创建用户model   user.java

 1 package com.gxy.model;
 2
 3 public class User {
 4     private int id;
 5     private String userName;
 6     private String password;
 7
 8     public User() {
 9         super();
10     }
11
12
13     public User(String userName, String password) {
14         super();
15         this.userName = userName;
16         this.password = password;
17     }
18
19
20     public int getId() {
21         return id;
22     }
23     public void setId(int id) {
24         this.id = id;
25     }
26     public String getUserName() {
27         return userName;
28     }
29     public void setUserName(String userName) {
30         this.userName = userName;
31     }
32     public String getPassword() {
33         return password;
34     }
35     public void setPassword(String password) {
36         this.password = password;
37     }
38
39 }

3、创建util包 Dbutil.java

 1 package com.gxy.util;
 2
 3 import java.sql.Connection;
 4 import java.sql.DriverManager;
 5
 6 public class Dbutil {
 7     private String dbUrl ="jdbc:mysql://localhost:3306/db_login";
 8     private String jdbcName="com.mysql.jdbc.Driver";
 9     private String dbUserName="root";
10     private String dbpassword="123456";
11
12     public Connection getcon() throws Exception{
13         Class.forName(jdbcName);
14         Connection con=DriverManager.getConnection(dbUrl,dbUserName,dbpassword);
15         return con;
16     }
17
18     public void closeCon(Connection con) throws Exception{
19         con.close();
20     }
21
22 }

4、创建dao包  UserDao.java

 1 package com.gxy.dao;
 2
 3 import java.sql.Connection;
 4 import java.sql.PreparedStatement;
 5 import java.sql.ResultSet;
 6
 7 import com.gxy.model.User;
 8
 9 public class UserDao {
10     public User login(Connection con,User user) throws Exception{
11         User resultUser=null;
12         String sql="select * from t_user where userName=? and passWord=?";
13         PreparedStatement pst=con.prepareStatement(sql);
14         pst.setString(1, user.getUserName());
15         pst.setString(2,user.getPassword());
16         ResultSet rs=pst.executeQuery();
17         if(rs.next()){
18             resultUser=new User();
19             resultUser.setUserName(rs.getString("userName"));
20             resultUser.setPassword(rs.getString("passWord"));
21         }
22         return resultUser;
23     }
24
25 }

5、创建servlet包  loginServlet

 1 package com.gxy.servlet;
 2
 3 import java.io.IOException;
 4 import java.sql.Connection;
 5
 6 import javax.servlet.RequestDispatcher;
 7 import javax.servlet.ServletException;
 8 import javax.servlet.http.HttpServlet;
 9 import javax.servlet.http.HttpServletRequest;
10 import javax.servlet.http.HttpServletResponse;
11 import javax.servlet.http.HttpSession;
12
13 import com.gxy.dao.UserDao;
14 import com.gxy.model.User;
15 import com.gxy.util.Dbutil;
16
17 public class loginServlet extends HttpServlet{
18
19     /**
20      *
21      */
22     private static final long serialVersionUID = 1L;
23
24     Dbutil dbutil=new Dbutil();
25     UserDao userDao=new UserDao();
26     @Override
27     protected void doGet(HttpServletRequest req, HttpServletResponse resp)
28             throws ServletException, IOException {
29         this.doPost(req, resp);
30     }
31
32     @Override
33     protected void doPost(HttpServletRequest req, HttpServletResponse resp)
34             throws ServletException, IOException {
35         String userName=req.getParameter("userName");
36         String passWord=req.getParameter("passWord");
37
38         Connection con=null;
39         try {
40             User user=new User(userName,passWord);
41             con=dbutil.getcon();
42             User resultUser=userDao.login(con, user);
43             if(resultUser==null){
44                 System.out.println("no");
45             }else{
46                 HttpSession session=req.getSession();
47                 session.setAttribute("userName", resultUser.getUserName());
48                 session.setAttribute("passWord", resultUser.getPassword());
49                 resp.sendRedirect("target.jsp");
50             }
51         } catch (Exception e) {
52             // TODO Auto-generated catch block
53             e.printStackTrace();
54         }
55
56     }
57
58 }

6、用户登录界面  login.jsp

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <%
 3 String path = request.getContextPath();
 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 5 %>
 6
 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 8 <html>
 9   <head>
10     <base href="<%=basePath%>">
11
12     <title>My JSP 'login.jsp' starting page</title>
13
14     <meta http-equiv="pragma" content="no-cache">
15     <meta http-equiv="cache-control" content="no-cache">
16     <meta http-equiv="expires" content="0">
17     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
18     <meta http-equiv="description" content="This is my page">
19     <!--
20     <link rel="stylesheet" type="text/css" href="styles.css">
21     -->
22
23   </head>
24
25   <body>
26   <form action="login" method="post">
27     <table>
28         <tr>
29             <td>用户名:</td>
30             <td><input type="text" id="userName" name="userName"></td>
31         </tr>
32         <tr>
33             <td>密码:</td>
34             <td><input type="password" id="passWord" name="passWord"></td>
35         </tr>
36         <tr>
37             <td colspan="2"><input type="submit" value="提交" ></td>
38         </tr>
39     </table>
40    </form>
41   </body>
42 </html>

跳转界面  target.jsp

 1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
 2 <%
 3 String path = request.getContextPath();
 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 5 %>
 6
 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
 8 <html>
 9   <head>
10     <base href="<%=basePath%>">
11
12     <title>My JSP 'target.jsp' starting page</title>
13
14     <meta http-equiv="pragma" content="no-cache">
15     <meta http-equiv="cache-control" content="no-cache">
16     <meta http-equiv="expires" content="0">
17     <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
18     <meta http-equiv="description" content="This is my page">
19     <!--
20     <link rel="stylesheet" type="text/css" href="styles.css">
21     -->
22
23   </head>
24
25   <body>
26     <h1>用户名:<%=session.getAttribute("userName") %></h1>
27     <h1>密码:<%=session.getAttribute("passWord") %></h1>
28   </body>
29 </html>

7、运行结果

转载于:https://www.cnblogs.com/guoxiangyue/p/8439856.html

jspservlet初体验——用户登录功能实现相关推荐

  1. 如何设计安全的用户登录功能

    用户登录功能是Web应用系统具备的最基本的功能,关系到用户数据和应用系统数据的安全,设计一个安全的用户登录功能,涉及到以下几个方面的内容. (一) 老生常谈--口令 1. 口令长度与复杂度限制 限制用 ...

  2. [转]你会做Web上的用户登录功能吗?

    Web上的用户登录功能应该是最基本的功能了,可是在我看过一些站点的用户登录功能后,我觉得很有必要写一篇文章教大家怎么来做用户登录功能.下面的文章告诉大家这个功能可能并没有你所想像的那么简单,这是一个关 ...

  3. 实现Web上的用户登录功能

    https://coolshell.cn/articles/5353.html Web上的用户登录功能应该是最基本的功能了,可是在我看过一些站点的用户登录功能后,我觉得很有必要写一篇文章教大家怎么来做 ...

  4. Web上的用户登录功能安全

    转载自:http://www.daimami.com/web/217218.htm 你会做Web上的用户登录功能吗? Web上的用户登录功能应该是最基本的功能了,可是在我看过一些站点的用户登录功能后, ...

  5. web上的用户登录功能

    Web上的用户登录功能应该是最基本的功能了,可是在我看过一些站点的用户登录功能后,我觉得很有必要写一篇文章教大家怎么来做用户登录功能.下面 的文章告诉大家这个功能可能并没有你所想像的那么简单,这是一个 ...

  6. 你会做Web上的用户登录功能吗?

    你会做Web上的用户登录功能吗? 2011年8月25日 陈皓    Web上的用户登录功能应该是最基本的功能了,可是在我看过一些站点的用户登录功能后,我觉得很有必要写一篇文章教大家怎么来做用户登录功能 ...

  7. Web上的用户登录功能——酷壳_陈皓

    为什么80%的码农都做不了架构师?>>>    Web上的用户登录功能应该是最基本的功能了,可是在我看过一些站点的用户登录功能后,我觉得很有必要写一篇文章教大家怎么来做用户登录功能. ...

  8. php mysql用户登录_php mysql实现用户登录功能的代码示例

    接着上次的php mysql添加用户的功能代码,今天来学习下php实现用户登录与注销的功能,通过跟踪session会话来保存用户的登陆状态. 1,登录页面 login.php 用户登录_www.# 用 ...

  9. Apache Shiro实现用户登录功能

    apache shiro实现用户登录功能 配置shiro的Filter实现URL级别权限控制 配置web.xml <!-- shiro的过滤器 --> <filter>< ...

最新文章

  1. [BZOJ1925]地精部落
  2. 前端学习(2887):如何短时间内实现v-for createApp解决方案
  3. 移动端Web开发如何处理横竖屏
  4. 账户配置阻止使用计算机.怎样开机,开机自启动设置怎么操作 开机自启动设置如何禁止【图文介绍】...
  5. Flutter 底部向上弹出的动画按钮
  6. C语言中的多字节字符与宽字符
  7. 搭配和谐的色彩的秘密
  8. string.split方法 保留分隔符_Python pandas库159个常用方法使用说明
  9. HTTP协议之Session和Cookie
  10. java编译提示错误信息_java常见编译错误信息
  11. 是计算机系男神女神用英语怎么说,生日快乐我的男神英文怎么写
  12. linux脚本解密,shell脚本加密与解密
  13. python lisp_随笔:code.org与python,FoxDot,Lisp
  14. c#阿里CSB接口对接--
  15. 摸爬滚打DirectX11_day02——VS2010+DirectX11的环境配置
  16. windows和linux快捷键
  17. 三维视频融合 倾斜摄影模型 开放c++源代码 支持与java js交互 内嵌web容器 可用于雪亮工程 等 安防项目 稳定流畅 点卯系列
  18. java 模拟火车站售票系统_模拟售票系统java编程
  19. 国内的多语言网站主要体现在哪几方面
  20. 童继龙的ERP顾问成长感悟

热门文章

  1. 征战蓝桥 —— 2017年第八届 —— C/C++A组第6题——最大公共子串
  2. 【IT资讯】继哈工大Matlab软件被美禁用后,华为、360再遭Docker软件禁令
  3. 【嵌入式】C语言高级编程-内建函数(11)
  4. 【Linux系统编程】Linux 信号列表
  5. 【Linux系统编程】Linux 可执行文件结构与进程结构
  6. apache的es的原理_ElasticSearch原理
  7. i386和X86各是什么意思
  8. 【算法】二分图的判定
  9. 数据结构-----最大堆的实现
  10. mysql2000 sp4_SQL Server 2000 (SP4)笔记整理(二):数据库表