1.cookie是什麼?html
cookie是web應用當中很是經常使用的一種技術,用於儲存某些特定的用戶信息。java
2.cookie的做用?web
在用戶登錄時將用戶的信息存放在cookie中,用戶在必定的時間中再次登錄時不須要輸入用戶名和密碼直接跳轉到下一個界面。瀏覽器
3.設置cookiecookie
Cookie cookie = new Cookie("key", "value");session
cookie.setMaxAge(saveTime*24*60*60);app
生存週期默認時間爲秒,若是設置爲負值的話,則爲瀏覽器進程Cookie(內存中保存),關閉瀏覽器就失效。jsp
cookie.setPath("/test/test2");ide
設置Cookie路徑,不設置的話爲當前路徑(對於Servlet來講爲request.getContextPath() + web.xml裏配置的該Servlet的url-pattern路徑部分) 。測試
response.addCookie(cookie);
4.讀取cookie
該方法能夠讀取當前路徑以及「直接父路徑」的全部Cookie對象,若是沒有任何Cookie的話,則返回null。若是設置了路徑使用這個方法也沒有值。
Cookie[] cookies = request.getCookies();
5.刪除cookie
Cookie cookie = new Cookie("key", null);
cookie.setMaxAge(0);
設置爲0爲當即刪除該Cookie;
cookie.setPath("/test/test2");
刪除指定路徑上的Cookie,不設置該路徑,默認爲刪除當前路徑Cookie;
response.addCookie(cookie);
下面看一個例子,這是模擬126郵箱登錄的小功能。創建一個名爲AutoLoginFilter的project,包結構以下:
![bao bao](http://static.javashuo.com/static/loading.gif)
project下有三個java文件、兩個jsp還有個html,
CheckLogin.java代碼以下:
public
class
CheckLogin
{
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
public static boolean login(String username, String password)
{
![](http://static.javashuo.com/static/loading.gif)
if ("admin".equals(username) && "123456".equals(password))
{
return true;
![](http://static.javashuo.com/static/loading.gif)
} else
{
return false;
}
}
![](http://static.javashuo.com/static/loading.gif)
}
![](http://static.javashuo.com/static/loading.gif)
IndexFilter.java代碼以下:
package
com.bx.course;
![](http://static.javashuo.com/static/loading.gif)
/** */
/**
* Filter能夠實現對請求的過濾和重定向等,也就是說能夠操做request和response,session等對象,listner只能監聽到以上對象的屬性的修改。
*/
![](http://static.javashuo.com/static/loading.gif)
import
java.io.IOException;
import
javax.servlet.Filter;
import
javax.servlet.FilterChain;
import
javax.servlet.FilterConfig;
import
javax.servlet.ServletException;
import
javax.servlet.ServletRequest;
import
javax.servlet.ServletResponse;
import
javax.servlet.http.Cookie;
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
![](http://static.javashuo.com/static/loading.gif)
public
class
IndexFilter
implements
Filter
{
![](http://static.javashuo.com/static/loading.gif)
public void destroy()
{
// TODO Auto-generated method stub
}
public void doFilter(ServletRequest arg0, ServletResponse arg1,
![](http://static.javashuo.com/static/loading.gif)
FilterChain arg2) throws IOException, ServletException
{
System.out.println("every request pass here haha");
HttpServletRequest request = (HttpServletRequest) arg0;
HttpServletResponse response = (HttpServletResponse) arg1;
Cookie[] cookies = request.getCookies();
// Cookie cookie = new Cookie("user",null);
// cookie.setMaxAge(0);
// response.addCookie(cookie);
String[] cooks = null;
String username = null;
String password = null;
![](http://static.javashuo.com/static/loading.gif)
if (cookies != null)
{
![](http://static.javashuo.com/static/loading.gif)
for (Cookie coo : cookies)
{
String aa = coo.getValue();
System.out.println("1");
cooks = aa.split("==");
![](http://static.javashuo.com/static/loading.gif)
if (cooks.length == 2)
{
System.out.println(cooks[0]+cooks[1]);
username = cooks[0];
password = cooks[1];
}
}
}
System.out.println("cookie username | " + username);
System.out.println("cookie password | " + password);
![](http://static.javashuo.com/static/loading.gif)
if (CheckLogin.login(username, password))
{
System.err.println("check successfully cookie data ");
request.getSession().setAttribute("username",username);
request.getRequestDispatcher("/main126.jsp").forward(request, response);
![](http://static.javashuo.com/static/loading.gif)
}else
{
arg2.doFilter(request,response );
}
}
![](http://static.javashuo.com/static/loading.gif)
public void init(FilterConfig arg0) throws ServletException
{
// TODO Auto-generated method stub
}
}
![](http://static.javashuo.com/static/loading.gif)
LoginServlet.java代碼以下:
package
com.bx.course;
import
java.io.IOException;
![](http://static.javashuo.com/static/loading.gif)
import
javax.servlet.ServletException;
import
javax.servlet.http.Cookie;
import
javax.servlet.http.HttpServlet;
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
![](http://static.javashuo.com/static/loading.gif)
public
class
LoginServlet
extends
HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response)
![](http://static.javashuo.com/static/loading.gif)
throws ServletException, IOException
{
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
![](http://static.javashuo.com/static/loading.gif)
throws ServletException, IOException
{
String username=request.getParameter("username");
String password=request.getParameter("password");
String savetime=request.getParameter("saveTime");
System.out.println("usrename "+username+" password "+password);
![](http://static.javashuo.com/static/loading.gif)
if(CheckLogin.login(username, password))
{
![](http://static.javashuo.com/static/loading.gif)
if(null!=savetime)
{
int saveTime=Integer.parseInt(savetime);//這裏接受的表單值爲天來計算的
int seconds=saveTime*24*60*60;
Cookie cookie = new Cookie("user", username+"=="+password);
cookie.setMaxAge(seconds);
response.addCookie(cookie);
}
request.setAttribute("username",username);
request.getRequestDispatcher("/main126.jsp").forward(request,response);
![](http://static.javashuo.com/static/loading.gif)
}else
{
request.getRequestDispatcher("/failure.jsp").forward(request,response);
}
}
}
![](http://static.javashuo.com/static/loading.gif)
web.xml配置文件代碼以下:
<
filter
>
<
filter
-
name
>
loginFilter
</
filter
-
name
>
<
filter
-
class
>
com.bx.course.IndexFilter
</
filter
-
class
>
</
filter
>
<
filter
-
mapping
>
<
filter
-
name
>
loginFilter
</
filter
-
name
>
<
url
-
pattern
>/
login.html
</
url
-
pattern
>
</
filter
-
mapping
>
<
servlet
>
<
servlet
-
name
>
LoginServlet
</
servlet
-
name
>
<
servlet
-
class
>
com.bx.course.LoginServlet
</
servlet
-
class
>
</
servlet
>
![](http://static.javashuo.com/static/loading.gif)
<
servlet
-
mapping
>
<
servlet
-
name
>
LoginServlet
</
servlet
-
name
>
<
url
-
pattern
>/
login.
do
</
url
-
pattern
>
</
servlet
-
mapping
>
<
welcome
-
file
-
list
>
<
welcome
-
file
>
login.html
</
welcome
-
file
>
</
welcome
-
file
-
list
>
![](http://static.javashuo.com/static/loading.gif)
<
welcome
-
file
-
list
>
<
welcome
-
file
>
main126.jsp
</
welcome
-
file
>
</
welcome
-
file
-
list
>
![](http://static.javashuo.com/static/loading.gif)
login.html代碼以下:
<
html
>
<
head
>
</
head
>
<
body
>
<
form
action
="login.do"
>
126郵箱登陸
<
br
/><
br
/>
用戶名:
<
input
type
="text"
name
="username"
><
br
/>
密 碼:
<
input
type
="text"
name
="password"
><
br
/>
<
select
name
="saveTime"
>
<
option
value
="366"
>
一年
</
option
>
<
option
value
="183"
>
半年
</
option
>
<
option
value
="30"
>
一個月
</
option
>
<
option
value
="7"
>
一週
</
option
>
</
select
><
br
/>
<
input
type
="submit"
value
="登陸"
/>
</
form
>
</
body
>
</
html
>
![](http://static.javashuo.com/static/loading.gif)
main126.jsp代碼以下:
<
html
>
<
head
>
<
title
>
測試cookie
</
title
>
</
head
>
<
body
>
<
h2
>
登陸成功,歡迎${username}的到來 126郵箱
</
h2
><
br
/>
<
h3
>
測試cookie的功能
</
body
>
</
html
>
![](http://static.javashuo.com/static/loading.gif)
failure.jsp代碼以下:
<
body
>
Login failure
<
br
>
</
body
>
運行效果:
在IE地址欄中輸入http://localhost:8080/LoginFilter/login.html
顯示以下界面:
![2 2](http://static.javashuo.com/static/loading.gif)
輸入用戶名:admin 密碼:123456 選擇保存時間,而後點擊登錄,就會進入如下界面:
![3 3](http://static.javashuo.com/static/loading.gif)
在保存時間內再次在IE地址欄中輸入:http://localhost:8080/LoginFilter/login.html
就會直接進入登錄成功界面。若是用戶名或密碼不正確則會進入如下界面:
![4 4](http://static.javashuo.com/static/loading.gif)