jsp/JavaScript實現自動登錄功能和統計頁面瀏覽次數

完成目標:利用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"); 
     }
}
%>檢查界面代碼(連接數據庫):
<%@ 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();
}
%>

<base charset="utf-8" content="no-cache" href="&lt;%=basePath%&gt;" http-equiv="cache-control" meta="" />
<meta content="0" http-equiv="expires" />
<meta content="keyword1,keyword2,keyword3" http-equiv="keywords" />
<meta content="This is my page" http-equiv="description" />
<script type="text/javascript" src="js/jquery-1.4.2.min.js"></script>

檢查頁面

效果:(輸入正確的用戶名和密碼,並且勾選自動登錄,會跳轉到檢查界面。返回登陸界面後刷新,兩秒後會自動跳轉到第三個界面,即登錄成功界面)


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+"/";
%>

<base charset="utf-8" content="no-cache" href="&lt;%=basePath%&gt;" http-equiv="pragma" meta="" session="" />
<meta content="no-cache" http-equiv="cache-control" />
<meta content="0" http-equiv="expires" />
<meta content="keyword1,keyword2,keyword3" http-equiv="keywords" />
<meta content="This is my page" http-equiv="description" />
<script type="text/javascript" src="../js/jquery-1.4.2.min.js"></script><script>
       function tijiao(){
            document.baseform.submit();
        }  
      </script>
<form action="session/check.jsp" method="post" name="baseform">
	<input name="user" placeholder="請輸入用戶名" type="text" /> <input name="pwd" placeholder="請輸入密碼" type="password" /> <input name="bt1" onclick="tijiao()" type="button" value="登錄" />&nbsp;</form>

檢查畫面代碼:(不連接數據庫,僅用做測試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+"/";
%>


<script>
function tijiao(){
window.location.href=”application/main.jsp”;
}
</script> 檢查界面代碼:

<%@ 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);
   }
%>


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。多次登錄後瀏覽量會隨次數增加)

發佈留言