JavaScript/Jsp 实现自动登录功能和统计页面浏览次数

完成目标:利用Cookie方法完成自动登录功能,并且掌握session,include基本用法。

1.自动登录功能

登录界面代码:

<%@ page language="java" import="java.util.*" 
pageEncoding="utf-8" contentType="text/html;charset:utf-8"%>
<%@ page import="javax.servlet.http.Cookie.*" %>
<%@page import="java.net.URLDecoder"%>
<%@page import="java.net.URLEncoder"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
String cookieFlag="0";
String name="";
String pwd="";
Cookie[] cookies=request.getCookies();
for(Cookie c:cookies){
     if(c.getName().equals("jredu")){
     String temp=URLDecoder.decode(c.getValue(),"utf-8");
     String []temps=temp.split(";");
     name=temps[0];
     pwd=temps[1];
     cookieFlag="1";
     //延时两秒跳转到main.jsp
     response.setHeader("REFRESH","2;url=main.jsp"); 
     }
}
%>


  
    
    
    cookie登录界面
    
	
	
	    
	
	
	
    
  
  
  
    

帐号登录

用户:>
密码:>
忘记密码
下次自动登录
检查界面代码(连接数据库):

<%@ page language="java" contentType="text/html;charset=utf-8"%>
<%@ page import="java.util.*"%>
<%@ page import="java.text.*"%>
<%@ page import="java.sql.*" %>
<%@ page import="com.mysql.jdbc.Driver" %>
<%@page import="java.net.URLDecoder"%>
<%@page import="java.net.URLEncoder"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
String cookieFlag="0";
String flag="0";
//驱动程序名 
	String driverName = "com.mysql.jdbc.Driver";
	//数据库用户名 
	 String userName   = "root";
	//密码 
	 String userPasswd = "ffffff";
	//数据库名 
	String dbName     = "shcoolapp";
	//联结字符串
	String url = "jdbc:mysql://localhost/" + dbName 
	                        + "?user="+ userName 
	                        + "&password=" + userPasswd
	                        + "&useUnicode=true&characterEncoding=gbk";
	request.setCharacterEncoding("utf-8");
	String uName=request.getParameter("uname");
    String upwd=request.getParameter("upwd");                        
    String login=request.getParameter("login");  
try{
  Class.forName(driverName);
  Connection con=DriverManager.getConnection(url);
  Statement stmt=con.createStatement();
  ResultSet rs=stmt.executeQuery("SELECT * FROM users where user_name='"+uName+"' and pwd='"+upwd+"'");
  out.println(uName);
  out.println(upwd);
  if(rs.next()){
     cookieFlag="1";
     String dName=rs.getString("DISPLAY_NAME");
     if(login.equals("1")){
     Cookie newCk=new Cookie("jredu",URLEncoder.encode(uName+";"+upwd,"utf-8"));
     newCk.setMaxAge(60);
     response.addCookie(newCk);
     out.println("cookie写入完成");
     }
    }else{
     response.setHeader("REFRESH","4;url=cookielogin.jsp"); 
     }
  con.close();
}catch(Exception e){
  e.printStackTrace();
}
%>


  
    
    
    检查页面
	
	
	    
	
	
	
    
  
   
  

检查页面

效果:(输入正确的用户名和密码,并且勾选自动登录,会跳转到检查界面。返回登陆界面后刷新,两秒后会自动跳转到第三个界面,即登录成功界面)

JavaScript/Jsp 实现自动登录功能和统计页面浏览次数_第1张图片JavaScript/Jsp 实现自动登录功能和统计页面浏览次数_第2张图片
2.掌握利用session获取数据
登录画面代码:

<%@ page language="java" import="java.util.*" 
pageEncoding="utf-8" contentType="text/html;charset:utf-8"%>

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>


  
    
    session
    
	
	
	    
	
	
	
      
  
  
  
    
检查画面代码:(不连接数据库,仅用做测试session传参所用)

<%@ page language="java" import="java.util.*" 
pageEncoding="utf-8" contentType="text/html;charset:utf-8"%>

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

request.setCharacterEncoding("utf-8");
String user=request.getParameter("user");
String pwd=request.getParameter("pwd");
Object userInfo=session.getAttribute("userInfo");
if(userInfo==null){
    session.setAttribute("userInfo", user+","+pwd);
};
response.sendRedirect("main.jsp");
%>


  
    
    检查
    
	
	
	    
	
	
  
  
  
    session检查界面
  

效果:


3.获取网页浏览次数

登录界面代码(测试效果,所以简单的只写了一个登录按钮作为提交):

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


  
    
    
    application登录
    
	
	
	    
	
	
	
  
  
  
    
  

检查界面代码:

<%@ page language="java" import="java.util.*" 
pageEncoding="utf-8" contentType="text/html;charset:utf-8"%>
<%

String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

   Object obj=application.getAttribute("loginUser");
   if(obj==null){
   application.setAttribute("loginUser", "456");
   }else{
     int count=Integer.parseInt(obj.toString());
     application.setAttribute("loginUser", count+1);
   }
%>


  
        
    application检查界面   
    
	
	
	    
	
	
  
  
  
    This is my JSP page. 
显示界面代码:

<%@ page language="java" import="java.util.*" 
pageEncoding="utf-8" contentType="text/html;charset:utf-8"%>
<%@ include file="include.jsp" %>
<%@ page  import="com.jredu.java.User"%>


  
        
    显示
    
	
	
	    
	
	
  
  
  
         浏览量:<%=currentcount%>
  

效果:(设定页面初始值为456。多次登录后浏览量会随次数增加)






你可能感兴趣的:(Java,Script)