我是靠谱客的博主 甜甜路灯,最近开发中收集的这篇文章主要介绍java cookie路径_路径问题以及cookie详解,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

1.路径问题:

注意 .代表执行程序的文件夹路径,在tomcat中也就是bin目录,所以要用this.getServletContext().getRealPath("/WEB-INF/classes/db.properties");得到绝对路径;

代码练习:

packagecom.http.path;importjava.io.File;importjava.io.FileInputStream;importjava.io.IOException;importjava.io.PrintWriter;importjava.util.Properties;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;public class PathDemo extendsHttpServlet {publicPathDemo() {super();

}public voiddestroy() {super.destroy(); //Just puts "destroy" string in log//Put your code here

}public voiddoGet(HttpServletRequest request, HttpServletResponse response)throwsServletException, IOException {

response.setCharacterEncoding("utf-8");

request.setCharacterEncoding("utf-8");//给服务器使用的: / 表示在当前web应用的根目录(webRoot下)//request.getRequestDispatcher("/target.html").forward(request, response);//给浏览器使用的: / 表示在webapps的根目录下//response.sendRedirect("/MyWeb/target.html");

String path= this.getServletContext().getRealPath("/WEB-INF/classes/db.properties");//this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");

System.out.println(path);

Properties properties= newProperties();

properties.load(new FileInputStream(newFile(path)));

String user= properties.getProperty("user");

String passwd= properties.getProperty("passwd");

System.out.println("user = " + user + "npasswd = " +passwd);

}public voiddoPost(HttpServletRequest request, HttpServletResponse response)throwsServletException, IOException {

response.setContentType("text/html");

PrintWriter out=response.getWriter();

out.println(""-//W3C//DTD HTML 4.01 Transitional//EN">");

out.println("");

out.println("

A Servlet");

out.println("

");

out.print(" This is ");

out.print(this.getClass());

out.println(", using the POST method");

out.println(" ");

out.println("");

out.flush();

out.close();

}public void init() throwsServletException {//Put your code here

}

}

2.Cooke技术

2.1 特点

Cookie技术:会话数据保存在浏览器客户端。

2.2 Cookie技术核心

Cookie类:用于存储会话数据

1)构造Cookie对象

Cookie(java.lang.String name, java.lang.String value)

2)设置cookie

void setPath(java.lang.String uri)   :设置cookie的有效访问路径

void setMaxAge(int expiry) : 设置cookie的有效时间

void setValue(java.lang.String newValue) :设置cookie的值

3)发送cookie到浏览器端保存

void response.addCookie(Cookie cookie)  : 发送cookie

4)服务器接收cookie

Cookie[] request.getCookies()  : 接收cookie

2.3 Cookie原理

1)服务器创建cookie对象,把会话数据存储到cookie对象中。

new Cookie("name","value");

2) 服务器发送cookie信息到浏览器

response.addCookie(cookie);

举例: set-cookie: name=eric  (隐藏发送了一个set-cookie名称的响应头)

3)浏览器得到服务器发送的cookie,然后保存在浏览器端。

4)浏览器在下次访问服务器时,会带着cookie信息

举例: cookie: name=eric  (隐藏带着一个叫cookie名称的请求头)

5)服务器接收到浏览器带来的cookie信息

request.getCookies();

2.4 Cookie的细节

1)void setPath(java.lang.String uri)   :设置cookie的有效访问路径。有效路径指的是cookie的有效路径保存在哪里,那么浏览器在有效路径下访问服务器时就会带着cookie信息,否则不带cookie信息。

2)void setMaxAge(int expiry) : 设置cookie的有效时间。

正整数:表示cookie数据保存浏览器的缓存目录(硬盘中),数值表示保存的时间。

负整数:表示cookie数据保存浏览器的内存中。浏览器关闭cookie就丢失了!!

零:表示删除同名的cookie数据

3)Cookie数据类型只能保存非中文字符串类型的。可以保存多个cookie,但是浏览器一般只允许存放300个Cookie,每个站点最多存放20个Cookie,每个Cookie的大小限制为4KB。

在下面查了下,cookie也可以存中文字符串,可以用URLEncoder类中的encode方法编码,然后用URLDecoder.decode方法解码

cookie的简单练习:

packagecom.http.cookie;importjava.io.IOException;importjava.io.PrintWriter;importjavax.servlet.ServletException;importjavax.servlet.http.Cookie;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;public class CookieDemo1 extendsHttpServlet {/*** Constructor of the object.*/

publicCookieDemo1() {super();

}/*** Destruction of the servlet.
*/

public voiddestroy() {super.destroy(); //Just puts "destroy" string in log//Put your code here

}/*** The doGet method of the servlet.

*

* This method is called when a form has its tag value method equals to get.

*

*@paramrequest the request send by the client to the server

*@paramresponse the response send by the server to the client

*@throwsServletException if an error occurred

*@throwsIOException if an error occurred*/

public voiddoGet(HttpServletRequest request, HttpServletResponse response)throwsServletException, IOException {

Cookie cookie= new Cookie("name", "handsomecui");

Cookie cookie2= new Cookie("email", "handsomecui@qq.com");

cookie2.setPath("/Test");//cookie.setMaxAge(10);//不会受到浏览器关闭的影响

cookie.setMaxAge(-1);//cookie保存在浏览器内存,关闭移除//cookie.setMaxAge(0);//删除同名cookie

response.addCookie(cookie);

response.addCookie(cookie2);

System.out.println(request.getHeader("cookie"));

Cookie[] cookies=request.getCookies();if(cookies == null){

System.out.println("没有cookie");

}else{for(Cookie acookie : cookies){

System.out.println(acookie.getName()+ "是" +acookie.getValue());

}

}

}/*** The doPost method of the servlet.

*

* This method is called when a form has its tag value method equals to post.

*

*@paramrequest the request send by the client to the server

*@paramresponse the response send by the server to the client

*@throwsServletException if an error occurred

*@throwsIOException if an error occurred*/

public voiddoPost(HttpServletRequest request, HttpServletResponse response)throwsServletException, IOException {

response.setContentType("text/html");

PrintWriter out=response.getWriter();

out.println(""-//W3C//DTD HTML 4.01 Transitional//EN">");

out.println("");

out.println("

A Servlet");

out.println("

");

out.print(" This is ");

out.print(this.getClass());

out.println(", using the POST method");

out.println(" ");

out.println("");

out.flush();

out.close();

}/*** Initialization of the servlet.

*

*@throwsServletException if an error occurs*/

public void init() throwsServletException {//Put your code here

}

}

2.5 案例-显示用户上次访问的时间

代码:

packagecom.http.cookie;importjava.io.IOException;importjava.io.PrintWriter;importjava.sql.Time;importjava.text.SimpleDateFormat;importjava.util.Date;importjavax.servlet.ServletException;importjavax.servlet.http.Cookie;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;public class ShowTime extendsHttpServlet {/*** Constructor of the object.*/

private int cnt = 0;publicShowTime() {super();

}/*** Destruction of the servlet.
*/

public voiddestroy() {super.destroy(); //Just puts "destroy" string in log//Put your code here

}privateString Isexist(HttpServletRequest request){

Cookie[] cookies=request.getCookies();if(cookies == null){return null;

}for(Cookie acookie:cookies){if("lasttime".equals(acookie.getName())){returnacookie.getValue();

}

}return null;

}public voiddoGet(HttpServletRequest request, HttpServletResponse response)throwsServletException, IOException {

cnt++;

response.setCharacterEncoding("gb2312");

request.setCharacterEncoding("gb2312");

response.getWriter().write("您好,这是您第" + cnt + "次访问本站n");

SimpleDateFormat format= new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

String str= format.format(newDate());

Cookie cookie= new Cookie("lasttime", str);

response.addCookie(cookie);

String lasttime=Isexist(request);if(lasttime != null){

response.getWriter().write("您上次访问的时间是:" + lasttime + "n");

}else{

}

response.getWriter().write("当前时间是:" + str + "n");

}/*** The doPost method of the servlet.

*

* This method is called when a form has its tag value method equals to post.

*

*@paramrequest the request send by the client to the server

*@paramresponse the response send by the server to the client

*@throwsServletException if an error occurred

*@throwsIOException if an error occurred*/

public voiddoPost(HttpServletRequest request, HttpServletResponse response)throwsServletException, IOException {

response.setContentType("text/html");

PrintWriter out=response.getWriter();

out.println(""-//W3C//DTD HTML 4.01 Transitional//EN">");

out.println("");

out.println("

A Servlet");

out.println("

");

out.print(" This is ");

out.print(this.getClass());

out.println(", using the POST method");

out.println(" ");

out.println("");

out.flush();

out.close();

}/*** Initialization of the servlet.

*

*@throwsServletException if an error occurs*/

public void init() throwsServletException {//Put your code here

}

}

2.6 案例-查看用户浏览器过的商品

思路:ListServlet展示商品列表,Detailservlet展示商品的详细信息,最近浏览的商品,用一个hashset存储MySet类,取出前三个不重复的即可,cookie存放最近浏览的商品编号,用逗号隔开;

product类:

packagecom.common.product;importjava.util.ArrayList;public classProduct {private static ArrayListarrayList;private intid;privateString name;privateString type;private doubleprice;static{

arrayList= new ArrayList();for(int i = 0; i < 10; i++){

Product product= new Product(i + 1, "笔记本"+(i + 1), "LN00"+(i+1), 35+i);

arrayList.add(product);

}

}public static ArrayListgetArrayList() {returnarrayList;

}

@OverridepublicString toString() {return "id=" + id + ", name=" + name + ", type=" +type+ ", price=" +price ;

}public Product(int id, String name, String type, doubleprice) {super();this.id =id;this.name =name;this.type =type;this.price =price;

}publicProduct() {super();

}public intgetId() {returnid;

}public void setId(intid) {this.id =id;

}publicString getName() {returnname;

}public voidsetName(String name) {this.name =name;

}publicString getType() {returntype;

}public voidsetType(String type) {this.type =type;

}public doublegetPrice() {returnprice;

}public void setPrice(doubleprice) {this.price =price;

}public Product getValueById(intp) {for(int i = 0; i < arrayList.size(); i++){if(arrayList.get(i).getId() ==p){returnarrayList.get(i);

}

}return null;

}

}

MySet类:由编号和商品名构成,主要是为了hashset的去重与排序;

packagecom.common.tool;importjava.util.HashSet;public classMySet{private intid;privateString name;public intgetId() {returnid;

}

@Overridepublic booleanequals(Object o) {//TODO Auto-generated method stub

MySet myset =(MySet)o;return myset.getName().equals(this.getName());

}

@Overridepublic inthashCode() {//TODO Auto-generated method stub

returnid;

}public void setId(intid) {this.id =id;

}publicString getName() {returnname;

}public voidsetName(String name) {this.name =name;

}public MySet(intid, String name) {super();this.id =id;this.name =name;

}publicMySet() {super();

}

}

ListServlet:

packagecom.http.servlet;importjava.io.IOException;importjava.io.PrintWriter;importjava.net.URLDecoder;importjava.net.URLEncoder;importjava.util.ArrayList;importjavax.servlet.ServletException;importjavax.servlet.http.Cookie;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importcom.common.product.Product;public class ListServlet extendsHttpServlet {/*** Constructor of the object.*/

publicListServlet() {super();

}/*** Destruction of the servlet.
*/

public voiddestroy() {super.destroy(); //Just puts "destroy" string in log//Put your code here

}/*** The doGet method of the servlet.

*

* This method is called when a form has its tag value method equals to get.

*

*@paramrequest the request send by the client to the server

*@paramresponse the response send by the server to the client

*@throwsServletException if an error occurred

*@throwsIOException if an error occurred*/

public voiddoGet(HttpServletRequest request, HttpServletResponse response)throwsServletException, IOException {

request.setCharacterEncoding("utf-8");

response.setCharacterEncoding("utf-8");

Product product= newProduct();

String html= "";

html+=("");

html+=("

");

html+=("");

html+=("

");

html+=("商品列表");

html+=("

");

html+=("");

html+=("

");

html+=("

html+=("

");

html+=("

");

html+=("编号");

html+=("

");

html+=("

");

html+=("商品名称");

html+=("

");

html+=("

");

html+=("商品型号");

html+=("

");

html+=("

");

html+=("商品价格");

html+=("

");

html+=("

");

ArrayList list = newProduct().getArrayList();for(int i = 0; i < list.size(); i++){

html+=("

");

html+=("

");

html+=("" +list.get(i).getId());

html+=("

");

html+=("

");

html+=("" + list.get(i).getName() + "");

html+=("

");

html+=("

");

html+=(list.get(i).getType());

html+=("

");

html+=("

");

html+=("" +list.get(i).getPrice());

html+=("

");

html+=("

");

}

html+=("

");

html+=("最近浏览过的商品: " + "
");

String recentview= null;

Cookie[] cookies=request.getCookies();if(cookies != null){for(Cookie cookie : cookies){if("RecentProduct".equals(cookie.getName())){

recentview=cookie.getValue();break;

}

}

}if(recentview != null){

recentview= URLDecoder.decode(recentview, "UTF-8");

String[] splits= recentview.split(",");for(String p : splits){

html+= (product.getValueById(Integer.parseInt(p)) + "
");

}

}

html+=("");

html+=("");

response.getWriter().write(html);

}/*** The doPost method of the servlet.

*

* This method is called when a form has its tag value method equals to post.

*

*@paramrequest the request send by the client to the server

*@paramresponse the response send by the server to the client

*@throwsServletException if an error occurred

*@throwsIOException if an error occurred*/

public voiddoPost(HttpServletRequest request, HttpServletResponse response)throwsServletException, IOException {

response.setContentType("text/html");

PrintWriter out=response.getWriter();

out.println(""-//W3C//DTD HTML 4.01 Transitional//EN">");

out.println("");

out.println("

A Servlet");

out.println("

");

out.print(" This is ");

out.print(this.getClass());

out.println(", using the POST method");

out.println(" ");

out.println("");

out.flush();

out.close();

}/*** Initialization of the servlet.

*

*@throwsServletException if an error occurs*/

public void init() throwsServletException {//Put your code here

}

}

DetailServlet

packagecom.http.servlet;importjava.io.IOException;importjava.io.PrintWriter;importjava.net.URLEncoder;importjava.util.HashSet;importjava.util.Iterator;importjava.util.Set;importjava.util.Stack;importjava.net.URLEncoder;importjavax.servlet.ServletException;importjavax.servlet.http.Cookie;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importcom.common.product.Product;importcom.common.tool.MySet;

@SuppressWarnings("unused")public class DetailServlet extendsHttpServlet {/*** Constructor of the object.*/

publicDetailServlet() {super();

}/*** Destruction of the servlet.
*/

public voiddestroy() {super.destroy(); //Just puts "destroy" string in log//Put your code here

}/*** The doGet method of the servlet.

*

* This method is called when a form has its tag value method equals to get.

*

*@paramrequest the request send by the client to the server

*@paramresponse the response send by the server to the client

*@throwsServletException if an error occurred

*@throwsIOException if an error occurred*/

public voiddoGet(HttpServletRequest request, HttpServletResponse response)throwsServletException, IOException {

request.setCharacterEncoding("utf-8");

response.setCharacterEncoding("utf-8");int id = Integer.parseInt(request.getParameter("id"));

System.out.println(id);

Product product= newProduct().getValueById(id);

String html= "";

html+=("");

html+=("

");

html+=("");

html+=("

");

html+=("商品信息");

html+=("

");

html+=("");

html+=("

");

html+=("

html+=("

");

html+=("

");

html+=("编号");

html+=("

");

html+=("

");

html+=(id);

html+=("

");

html+=("

");

html+=("

");

html+=("

");

html+=("商品名称");

html+=("

");

html+=("

");

html+=(product.getName());

html+=("

");

html+=("

");

html+=("

");

html+=("

");

html+=("商品型号");

html+=("

");

html+=("

");

html+=(product.getType());

html+=("

");

html+=("

");

html+=("

");

html+=("

");

html+=("商品价格");

html+=("

");

html+=("

");

html+=(product.getPrice());

html+=("

");

html+=("

");

html+=("");

html+=("");

Cookie acookie= null;

Cookie[] cookies=request.getCookies();if(cookies != null){for(Cookie cookie : cookies){if("RecentProduct".equals(cookie.getName())){

acookie=cookie;break;

}

}

}

System.out.println(product.getName());if(acookie == null){

acookie= new Cookie("RecentProduct", product.getId()+"");

response.addCookie(acookie);

}else{

String[] splits= acookie.getValue().split(",");

HashSet set = new HashSet();

set.add(new MySet(0, product.getId()+""));for(int i = 0, j = 0; i < splits.length;i++){if((product.getId()+"").equals(splits[i]))continue;

j++;

set.add(newMySet(j, splits[i]));

}

String value= "";

Iterator iterator =set.iterator();for(int i = 0; iterator.hasNext()&& i < 3; i++){

MySet mySet=iterator.next();

value+= (mySet.getName() + ",");

}

acookie= new Cookie("RecentProduct", value);

response.addCookie(acookie);

}

response.getWriter().write(html);

}/*** The doPost method of the servlet.

*

* This method is called when a form has its tag value method equals to post.

*

*@paramrequest the request send by the client to the server

*@paramresponse the response send by the server to the client

*@throwsServletException if an error occurred

*@throwsIOException if an error occurred*/

public voiddoPost(HttpServletRequest request, HttpServletResponse response)throwsServletException, IOException {

response.setContentType("text/html");

PrintWriter out=response.getWriter();

out.println(""-//W3C//DTD HTML 4.01 Transitional//EN">");

out.println("");

out.println("

A Servlet");

out.println("

");

out.print(" This is ");

out.print(this.getClass());

out.println(", using the POST method");

out.println(" ");

out.println("");

out.flush();

out.close();

}/*** Initialization of the servlet.

*

*@throwsServletException if an error occurs*/

public void init() throwsServletException {//Put your code here

}

}

4 Session技术

4.1 引入

Cookie的局限:

1)Cookie只能存字符串类型。不能保存对象

2)只能存非中文。

3)1个Cookie的容量不超过4KB。

如果要保存非字符串,超过4kb内容,只能使用session技术!!!

Session特点:

会话数据保存在服务器端。(内存中)

4.2 Session技术核心

HttpSession类:用于保存会话数据

1)创建或得到session对象

HttpSession getSession()

HttpSession getSession(boolean create)

2)设置session对象

void setMaxInactiveInterval(int interval)  : 设置session的有效时间

void invalidate()     : 销毁session对象

java.lang.String getId()  : 得到session编号

3)保存会话数据到session对象

void setAttribute(java.lang.String name, java.lang.Object value)  : 保存数据

java.lang.Object getAttribute(java.lang.String name)  : 获取数据

void removeAttribute(java.lang.String name) : 清除数据

4.3 Session原理

问题: 服务器能够识别不同的浏览者!!!

现象:

前提: 在哪个session域对象保存数据,就必须从哪个域对象取出!!!!

浏览器1:(给s1分配一个唯一的标记:s001,把s001发送给浏览器)

1)创建session对象,保存会话数据

HttpSession session = request.getSession();   --保存会话数据s1

浏览器1 的新窗口(带着s001的标记到服务器查询,s001->s1,返回s1)

1)得到session对象的会话数据

HttpSession session = request.getSession();   --可以取出s1

新的浏览器1:(没有带s001,不能返回s1)

1)得到session对象的会话数据

HttpSession session = request.getSession();   --不可以取出s2

浏览器2:(没有带s001,不能返回s1)

1)得到session对象的会话数据

HttpSession session = request.getSession();  --不可以取出s3

代码解读:HttpSession session = request.getSession();

1)第一次访问创建session对象,给session对象分配一个唯一的ID,叫JSESSIONID

new HttpSession();

2)把JSESSIONID作为Cookie的值发送给浏览器保存

Cookie cookie = new Cookie("JSESSIONID", sessionID);

response.addCookie(cookie);

3)第二次访问的时候,浏览器带着JSESSIONID的cookie访问服务器

4)服务器得到JSESSIONID,在服务器的内存中搜索是否存放对应编号的session对象。

if(找到){

return map.get(sessionID);

}

Map]

5)如果找到对应编号的session对象,直接返回该对象

6)如果找不到对应编号的session对象,创建新的session对象,继续走1的流程

结论:通过JSESSION的cookie值在服务器找session对象!!!!!

4.4 Sesson细节

1)java.lang.String getId()  : 得到session编号

2)两个getSession方法:

getSession(true) / getSession()  : 创建或得到session对象。没有匹配的session编号,自动创 建新的session对象。

getSession(false):              得到session对象。没有匹配的session编号,返回null

3)void setMaxInactiveInterval(int interval)  : 设置session的有效时间

session对象销毁时间:

3.1 默认情况30分服务器自动回收

3.2 修改session回收时间

3.3 全局修改session有效时间

1

3.4.手动销毁session对象

void invalidate()     : 销毁session对象

4)如何避免浏览器的JSESSIONID的cookie随着浏览器关闭而丢失的问题

/**

* 手动发送一个硬盘保存的cookie给浏览器

*/

Cookie c = new Cookie("JSESSIONID",session.getId());

c.setMaxAge(60*60);

response.addCookie(c);

代码SessionDemo1

packagecom.http.cookie;importjava.io.IOException;importjava.io.PrintWriter;importjava.net.URLEncoder;importjavax.servlet.ServletException;importjavax.servlet.http.Cookie;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importjavax.servlet.http.HttpSession;public class SessionDemo1 extendsHttpServlet {/*** Constructor of the object.*/

publicSessionDemo1() {super();

}/*** Destruction of the servlet.
*/

public voiddestroy() {super.destroy(); //Just puts "destroy" string in log//Put your code here

}/*** The doGet method of the servlet.

*

* This method is called when a form has its tag value method equals to get.

*

*@paramrequest the request send by the client to the server

*@paramresponse the response send by the server to the client

*@throwsServletException if an error occurred

*@throwsIOException if an error occurred*/

public voiddoGet(HttpServletRequest request, HttpServletResponse response)throwsServletException, IOException {

HttpSession session=request.getSession();

System.out.println("id = "+session.getId());

session.setAttribute("name", "rose");

session.setMaxInactiveInterval(60*60);

Cookie cookie= new Cookie("JSESSIONID", session.getId());

cookie.setMaxAge(60*60);

response.addCookie(cookie);

}/*** The doPost method of the servlet.

*

* This method is called when a form has its tag value method equals to post.

*

*@paramrequest the request send by the client to the server

*@paramresponse the response send by the server to the client

*@throwsServletException if an error occurred

*@throwsIOException if an error occurred*/

public voiddoPost(HttpServletRequest request, HttpServletResponse response)throwsServletException, IOException {

response.setContentType("text/html");

PrintWriter out=response.getWriter();

out.println(""-//W3C//DTD HTML 4.01 Transitional//EN">");

out.println("");

out.println("

A Servlet");

out.println("

");

out.print(" This is ");

out.print(this.getClass());

out.println(", using the POST method");

out.println(" ");

out.println("");

out.flush();

out.close();

}/*** Initialization of the servlet.

*

*@throwsServletException if an error occurs*/

public void init() throwsServletException {//Put your code here

}

}

SessionDemo2:

packagecom.http.cookie;importjava.io.IOException;importjava.io.PrintWriter;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importjavax.servlet.http.HttpSession;public class SessionDemo2 extendsHttpServlet {/*** Constructor of the object.*/

publicSessionDemo2() {super();

}/*** Destruction of the servlet.
*/

public voiddestroy() {super.destroy(); //Just puts "destroy" string in log//Put your code here

}/*** The doGet method of the servlet.

*

* This method is called when a form has its tag value method equals to get.

*

*@paramrequest the request send by the client to the server

*@paramresponse the response send by the server to the client

*@throwsServletException if an error occurred

*@throwsIOException if an error occurred*/

public voiddoGet(HttpServletRequest request, HttpServletResponse response)throwsServletException, IOException {

HttpSession session= request.getSession(false);if(session != null){

System.out.println(session.getAttribute("name"));

}

}/*** The doPost method of the servlet.

*

* This method is called when a form has its tag value method equals to post.

*

*@paramrequest the request send by the client to the server

*@paramresponse the response send by the server to the client

*@throwsServletException if an error occurred

*@throwsIOException if an error occurred*/

public voiddoPost(HttpServletRequest request, HttpServletResponse response)throwsServletException, IOException {

response.setContentType("text/html");

PrintWriter out=response.getWriter();

out.println(""-//W3C//DTD HTML 4.01 Transitional//EN">");

out.println("");

out.println("

A Servlet");

out.println("

");

out.print(" This is ");

out.print(this.getClass());

out.println(", using the POST method");

out.println(" ");

out.println("");

out.flush();

out.close();

}/*** Initialization of the servlet.

*

*@throwsServletException if an error occurs*/

public void init() throwsServletException {//Put your code here

}

}

DeleteSession

packagecom.http.cookie;importjava.io.IOException;importjava.io.PrintWriter;importjavax.servlet.ServletException;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;importjavax.servlet.http.HttpSession;public class DeleteSession extendsHttpServlet {/*** The doGet method of the servlet.

*

* This method is called when a form has its tag value method equals to get.

*

*@paramrequest the request send by the client to the server

*@paramresponse the response send by the server to the client

*@throwsServletException if an error occurred

*@throwsIOException if an error occurred*/

public voiddoGet(HttpServletRequest request, HttpServletResponse response)throwsServletException, IOException {

HttpSession session= request.getSession(false);if(session != null){

session.invalidate();

System.out.println("销毁成功");

}

}

}

最后

以上就是甜甜路灯为你收集整理的java cookie路径_路径问题以及cookie详解的全部内容,希望文章能够帮你解决java cookie路径_路径问题以及cookie详解所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(53)

评论列表共有 0 条评论

立即
投稿
返回
顶部