How to use the Cookie?el expression in the JSP to get the cookie problem __js

Source: Internet
Author: User
Tags rfc

How do I use cookies in the JSP?2006-10-04 01:27 diqigan0623 |  Category: Other programming Languages | Viewed 21,873 times


Beginner JSP, do not know how to use cookies, I hope the master pointing.

 

Share to: 2006-10-04 09:12 questioner Acceptance

Generally speaking, there are two ways to use Java's embedded script in JSP.
E.g:
Write cookies
<html>
<head> ........ (middle)
</ head>
<body>
<%
String cookieName = "Sender";
Cookie cookie = new Cookie (cookieName, "Test_Content");
cookie.setMaxAge (10);
response.addCookie (cookie);
%>
........(Other content)
</ body>
</ html>
  This way we set a cookie.
  Take a closer look at this code:
  Cookie cookie = new Cookie (cookieName, "Test_Content");
  This line creates a Cookie object, initialized with two parameters, the first parameter cookieName defines the name of the cookie, and the latter parameter, which is also a string, defines the content of the cookie. That is, the content of the file we want the webpage to identify on the user's machine.
  The next line: cookie.setMaxAge (10) calls the setMaxAge method in the cookie and sets the cookie's lifetime on the user's hard disk to 10 seconds. A cookie does not exist in the user's hard disk for an indefinite period of time. When creating a cookie object, we must set the lifetime of the cookie. After this lifetime, the cookie file will no longer work and will be viewed by the user. The device deletes itself. If we hope that the next time the user visits this page, the cookie file is still valid and can be read out by the web page, we can set the lifetime of the cookie to be slightly longer. For example, cookie.setMaxAge (365 * 24 * 60 * 60) can make the cookie file valid for one year.

Read out cookies
  After the cookie file is created, we naturally need to read it out, otherwise we are not in vain. Next we look at how to read out cookies on the user's hard drive.
<html>
<head> ........ (middle)
</ head>
<body>
<table border = 1>
<tr> <td> Name </ td> <td> value </ td> </ tr>
<%
Cookie cookies [] = request.getCookies ();
Cookie sCookie = null;
String svalue = null;
String sname = null;
for (int i = 0; i <cookies.length; i ++)
{
sCookie = cookies [i];
svalue = sCookie.getValue ();
sname = sCookie.getName ();
%>
<tr> <td> <% = name%> </ td> <td> <% = svalue%> </ td> </ tr>
<%
}
%>
</ table>
........(Other content)
</ body>
</ html>
  This short JSP file can read all valid cookies on the user's hard disk, that is, the cookie files that are still in the lifetime. List the names and contents of each cookie in a table.
  Let's analyze this code line by line:
  Cookie cookies [] = request.getCookies () We use request.getCookies () to read out the cookies on the user's hard disk, and put all cookies into an array of cookie objects.
  Next, we use a loop statement to iterate over the cookie object array we just created. We use sCookie = cookies [i] to retrieve a cookie object in the array. Then we use sCookie.getValue () and sCookie.getName () to obtain The name and content of this cookie.
  By putting the name and content of the retrieved cookie in a string variable, we can perform various operations on it. In the above example, all cookies can be displayed in a table by looping through the loop statements.

Through the above two simple examples, you can see that the operation of cookies with JSP is very simple. However, we need to pay attention to some problems in actual operation:
  1. Cookie compatibility issues
  There are two different versions of the cookie format. The first version, which we call Cookie Version 0, was originally developed by Netscape and is also supported by almost all browsers. The newer version, Cookie Version 1, is based on RFC 2109. In order to ensure compatibility, JAVA stipulates that the aforementioned operations involving cookies are performed on older versions of cookies. The new version of cookies is not currently supported by the Javax.servlet.http.Cookie package.
  2. Content of cookies
  The character limits of the same cookie content are different for different cookie versions. In Cookie Version 0, some special characters, such as: space, square brackets, parentheses, equal sign (=), comma, double quotes, slash, question mark, @ symbol, colon, semicolon are not used as cookies content. This is why we set the content of the cookie to "Test_Content" in the example.
  Although the restrictions have been relaxed in the Cookie Version 1 rule, these characters can be used, but considering that the new version of the cookie specification is not currently supported by all browsers, we should avoid the use of cookies as much as possible in order to be safe. These characters.

Another way is to manipulate cookies with Javascript script

The specific methods of operating cookies in two ways can be found this way:
Search keywords in baidu: JSP cookie or Javascript cookie to see the information you want.
 
 
 
Problems getting cookies in el expression
The code in my servlet looks like this
Cookie cookie = new Cookie ("username", new String (name.getBytes ("iso8859-1")));
In the jsp page,
$ {cookie.username.value}
However, the value obtained on the page is garbled.

Comment by questioner

Thanks a lot.

References: http://www.jr163.org/cup2/9/9903.htm


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.