简体   繁体   English

Java servlet在JSP中获取cookie值

[英]java servlet get cookie value in JSP

In java servlet I have following code: 在Java Servlet中,我有以下代码:

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {       
    Cookie cookie = new Cookie("tom_cookies",Long.toString(new Date().getTime()));
    cookie.setMaxAge(30);
    cookie.setPath(request.getContextPath());
    cookie.setComment("1");
    cookie.setVersion(1);
    System.out.println("Cookie created!");
    response.addCookie(cookie);
}

In JSP index.jsp I have following code: 在JSP index.jsp中,我有以下代码:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>MyIndex</title>
</head>
<body>
<div>CookieComment: <%
Cookie[] my = request.getCookies();
for(int i=0;i<my.length;i++){
String comment = my[i].getComment();
out.println(comment);
}
%> 
</div></body></html>

My web.xml: 我的web.xml:

<?xml version="1.0" encoding="UTF-8"?>

<servlet>
<servlet-name>Test</servlet-name>
<servlet-class>Servlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>Test</servlet-name>
<url-pattern>/servlet1</url-pattern>
</servlet-mapping>

<display-name>1aaa</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>

In local:8080/servlet, what I expect is like: 在local:8080 / servlet中,我期望的是:

CookieComment: 1 Cookie评论:1

However, it only shows: 但是,它仅显示:

CookieComment: null CookieComment:null

whats wrong here? 这怎么了

This is how cookies work: Cookie的工作方式如下:

  1. Browser sends a request to server. 浏览器向服务器发送请求。
  2. Server script sends a set of cookies to the browser. 服务器脚本将一组cookie发送到浏览器。
  3. Browser stores this information on local machine for future use. 浏览器将此信息存储在本地计算机上,以备将来使用。
  4. When next time browser sends any request to web server then it sends those cookies information to the server and server uses that information to identify the user. 当下一次浏览器向Web服务器发送任何请求时,它会将那些cookie信息发送到服务器,并且服务器使用该信息来识别用户。

What you are trying to do is, access the cookies set in response object (step 2) from request object(step 1). 您想要做的是从请求对象(步骤1)访问在响应对象(步骤2)中设置的cookie。 As request object clearly has no idea of the cookie set in response, you are getting null value. 由于请求对象显然不知道响应中设置了cookie,因此您将获得null值。

You can only access cookie in the subsequent request sent by browser (please note that although the code between <% and %> is written in html/jsp, it is actually server side code and is executed before response is rendered. 您只能在浏览器发送的后续请求中访问cookie(请注意,尽管<%%>之间的代码是用html / jsp编写的,但它实际上是服务器端代码,并在呈现响应之前执行。

If you want to pass something back from server and print it then you can use response object or set attributes in request . 如果要从服务器传回某些内容并进行打印,则可以使用response对象或在request 设置属性

You do not really need to get the cookies from the request object via scriptlet code, you can use the implicit variable called 'cookie' via EL 您实际上不需要通过scriptlet代码从请求对象获取cookie,可以通过EL使用称为“ cookie”的隐式变量。

${cookie.yourCookieName}

This should print the value of your cookie in the JSP page. 这应该在JSP页面中打印cookie的值。 It looks for the cookie in the response object 它在响应对象中查找cookie

Hope it helps 希望能帮助到你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM