JSP基础(调用数据库数据库验证登录)

1.登录界面(1.login.jsp)

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

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'login.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    <form action="2.login_do.jsp">
          用户名:<input type="text" name="username"/><br>
          密码:<input type="password" name="password"/><br>
     <input type="submit" value="确定"/>     
    </form>
  </body>
</html>

2.验证页面(2.login_do.jsp)

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

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'testconnection.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
     <%
     String url="jdbc:mysql://localhost/shopping";
        String userName="root";
        String password="123456";
        
        Connection conn=null;
        Statement stmt=null;  
        ResultSet rst=null;
          
        Class.forName("com.mysql.jdbc.Driver");             
        conn=DriverManager.getConnection(url,userName,password);
        stmt=conn.createStatement();
        
        String inputname=request.getParameter("username");
        String inputpwd=request.getParameter("password");
        out.println(inputname);
        out.println(inputpwd);
        
        String sql="select * from user where name='"+inputname+"'";
        out.println(sql+"<br>");

        rst=stmt.executeQuery(sql);
        
        
/*           if(rst.next())
        {
           out.print("账号输入正确");
        }
        else
        {
           out.print("账号输入错误");
        } */
        
        rst.next();
        if(rst.getRow()==1)
         {
          out.println("账号输入正确");
            if(inputpwd.equals(rst.getString(2)))
              {
               out.println("密码输入正确");
               response.sendRedirect("/text9/3.login_success.jsp");
               }
            else
               {
               out.println("密码输入错误");
               out.println(rst.getString(2));
               out.println(inputpwd);
               response.sendRedirect("/text9/4.login_fail.jsp");
               }
         }    
        else
        {
          out.print("账号输入错误!");
          response.sendRedirect("/text9/5.login_fail2.jsp");
        }
                        
        rst.close();
        stmt.close();  
                                                                                                 
          try{
             if(conn!=null)
              conn.close();
            }
           catch(SQLException e)
           {
              out.println("关闭数据库连接时出现SQl异常");
           }           
      %>
  </body>
</html>

3.登录成功(3.login_success.jsp)

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

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'login_success.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    登录成功 <br>
  </body>
</html>

4.密码错误(4.login_fail.jsp)

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

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'login_fail.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    密码输入错误 <br>
  </body>
</html>

5.账号错误(5.login_fail2.jsp)

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

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP '5.login_fail2.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
         账号输入错误<br>
  </body>
</html>

  • 0
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="java.sql.*" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>普通话水平测试报名系统</title> </head> <body> <center> <h1>普通话水平测试报名系统</h1> <hr> <br> <br> <% //接收用户提交的参数 String username=request.getParameter("uname"); String userpassword=request.getParameter("upassword"); String userjb=request.getParameter("userjb"); //设置一个变量保存登录状态,ture表示登录成功,false表示登录失败 boolean flag=true; %> <% String driverclass="com.mysql.jdbc.Driver"; String url="jdbc:mysql://localhost:3306/atao"; String uname="root"; String upass="958672"; Connection conn=null; PreparedStatement stmt=null; ResultSet rs=null; Class.forName(driverclass); conn=DriverManager.getConnection(url,uname,upass); if(userjb.equals("1")) {stmt=conn.prepareStatement("select * from users where user_id=? and userpass=? and role=?"); stmt.setString(1,username); stmt.setString(2,userpassword); stmt.setString(3,userjb); rs=stmt.executeQuery(); if(rs.next()) { flag=false; } if(flag){ %> <script type="text/javascript" language="javascript"> alert("密码错误或无权限登录,请重新登录"); window.document.location.href="dl-index.jsp"; </script> <% } else{ request.setCharacterEncoding("UTF-8"); session.setAttribute("uname",rs.getString(1)); session.setAttribute("upassword",rs.getString(2)); session.setAttribute("userjb",rs.getString(3)); response.sendRedirect("sgly/gg-index.jsp"); rs.close(); stmt.close(); conn.close(); } } if(userjb.equals("2")) {stmt=conn.prepareStatement("select * from xxwyb where xxwy_id=? and password=? and role=?"); stmt.setString(1,username); stmt.setString(2,userpassword); stmt.setString(3,userjb); rs=stmt.executeQuery(); if(rs.next()) {session.setAttribute("id",rs.getString(1)); session.setAttribute("pd",rs.getString(7)); flag=false; } if(flag){ %> <script type="text/javascript" language="javascript"> alert("密码错误或无权限登录,请重新登录"); window.document.location.href="dl-index.jsp"; </script> <% } else{ request.setCharacterEncoding("UTF-8"); session.setAttribute("uname",rs.getString(1)); session.setAttribute("upassword",rs.getString(2)); session.setAttribute("userjb",rs.getString(3)); response.sendRedirect("sxxwy/xx-ym.jsp"); rs.close(); stmt.close(); conn.close(); } } if(userjb.equals("0")){ %> <script type="text/javascript" language="javascript"> alert("您还没有选择用户类型"); window.document.location.href="dl-index.jsp"; </script> <% } %> </center> </body> </html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值