1. 程式人生 > >S2jsp第三章 資料互動(二)

S2jsp第三章 資料互動(二)

 1.什麼是cookie?
      cookie是由伺服器埠生成,傳送給客戶端的瀏覽器的,瀏覽器會將其儲存在某個目錄下的文字檔案中
2.cookie作用?
   通過cookie,可以實現瀏覽器與伺服器之間的資料傳遞
3.session和cookie的異同?
  session與cookie均能實現資訊的儲存。
  seesion是在伺服器儲存使用者資訊
  session中儲存的是Object型別,cookie儲存的是String型別
  seession物件會隨時關閉而結束,cookie可以長期儲存在客戶端
  cookie通常用於不儲存不重要使用者資訊,重要的使用者資訊使用session儲存
<%@ 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>註冊頁面</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="do.jsp" method="post">
     使用者名稱:<input type="text" name="txtname" value="<%=request.getAttribute("name")%>"/>
    密碼: <input type="password" name="txtpwd"/>
   
  <input type="submit" value="提交"/>
     </form>
  </body>
</html>
<%@ 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>處理頁面</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>
  //session作用域
  <body>
   <%-- <%
           
     
       <% 
        String  name=request.getParameter("txtname");
        String pwd=request.getParameter("txtpwd");
        if("1".equals(name)&&"1".equals(pwd)){
        session.setAttribute("name", name);  
        request.getRequestDispatcher("index.jsp").forward(request, response);
        Cookie cook=new Cookie("name",name);
         Cookie cookpwd=new Cookie("pwd",pwd);
         response.addCookie(cook);
         response.addCookie(cookpwd);
        }else{
        response.sendRedirect("register.jsp");
        }
        %>
  </body>
</html>
<%@ 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 'index.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>
  <%-- 小指令碼:
   
 <%!
	boolean findyear(int year){

	if((year%4==0&&year%100!=0)||year%400==0){
		return true;
	}else{
	return false;
	}
 }
 %>
 <% 	
 int count=0;
	for(int i=2000;i<2014;i++){
		if(findyear(i)){
     		count++;
     		}
    }%>

從2000-2013年期間,一共有<%=count %>個閏年  --%>
 <%-- 歡迎<%=request.getAttribute("name")
   %> --%>
  歡迎 <%=session.getAttribute("name")%>
  <br/>
  <a href="/Day-0100/Login.jsp">登出</a>
  
  
  
  </body>
</html>


<%@page import="java.nio.channels.SeekableByteChannel"%>
<%@ 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>
   

  </head>
  
  <body>
  <% //清除session
   session.removeAttribute("name");
   //跳轉到登入頁面
   response.sendRedirect("/Day-0100/register.jsp");
	 %>
  </body>
</html>

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    
    
    <title>圖書列表</title>
    
	

  </head>
  
  <body>
    <%@ include file="pandau.jsp" %>
  </body>
</html>
<%@page import="java.nio.channels.SeekableByteChannel"%>
<%@ 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 'pandau.jsp' starting page</title>
   
	
  </head>
  
  <body>
   <% //判斷是否有session值
   Object uname=session.getAttribute("name");
   if(uname==null){
   response.sendRedirect("register.jsp");
   }
    %>
  </body>
</html>