Quick warm-up:
Cookies can be used to store user information on the client through a browser.
Cookies are some text information that the Web server sends to the browser and is stored on the client computer disk.
When a browser sends a request to the Web server, if the Cookie does not expire, the server-side program will read the information.
Sometimes cookies are used to collect user information without the user's knowledge, resulting in leakage of some confidential information.
Users may disable the Cookie function in the browser.
Validity Period: You can set the validity period.
Test Cookie:
1. Create a web project.
2. Create a new package named com. neusoft. servlet_cookie under the src directory.
3. Create two servlets named cookieRead and cookieTest under the package. The content of cookieTest is:
------------------------------------
Package com. neusoft. servlet_cookie;
Import java. io. IOException;
Import javax. servlet. ServletException;
Import javax. servlet. http. Cookie;
Import javax. servlet. http. HttpServlet;
Import javax. servlet. http. HttpServletRequest;
Import javax. servlet. http. HttpServletResponse;
Public class cookieTest extends HttpServlet {
Public void doGet (HttpServletRequest request, HttpServletResponse response)
Throws ServletException, IOException {
This. doPost (request, response );
}
Public void doPost (HttpServletRequest request, HttpServletResponse response)
Throws ServletException, IOException {
Response. setContentType ("text/html; charset = gb2312 ;");
Cookie cookie = new Cookie ("userName", "wjl"); // you can specify the information to be written to the cookie.
Cookie. setMaxAge (5); // set the cookie Validity Period
Response. addCookie (cookie); // write the cookie to the client
Response. sendRedirect ("cookieRead ");
}
}
------------------------------------
CookieRead content:
------------------------------------
Package com. neusoft. servlet_cookie;
Import java. io. IOException;
Import java. io. PrintWriter;
Import javax. servlet. ServletException;
Import javax. servlet. http. Cookie;
Import javax. servlet. http. HttpServlet;
Import javax. servlet. http. HttpServletRequest;
Import javax. servlet. http. HttpServletResponse;
Public class cookieRead extends HttpServlet {
Public void doGet (HttpServletRequest request, HttpServletResponse response)
Throws ServletException, IOException {
This. doPost (request, response );
}
Public void doPost (HttpServletRequest request, HttpServletResponse response)
Throws ServletException, IOException {
Response. setContentType ("text/html; charset = gb2312 ;");
PrintWriter out = response. getWriter ();
Cookie [] cookie = request. getCookies ();
For (int I = 0; I <cookie. length; I ++ ){
Out. println ("userName:" + cookie [I]. getName () + "Value :"
+ Cookie [I]. getValue ());
}
Out. flush ();
Out. close ();
}
}
------------------------------------
4. The servlet configuration in web. xml is as follows:
------------------------------------
<Servlet>
<Servlet-name> cookieTest </servlet-name> <servlet-class> com. neusoft. servlet_cookie.cookieTest </servlet-class>
</Servlet>
<Servlet>
<Servlet-name> cookieRead </servlet-name> <servlet-class> com. neusoft. servlet_cookie.cookieRead </servlet-class>
</Servlet>
<Servlet-mapping>
<Servlet-name> cookieTest </servlet-name>
<Url-pattern>/servlet/cookieTest </url-pattern>
</Servlet-mapping>
<Servlet-mapping>
<Servlet-name> cookieRead </servlet-name>
<Url-pattern>/servlet/cookieRead </url-pattern>
</Servlet-mapping>
------------------------------------
5. re-release the project and enter the address http: // localhost: 8080/Test/servlet/cookieTest in the browser. At this time, the cookie has been written to the client computer and then redirected to http: // when the localhost: 8080/Test/servlet/cookieRead page is accessed, the content of the cookie object that is stored on the client computer for valid time of 5 seconds is read, the information displayed on the page is as follows:
UserName: userNameValue: wjl
The cookie object becomes invalid after five seconds. At this time, the http: // localhost: 8080/Test/servlet/cookieRead page is refreshed. The page cannot read the value from the cookie object, the page information is as follows:
HTTP Status 500-
Type Exception report
Message
Description The server encountered an internal error () that prevented it from fulfilling this request.
Exception
Java. lang. NullPointerException
Com. neusoft. servlet_cookie.cookieRead.doPost (cookieRead. java: 23)
Com. neusoft. servlet_cookie.cookieRead.doGet (cookieRead. java: 15)
Javax. servlet. http. HttpServlet. service (HttpServlet. java: 690)
Javax. servlet. http. HttpServlet. service (HttpServlet. java: 803)
Note The full stack trace of the root cause is available in the Apache Tomcat/6.0.14 logs.
Apache Tomcat/6.0.14
Author: "extreme scholar"