JSP实现登录注册并链接数据库页面

        在学习了页面跳转及部分知识后做了登录注册界面,并经过本博主调试bug后完善的更进一步,大家有什么问题也可以留言,本博主以更广泛学习讨论为目的。

        内容介绍:实现页面的跳转;注册登录时实现读取数据库,并对数据库实现插入(insert)和查询(select)功能。

        几点注意:sqljdbc.jar包的导入和环境变量;数据库的登录读取,可以参考数据库的测试。

登录注册界面的代码实现

index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
    
    
    My JSP 'Feilong_index.jsp' starting page
	
	
	    
	
	
	
  
  
    
欢迎使用飞龙科技

login.jsp  //登录界面

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
    
    
    My JSP 'Feilong_login.jsp' starting page
    
	
	
	    
	
	
	

  
    
登录界面 <% String flag = request.getParameter("errNo"); try{ if(flag!=null) out.println("用户名不存在或密码错误"); }catch(Exception e){ e.printStackTrace(); } %>
账 户:
密 码:
loginCh.jsp  //登录检验
<%@ page language="java" import="java.util.*,java.sql.*,java.net.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>


  
    
    
    My JSP 'Feilong_loginCh.jsp' starting page
    
	
	
	    
	
	
	

  
  
    <%      //接收用户名和密码  
            String user = new String(request.getParameter("name").getBytes("ISO-8859-1"),"UTF-8");  
            String pwd = request.getParameter("pwd");

            String driverClass = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
            String url = "jdbc:sqlserver://localhost:1433; DatabaseName = db_01";
            String username = "sa";
            String password = "123";
            Class.forName(driverClass);//加载驱动 
            Connection conn = DriverManager.getConnection(url,username,password);//得到连接
            PreparedStatement pStmt = conn.prepareStatement("select * from tb_user where UName = '" + user + "' and Pwd = '" + pwd + "'");
              ResultSet rs = pStmt.executeQuery();
                if(rs.next()){
                    response.sendRedirect("success.jsp?username="+URLEncoder.encode(user)); //解决乱码 
                }else{
                    response.sendRedirect("login.jsp?errNo");//密码不对返回到登陆  
                }
     rs.close();
     pStmt.close();
     conn.close();
     %>
  

success.jsp  //登录成功界面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
    
    Feilong_登录成功
	
	
	    
	
	
	
  
  
    
<% String name = new String(request.getParameter("username").getBytes("8859_1")); out.println("欢迎你:" + name); %>
重新登陆

register.jsp  //注册界面

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>


  
    
    My JSP 'Feilong_register.jsp' starting page
	
	
	    
	
	
	
    
  
    
注册界面
用户名:
输入密码:
确认密码:
    
checkRegister.jsp //验证注册用户并导入数据库

<%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
    
    
    My JSP 'Feilong_chechRegister.jsp' starting page
    
	
	
	    
	
	
	

  
  
    <%      
            String user = new String(request.getParameter("username").getBytes("ISO-8859-1"),"UTF-8");  
            String pwd = request.getParameter("password");

            String driverClass = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
            String url = "jdbc:sqlserver://localhost:1433; DatabaseName = db_01";
            String username = "sa";
            String password = "123";
            Class.forName(driverClass);//加载驱动 
            Connection conn = DriverManager.getConnection(url,username,password);//得到连接
            PreparedStatement pStmt = conn.prepareStatement("select * from tb_user where UName = '" + user + "'");
              ResultSet rs = pStmt.executeQuery();
                if(rs.next()){
                    out.println("");
                }else{
                    PreparedStatement tmt = conn.prepareStatement("Insert into tb_user values('" + user + "','" + pwd + "')");
                        int rst = tmt.executeUpdate();
                        if (rst != 0){
                              out.println("");  
                        }else{
                           out.println("");  
                        }
                }
     %>
  


以上代码若有问题,首先执行下面数据库的测试代码:

执行下面代码前,首先在数据库中创建名为‘db_01’的数据库,在表中创建名为‘user’的表

login1.jsp

<%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



  
    
    
    My JSP 'Feilong_login.jsp' starting page
    
	
	
	    
	
	
	

  
  
    <%
    String driverClass = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
    String url = "jdbc:sqlserver://localhost:1433; DatabaseName = db_01";
    String username = "sa";
    String password = "123";
    Class.forName(driverClass);//这步错可能是因为.jar包导入问题
    Connection conn = DriverManager.getConnection(url,username,password);//这步错可能是因为数据库属性安全中的名、密码不对或SQL的IP端口不是‘1433’
    PreparedStatement pStmt = conn.prepareStatement("select * from tb_user");
           ResultSet rs = pStmt.executeQuery();
                while(rs.next()){
                      out.println("用户名: " + rs.getString(1) 
                             + " 密码: " + rs.getString(2));
                }
     rs.close();
     pStmt.close();
     conn.close();
     %>
  

小人团队.jpg

JSP实现登录注册并链接数据库页面_第1张图片

以上代码都是经过本博主细心敲打修改并实现的。转载请注明本博客的地址。

你可能感兴趣的:(JSP)