本文共 10128 字,大约阅读时间需要 33 分钟。
jsp servlet示例
Welcome to Java Servlet Cookies example. Cookies are used a lot in web client-server communication, it’s not something specific to java.
欢迎使用Java Servlet Cookies示例。 Cookie在Web客户端与服务器之间的通信中被大量使用,它不是Java特有的。
Some of the common usage of cookies are:
Cookies的一些常见用法是:
Cookies是服务器发送到客户端的文本数据,并保存在客户端本地计算机上。 客户端将请求发送到服务器时,它将服务器存储的cookie传递到请求标头中,如下所示:
Cookie Test="Test Cookie5"
Client can send multiple cookies to server and we can disable cookies to get stored at client side from browser preferences. Apart from the key-value pairs, server sends some other data to client in response header and it looks something like below.
客户端可以向服务器发送多个cookie,并且我们可以禁用cookie以根据浏览器首选项存储在客户端。 除了键值对之外,服务器还会在响应标头中向客户端发送其他一些数据,如下所示。
Set-Cookie Counter=7;Version=1;Comment="SetCookie Counter";Domain="localhost";Max-Age=86400;Expires=Thu, 15-Aug-2013 20:19:19 GMT;Path=/cookie/SetCookieSet-Cookie Test="Test Cookie7";Version=1;Comment="Test Cookie"
Note that server sends some additional information for cookie, such as comment, domain, maximum time before cookie expires and Path where browser should send the cookie back in request. But when client sends cookie to browser, it only sends the name and value of the cookie.
请注意,服务器会为cookie发送一些其他信息,例如注释,域,cookie过期之前的最长时间以及浏览器应在请求中将cookie发送回的路径。 但是,当客户端向浏览器发送cookie时,它仅发送cookie的名称和值。
Servlet API provides cookies support through javax.servlet.http.Cookie
class that implements Serializable and Cloneable interfaces.
Servlet API通过实现Serializable和Cloneable接口的javax.servlet.http.Cookie
类提供cookie支持。
HttpServletRequest getCookies()
method is provided to get the array of Cookies from request, since there is no point of adding Cookie to request, there are no methods to set or add cookie to request.
提供了HttpServletRequest getCookies()
方法来从请求中获取Cookie数组,因为没有必要向请求中添加Cookie,所以没有方法可以设置或向请求中添加Cookie。
Similarly HttpServletResponse addCookie(Cookie c)
method is provided to attach cookie in response header, there are no getter methods for cookie.
类似地,提供了HttpServletResponse addCookie(Cookie c)
方法来将cookie附加到响应头中,没有用于cookie的getter方法。
Cookie class has a single constructor that takes name and value because they are mandatory parameters for a cookie, all other parameters are optional.
Cookie类具有一个使用名称和值的构造函数,因为它们是cookie的必需参数,所有其他参数都是可选的。
Some important methods of Cookie class are:
Cookie类的一些重要方法是:
We will create two simple servlets to print cookies from client, in one of the servlet we will set a cookie for every domain and a cookie with Path settings so that other servlet won’t receive this from client.
我们将创建两个简单的servlet,以从客户端打印cookie,在其中一个servlet中,我们将为每个域设置一个cookie,并为Path设置一个cookie,以便其他servlet不会从客户端接收到它。
Our final project structure for cookies in java servlet will look like below image.
下图是Java Servlet中Cookie的最终项目结构。
SetCookie.java: This servlet will set some cookies and send it to browser. It will also print cookie information and send it as HTML response.
SetCookie.java :此servlet将设置一些cookie并将其发送到浏览器。 它还将打印cookie信息并将其作为HTML响应发送。
package com.journaldev.servlet.cookie;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.Cookie;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;@WebServlet("/cookie/SetCookie")public class SetCookie extends HttpServlet { private static final long serialVersionUID = 1L; private static int count = 0; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); Cookie[] requestCookies = request.getCookies(); out.write(""); out.write("Hello Browser!!
"); if(requestCookies != null){ out.write("Request Cookies:
"); for(Cookie c : requestCookies){ out.write("Name="+c.getName()+", Value="+c.getValue()+", Comment="+c.getComment() +", Domain="+c.getDomain()+", MaxAge="+c.getMaxAge()+", Path="+c.getPath() +", Version="+c.getVersion()); out.write(""); } } //Set cookies for counter, accessible to only this servlet count++; Cookie counterCookie = new Cookie("Counter", String.valueOf(count)); //add some description to be viewed in browser cookie viewer counterCookie.setComment("SetCookie Counter"); //setting max age to be 1 day counterCookie.setMaxAge(24*60*60); //set path to make it accessible to only this servlet counterCookie.setPath("/ServletCookie/cookie/SetCookie"); //adding cookie to the response response.addCookie(counterCookie); //set a domain specific cookie Cookie domainCookie = new Cookie("Test", "Test Cookie"+String.valueOf(count)); domainCookie.setComment("Test Cookie"); response.addCookie(domainCookie); out.write(""); }}
GetCookie.java: A simple servlet that will demonstrate that the cookie set in SetCookie with specific Path will not be send by browser to this servlet.
GetCookie.java :一个简单的Servlet,它将演示浏览器不会将SetCookie中具有特定Path设置的cookie发送到该servlet。
package com.journaldev.servlet.cookie;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.Cookie;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;@WebServlet("/cookie/GetCookie")public class GetCookie extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); Cookie[] requestCookies = request.getCookies(); out.write(""); out.write("Hello Browser!!
"); if(requestCookies != null){ out.write("Request Cookies:
"); for(Cookie c : requestCookies){ out.write("Name="+c.getName()+", Value="+c.getValue()+", Comment="+c.getComment() +", Domain="+c.getDomain()+", MaxAge="+c.getMaxAge()+", Path="+c.getPath() +", Version="+c.getVersion()); out.write(""); //delete cookie if(c.getName().equals("Test")){ c.setMaxAge(0); response.addCookie(c); } } } out.write(""); }}
When you will run the program, you will notice few things:
当您运行该程序时,您会注意到一些事情:
That’s all for cookies in java and it’s usage in Servlet API, you might want to check out other servlet tutorials too.
这就是Java中的cookie以及Servlet API中的用法,您可能还想查看其他servlet教程。
Check out next articles in series about and .
查阅有关和系列的下一篇文章。
翻译自:
jsp servlet示例
转载地址:http://gvlzd.baihongyu.com/