纯JSP用户登录系统

login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%
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 bgcolor = "pink">
  
  <!-- 居中对齐 -->
  <center>
  用户登录<br>
  <hr>
  <%
  	String flag = request.getParameter("errNo");
  	try{
  	
	  	if(flag.equals("1")){
	  		out.println("密码错误");
	  	}
	  	if(flag.equals("2")){
	  		out.println("用户名不存在");
	  	}
  	}catch(Exception e){
  		e.printStackTrace();
  	}
  	
  	
   %>
  <form action = "loginCl.jsp" method="post">
  	用户名:<input type="text" name="username"><br>
  	密码:<input type="password" name ="passwd"><br>
  	
  	<input type="submit" value="提交">
  	
  	<input type="reset" value="重置">
  	
  	
  </form>
  </center>
  </body>
</html>

loginCl.jsp验证用户名密码

 

<%@ page language="java" import="java.util.*,java.sql.*" pageEncoding="gb2312"%>
<%
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 'loginCl.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>
  	<%
  		
  		Connection conn= null ;
  		Statement smt = null;
  		ResultSet rs = null;
  		
  		try{
  		
	  		//接收用户名和密码
	  		String u = request.getParameter("username");
	  		String p = request.getParameter("passwd");
	  		
	  		//在数据库中验证用户
	  		String className="oracle.jdbc.driver.OracleDriver";
	  	//	String url = "jdbc:oracle:thin:@192.168.1.3:1521:unionuat";
	  		String url = "jdbc:oracle:thin:@192.168.1.3:1521:unionuat";
	  		
	  		String userName = "sales_jacdcs";
	  		String userPass ="sales_jacdcs";
	  		
	  		
	  		//加载驱动
	  		Class.forName(className);
	  		//得到连接
	  		//注意这里饭的错误:url userName uerPass已经是字符串的格式。下面的语句不需要加""双引号。害了我10分钟
	  		 conn = DriverManager.getConnection(url,userName,userPass);
	  		//创建Statement
	  		 smt = conn.createStatement();
	  		
	  			//查询
	  	//	StringBuffer sb = "select passwd from yyytestusers where username = ';
	  		 rs = smt.executeQuery("select passwd from yyytestusers where username = '"+ u +"'");
	  		//
	  		
	  		if(rs.next()){
	  			//c存在用户名
	  			//判断密码
	  			if(rs.getString(1).equals(p)){
	  				response.sendRedirect("wel.jsp?user="+u);
	  			}else{
	  			//密码不对返回到登陆
	  				response.sendRedirect("login.jsp?errNo=1");
	  			}
	  			
	  		}else{
	  			response.sendRedirect("login.jsp?errNo=2");
	  		}
	  		
  		}catch(Exception e){
  		
  			e.printStackTrace();
  		}finally{
	  		if(null != rs){
	  			rs.close();
	  		}
	  		if(null != smt){
	  			smt.close();
	  		}
  			if(null != conn){
  				conn.close();
  			}
  			
  			
  		}
  		
  		
  	
  		
  	/*	
  		//简单验证用户名密码
  		if(u.equals("yyy") && p.equals("123")){
  			//跳转页面
  			//将数据传到下一个界面(注意这里的wel.jsp?user中间不允许有空格,否则会出错)
  			response.sendRedirect("wel.jsp?user="+u);
  			
  			//
  		}else{
  		
  			//不合法
  			response.sendRedirect("login.jsp");
  		}
  		
  		*/
  		
  	
  	 %>
  </body>
</html>


wel.jsp成功登录界面

<%@ page language="java" import="java.util.*,  java.sql.*" pageEncoding="gb2312"%>
<%
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 'wel.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>
  	登陆成功!<%= request.getParameter("user") %><br>
  	<a href="login.jsp">重新登陆</a>
  	
  	<h1>用户列表</h1>
  	
  	<%
  		//定义分页用到的4个变量
  		int pageSize = 3; //每页显示3个
  		int pageNow = 1; //默认第一页
  		int rowCount = 0;//所有记录数
  		int pageCount=0;// rowCount/pageSize
  		
  		//获取页面传来的pageNow信息
  		String pp = request.getParameter("pageTo");
  		if(pp != null){
  			try{
  			
  				pageNow = Integer.parseInt(pp);
  				
  				}catch(Exception e){
  					out.println("pagenow=" + pageNow);
  					e.printStackTrace();
  				}
  		}
  		
  		//查询所有的记录数
  		
  		Connection conn= null ;
  		Statement smt = null;
  		ResultSet rs = null;
  		
  		try{
  		
	  		//接收用户名和密码
	  		String u = request.getParameter("username");
	  		String p = request.getParameter("passwd");
	  		
	  		//在数据库中验证用户
	  		String className="oracle.jdbc.driver.OracleDriver";
	  	//	String url = "jdbc:oracle:thin:@192.168.1.3:1521:unionuat";
	  		String url = "jdbc:oracle:thin:@192.168.1.3:1521:unionuat";
	  		
	  		String userName = "sales_jacdcs";
	  		String userPass ="sales_jacdcs";
	  		
	  		
	  		//加载驱动
	  		Class.forName(className);
	  		//得到连接
	  		//注意这里饭的错误:url userName uerPass已经是字符串的格式。下面的语句不需要加""双引号。害了我10分钟
	  		 conn = DriverManager.getConnection(url,userName,userPass);
	  		//创建Statement
	  		 smt = conn.createStatement();
	  		
	  			//查询
	  	//	StringBuffer sb = "select passwd from yyytestusers where username = ';
	  		 rs = smt.executeQuery("select count(*) from yyytestusers ");
	  		
	  		//一定要rs.next()  rs才指向要取得值的地方
	  		if(rs.next()){
	  			
	  			rowCount = rs.getInt(1);//1 :表示 一行一列
	  			
	  		}
	  		}catch(Exception e){
  		
  			e.printStackTrace();
  			
  		}finally{
	  	/*	if(null != rs){
	  			rs.close();
	  		}
	  		if(null != smt){
	  			smt.close();
	  		}
  			if(null != conn){
  				conn.close();
  			}*/
  		}
	  		
	  		//计算pageCount
	  		if(rowCount%pageSize == 0){
	  			pageCount = rowCount / pageSize;
	  		}else{
	  			pageCount = rowCount/ pageSize + 1;
	  		}
	  		//查询需要显示的记录
	  		String select_by_pageNow = "select * from (select rownum rn, a.* from yyytestusers a) where rn < "+ (pageNow*pageSize+1)
  					 +"and rn not in (select rn from (select rownum rn, a.* from yyytestusers a) where rn < "+((pageNow-1)*pageSize+1) +
   					 ") order by userid";
	  		try{
	  			rs = smt.executeQuery(select_by_pageNow);
	  			}catch(Exception e){
	  				e.printStackTrace();
	  			}
	  		
	  		%>
	  		<table border = "1">
	  		<tr><td>用户ID</td><td>名字</td><td>密码</td><td>Email</td><td>级别</td></tr>
	  		<%
	  			while(rs.next()){
	  				%>
	  				<!-- 注意:这里使用的是字符串的形式得到值 rs.getInt("userid")  rs.getInt(1) 会出异常!!待答复-->
	  				<tr><td><%=rs.getInt("userid") %></td><td><%=rs.getString("username") %></td><td><%=rs.getString("passwd") %></td><td><%=rs.getString("email") %></td><td><%=rs.getInt("grade") %></td></tr>
	  				
	  				<%
	  			}
	  		 %>
	  		
	  		</table>
	  		
	  		<%
	  		//显示超链接
	  		//首页
	  		out.println("<a href=wel.jsp?pageTo="+1+">首页</a>");
	  		//上一页
	  		if(pageNow != 1){
	  			out.println("<a href=wel.jsp?pageTo="+(pageNow - 1)+">上一页</a>");
	  		}
	  		// 判断页数的
	  		if(pageNow == 1){
	  			for(int i = pageNow; i <= pageNow + 5; i++){
	  				out.println("<a href= wel.jsp?pageTo="+i+"> ["+i+"] </a>");
	  			}
	  		}else 	if(pageNow == 2){
	  			for(int i = pageNow-1; i < pageNow + 5; i++){
	  				out.println("<a href= wel.jsp?pageTo="+i+"> ["+i+"] </a>");
	  			}
	  		}else 	if(pageNow == pageCount){
	  			for(int i = pageCount-4; i <= pageCount; i++){
	  				out.println("<a href= wel.jsp?pageTo="+i+"> ["+i+"] </a>");
	  			}
	  		}else 	if(pageNow == pageCount-1){
	  			for(int i = pageCount-3; i <= pageCount+1; i++){
	  				out.println("<a href= wel.jsp?pageTo="+i+"> ["+i+"] </a>");
	  			}
	  		}else{
	  			for(int i=pageNow - 2; i <= pageNow + 2; i++){
	  			out.println("<a href= wel.jsp?pageTo="+i+"> ["+i+"] </a>");
	  			}
	  		
	  		}
	  		
	  		//显示写一页
	  		if(pageNow != pageCount){
	  			out.println("<a href=wel.jsp?pageTo="+(pageNow + 1)+">下一页</a>");
	  		}
  			//末页
	  		out.println("<a href=wel.jsp?pageTo="+pageCount+">末页</a>");
  		
  		
  		
  		
  	
  	 %>
  </body>
</html>

 

你可能感兴趣的:(jsp)