(详细)Eclips+jsp+servlet+mysql+登录实例+源代码

 欢迎任何形式的转载,但请务必注明出处。

      该教程较全,从软件的安装以及相关的环境配置我都放置了相关教程的链接,读者可直接点击进入。自己写电商网站作业时查找了很多资料,但都不是很全,所以趁着寒假写了这份教程,也对自己所学的内容总结总结。如果一些地方读者看不懂,可以自行翻阅我的其他教程内容或私信我,我可能忘放链接了。还有一些专业名词请问度娘。

必做:

 本节内容

  • 新建项目
  • 结构图
  • 数据库MySQL
  • 登录页面Login.jsp
  • java类编写
  • xml编写
  • 完善Login.jsp
  • 效果展示

 

一、新建项目

      Dynamic WEB项目 “MyWeb”

      点击进入教程3 实例部分

二、结构图

      新建JSP页面"Login"里面的各项,保证红框部分都没丢

 三、数据库

数据库的编写我略掉了,读者自己编写时请和我的数据库名称以及表名一致。或者在Connsql中修改代码(Eclipse连接Mysql我前面有具体文章)

四、登录页面Login.jsp

      servlet完成后此块还要完善

 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    <form method="" action="" >
11        <!-- 用户名-->
12        <label for="userName">用户名</label> 
13        <input type="text" name="userName"/><br>
14        
15        <!-- 密码-->
16        <label for="passWord">密 码</label>
17        <input type="password" name="passWord" /><br><br>
18        
19         <!-- 登录 -->
20        <input type="submit" value="Sign Up"></input>
21    </form> 
22 </body>
23 </html>

五、java类编写

 

 1 package com.entity;
 2 
 3 public class Customer {
 4     private String cusname;
 5     private String cuspassword;
 6 
 7     public Customer(String cusname,String cuspassword){
 8         this.cusname = cusname;
 9         this.cuspassword = cuspassword;
10     }
11     
12     //会员名字
13     public String getCusname() {
14         return cusname;
15     }
16     public void setCusname(String cusname) {
17         this.cusname = cusname;
18     }
19     //会员密码
20     public String getCuspassword(){
21         return cuspassword;
22     }
23     public void setCuspassword(String cuspassword){
24         this.cuspassword = cuspassword;
25     }
26 }

 

 1 package com.function;
 2 
 3 import java.sql.DriverManager;
 4 import java.sql.ResultSet;
 5 import java.sql.SQLException;
 6 
 7 import com.entity.Customer;  
 8 import com.mysql.jdbc.Connection;
 9 import com.mysql.jdbc.PreparedStatement;
10 
11 public class Connsql {
12     /**************连接数据库部分********************/
13     public static Connection conn(){
14         Connection conn = null;
15         
16         String driver = "com.mysql.jdbc.Driver";
17         String url = "jdbc:mysql://localhost:3306/demo?characterEncoding=utf-8&useSSL=false";
18         String username="root";
19         String pw = "111111";
20         
21         try {            
22             Class.forName(driver);//加载MySql的驱动类   
23             System.out.println("成功加载驱动程序!!!!");
24             conn = (Connection) DriverManager.getConnection(url,username,pw);//创建数据库的连接  
25         } catch (Exception e) {
26             // TODO: handle exception
27             System.out.println("找不到驱动程序类 ,加载驱动失败!");   
28             e.printStackTrace();
29         }
30         return conn;
31     }
32     
33     /**************用户登录********************/
34     public boolean cusLogin(Customer customer){
35         Connection conn = conn();
36         PreparedStatement pstmt = null;//创建一个Statement  
37         ResultSet rs = null;           //创建结果集
38         String sql = "select * from user where username = ? and userpass= ? ";//创建SQL语句
39         boolean bl = false;
40         
41         try{
42             pstmt = (PreparedStatement) conn.prepareStatement(sql);
43             pstmt.setString(1, customer.getCusname());    //设置'?'值
44             pstmt.setString(2, customer.getCuspassword());//设置'?'值
45             
46             rs = pstmt.executeQuery();  //执行SQL语句 
47             
48             if(rs.next()){           //处理结果
49                 System.out.println("用户名密码正确");
50                 bl = true;
51             }
52             
53             rs.close();     //关闭记录集  
54             pstmt.close();  //关闭声明 
55             conn.close();   //关闭连接对象  
56         }catch(SQLException e){
57             e.getStackTrace();
58         }
59         return bl;
60     }
61 }
 1 package com.servlet;
 2 
 3 import java.io.IOException;
 4 import javax.servlet.ServletException;
 5 import javax.servlet.http.HttpServlet;
 6 import javax.servlet.http.HttpServletRequest;
 7 import javax.servlet.http.HttpServletResponse;
 8 
 9 import com.entity.Customer;
10 import com.function.Connsql;
11 
12 public class CusLogin extends HttpServlet {
13     private static final long serialVersionUID = 1L;
14 
15     public CusLogin() {
16         super();
17         // TODO Auto-generated constructor stub
18     }
19 
20     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
21         try{
22         request.setCharacterEncoding("UTF-8");
23         response.setCharacterEncoding("UTF-8");        
24         
25         //表单数据获取
26         String name = request.getParameter("userName");
27         String password = request.getParameter("passWord");        
28         Customer customer = new Customer(name, password);
29         
30         //数据库部分
31         Connsql sql = new Connsql();          //新建数据库连接
32         boolean bl =sql.cusLogin(customer);   //检查用户名
33         
34         if(bl){   //登录成功
35             response.sendRedirect("Login.jsp?loginerror=no");  //将失败响应传递给Login.jsp
36             System.out.printf("success");
37         }else{   //登录失败
38             response.sendRedirect("Login.jsp?loginerror=yes");  //将失败响应传递给Login.jsp
39             System.out.printf("faild");
40         }        
41     }catch(Exception ex){
42         ex.printStackTrace();
43     }
44     }
45 
46 
47     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
48         // TODO Auto-generated method stub
49         doGet(request, response);
50     }
51 
52 }

六、编写web.xml

 打开web.xml在内容里添加如下代码

1   <servlet>
2     <servlet-name>CusLogin</servlet-name>
3     <servlet-class>com.servlet.CusLogin</servlet-class>
4   </servlet>
5   <servlet-mapping>
6     <servlet-name>CusLogin</servlet-name>
7     <url-pattern>/CusLogin</url-pattern>
8   </servlet-mapping>

七、完善Login.jsp

1.于<form>中补充action

<form method="get" action="CusLogin" 

2.于</html>底部增加<script>,接受响应

<script> 
//取出传回来的参数error并与yes比较  
  var error ='<%=request.getParameter("loginerror")%>'; if(error=="yes"){ alert("用户名或密码错误!"); //弹框:登陆失败  } else if(error=="no"){ alert("登陆成功!"); //弹框,登陆成功  } </script>

八、效果展示

 

转载于:https://www.cnblogs.com/L-xmin/p/6349222.html

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值