Servlet cookie value assignment and value processing and page-based forward value processing

Source: Internet
Author: User

Servlet cookie value assignment and value processing method and page-based pre-pass value processing method. The following describes how to obtain and set values for cookies, and then detailed the servlet cookie instance operation using an instance.


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 AddCookieServlet extends HttpServlet {

Public void doPost (HttpServletRequest request, HttpServletResponse response)
Throws ServletException, IOException {
String data = request. getParameter ("data ");
Cookie cookie = new Cookie ("MyCookie", data );
Response. addCookie (cookie );
Response. setContentType ("text/html ");
PrintWriter pw = response. getWriter ();
Pw. println ("<B> MyCookie has been set ");
Pw. println (data );
Pw. close ();
}
}

Cookie acquisition

 

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 GetCookiesServlet extends HttpServlet {

Public void doGet (HttpServletRequest request, HttpServletResponse response)
Throws ServletException, IOException {

Cookie [] cookies = request. getCookies ();

Response. setContentType ("text/html ");
PrintWriter pw = response. getWriter ();
Pw. println ("<B> ");
For (int I = 0; I <cookies. length; I ++ ){
String name = cookies [I]. getName ();
String value = cookies [I]. getValue ();
Pw. println ("name =" + name + "; value =" + value );
}
Pw. close ();
}
}

Based on the above example, let's take a look at the transmission between servlet cookie pages.

Import java. io .*;
Import javax. servlet .*;
Import javax. servlet. http .*;
Import java. util .*;

Public class MyServlet extends HttpServlet {

Public void doGet (HttpServletRequest request, HttpServletResponse response)
Throws ServletException, java. io. IOException {

Cookie cookie = null;
// Get an array of Cookies associated with this domain
Cookie [] cookies = request. getCookies ();
Boolean newCookie = false;

// Get the 'mycooker' Cookie if it exists
If (cookies! = Null ){
For (int I = 0; I <cookies. length; I ++ ){
If (cookies [I]. getName (). equals ("mycookie ")){
Cookie = cookies [I];
}
} // End
} // End if

If (cookie = null ){
NewCookie = true;
// Get the cookie's Max-Age from a context-param element
// If the 'cookie-age' param is not set properly
// Then set the cookie to a default of-1, 'Never expires'
Int maxAge;
Try {
MaxAge = new Integer (getServletContext (). getInitParameter ("cookie-age"). intValue ();
} Catch (Exception e ){
MaxAge =-1;
}

// Create the Cookie object

Cookie = new Cookie ("mycookie", "" + getNextCookieValue ());
Cookie. setPath (request. getContextPath ());
Cookie. setMaxAge (maxAge );
Response. addCookie (cookie );

} // End if
// Get some info about the cookie
Response. setContentType ("text/html ");
Java. io. PrintWriter out = response. getWriter ();

Out. println ("Out. println ("Out. println ("<title> Cookie info </title> ");
Out. println ("Out. println ("<body> ");

Out. println ("
Out. println ("Cookie value:" + cookie. getValue () + "<br> ");
If (newCookie ){
Out. println ("Cookie Max-Age:" + cookie. getMaxAge () + "<br> ");
Out. println ("Cookie Path:" + cookie. getPath () + "<br> ");
}

Out. println ("</body> ");
Out. println ("
Out. close ();
}
Private long getNextCookieValue (){

/* This produces a cookie value to show how to create Cookie objects. If
This method was heavily used in a production environment it may
Produce too prepare objects; synchronization of a single Date object
Might be better, based on performance testing. At any rate
Production environment wowould produce a unique cookie value in
Different manner such as from a unique database ID .*/

// Returns the number of milleseconds since Jan 1, 1970
Return new java. util. Date (). getTime ();

}


Public void doPost (HttpServletRequest request, HttpServletResponse response)
Throws ServletException, java. io. IOException {

DoGet (request, response );
}
}
 
<? Xml version = "1.0" encoding = "ISO-8859-1"?>

<! DOCTYPE web-app
PUBLIC "-// Sun Microsystems, Inc. // DTD Web Application 2.2 // EN"
Http://java.sun.com/j2ee/dtds/web-app_2_2.dtd>

<Web-app>
<Servlet> <servlet-name> MyServletName </servlet-name>
<Servlet-class> MyServlet </servlet-class>
<Init-param>
<Param-name>
Go
</Param-name>
<Param-value>
Weather
</Param-value>
</Init-param>

</Servlet>

<Servlet-mapping> <servlet-name> MyServletName </servlet-name>
<Url-pattern>/index.html </url-pattern>
</Servlet-mapping>
</Web-app>
 

 

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.