java on the cookie operation (transfer)

2011-07-18  来源:本站原创  分类:Development  人气:93 

java on the cookie's operation is relatively simple, mainly introduced to create a cookie and read the cookie, and how to set the cookie's life cycle and the cookie path problem.

Towards a life cycle of the cookie, the browser with the disappearing off the cookie, the code is as follows

1 HttpServletRequest request
2 HttpServletResponse response
3 Cookie cookie = new Cookie( "cookiename" , "cookievalue" );
4 response.addCookie(cookie);

Following a life cycle of the cookie, you can set the period of his life

1 cookie = new Cookie( "cookiename" , "cookievalue" );
3 cookie.setMaxAge( 3600 );
5 // Set the path, the path that the project can be accessed under the cookie If the path is not set, then set the cookie Paths and sub paths can be accessed
7 cookie.setPath( "/" );
8 response.addCookie(cookie);

Here's how to read the cookie, reads the cookie code is as follows

1 Cookie[] cookies = request.getCookies(); // So that you can get an array of cookie
2 for (Cookie cookie : cookies){
3 cookie.getName(); // get the cookie name
4 cookie.getValue(); // get the cookie value
5 }

The above is the basic read and write cookie operation. We are the best to look at the actual package, such as adding a cookie, our concern is the cookie name, value, life cycle, so a package a function, of course, passed a response object, addCookie () code is as follows

01 /**
02 * Set cookie
03 * @param response
04 * @param name cookie First name
05 * @param value cookie The value of
06 * @param maxAge cookie Life cycle in seconds
07 */
08 public static void addCookie(HttpServletResponse response,String name,String value, int maxAge){
09 Cookie cookie = new Cookie(name,value);
10 cookie.setPath( "/" );
11 if (maxAge> 0 ) cookie.setMaxAge(maxAge);
12 response.addCookie(cookie);
13 }

Read a cookie in order to facilitate our operations, we want to encapsulate a function, as long as we deliver the cookie's name, we can get the cookie value, with the idea, it is easy to think inside the cookie package to the Map, then the The following package

View Source Print ?

01 /**
02 * Under the name gets cookie
03 * @param request
04 * @param name cookie First name
05 * @return
06 */
07 public static Cookie getCookieByName(HttpServletRequest request,String name){
08 Map<String,Cookie> cookieMap = ReadCookieMap(request);
09 if (cookieMap.containsKey(name)){
10 Cookie cookie = (Cookie)cookieMap.get(name);
11 return cookie;
12 } else {
13 return null ;
14 }
15 }
19 /**
20 * The cookie package to Map Inside
21 * @param request
22 * @return
23 */
24 private static Map<String,Cookie> ReadCookieMap(HttpServletRequest request){
25 Map<String,Cookie> cookieMap = new HashMap<String,Cookie>();
26 Cookie[] cookies = request.getCookies();
27 if ( null !=cookies){
28 for (Cookie cookie : cookies){
29 cookieMap.put(cookie.getName(), cookie);
30 }
31 }
32 return cookieMap;
33 }
  • java on the cookie operation (transfer) 2011-07-18

    java on the cookie's operation is relatively simple, mainly introduced to create a cookie and read the cookie, and how to set the cookie's life cycle and the cookie path problem. Towards a life cycle of the cookie, the browser with the disappearing o

  • Java programming for the operation of cookie 2011-08-08

    Java on the cookie's operation is relatively simple, mainly introduced to create a cookie and read the cookie, and how to set the cookie's life cycle and the cookie path problem. Towards a life cycle of the cookie, the browser with the disappearing o

  • js / jsp cookie operation method 2010-09-26

    Recent projects use the automatic logon feature, the use to the cookie. 1, cookie Introduction browser and WEB server is using the HTTP protocol to communicate; the HTTP protocol is stateless protocol. That is, when a user send the page request, WEB

  • java to get the main information page two: auxiliary operation (transfer) 2010-12-04

    1 stack operation package Source; import java.util.LinkedList; // List the definition of a stack public class Stack { // Construction method public Stack() { list = new LinkedList(); isEmpty = true; } // In stack public void push(Object node) { list.

  • Java General access control algorithm (transfer) 2010-06-20

    Selections A common access control algorithm, reference LINUX / UNIX permissions coding Here the author describes a very common and have a better idea of professional access control. The language to be replaced by other owners, their transfer click o

  • Java properties file to read [transfer] 2010-12-29

    Java properties file to read] [switch Use J2SE API methods to read documents in the six Properties 1. Using the java.util.Properties class load () method example: InputStream in = lnew BufferedInputStream (new FileInputStream (name)); Properties p =

  • java \ oracle date format operation 2011-07-01

    java from the database to take time, 1 in JAVA, some use the date format, such as DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); The TO_DATE function in ORACLE in the corresponding format is yyyy-MM-dd hh24: mi: ss. Although the us

  • Commons-httpclient cookie operation 2010-03-29

    This tip shows how to use cookies. The application below sets an HTTP cookie and updates the cookie's value across multiple HTTP GET requests. /* * $Header: * $Revision$ * $Date$ * ====================================================================

  • Java to take over operations (transfer) 2010-04-04

    Mod operator is "%" It is a binocular operator, its operands is usually a positive integer can be negative or even float the book, if a negative number to participate in this operation, you will need special attention, for the integers, java to

  • Java implementation Windows system services (transfer) 2008-07-15

    How to turn a Java program made NT Service article: From: Windows System Service (NT service) relative to the general application, the most direct one advantage is that the system startup

  • Java call com component operation word 2010-03-30

    First, preparatory work First look at the concept, JACOB JAVA-COM Bridge is an abbreviation and provide automated functionality to access com, also through the JNI functions to access the com component windows platform or win32 system libraries. This

  • JavaScript cookie operation 2010-06-20

    Add a cookie of JavaScript operations, access, delete JavaScript operation of the path and cookie domain

  • java access to cookie 2010-06-23

    Take: Cookie [] cookies = request.getCookies (); if (cookies! = null) ( for (Cookie cookie: cookies) ( if (cookie.getName (). equals ("mid")) ( String mid = cookie.getValue (); ) ) ) Deposit: String mid = (). Ge

  • Java in the bit operation 2010-06-28

    Shift operator Include: ">> Shifted to the right ";"<< left ";">>> unsigned right shift" Left three is multiplied by 8, left n bits is multiplied by 2 n-th power Three right shifts is divided by 8, right

  • JAVA ARP spoofing packet modification (transfer) 2010-10-16

    ARP (Address Resolution Protocol) address both the interpretation of agreements, the main purpose is used for IP and MAC address of interpretation. IP is a network layer protocol, while the MAC is the data link layer. Network, two nodes to communicat

  • Java MySQL image access operation to achieve 2010-10-31

    Article Source: Into operation /* --------------- Table structure ------------ The table name :student2 +--------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key |

  • SAP ABAP OLE EXCEL template upload operation (transfer) 2011-05-04

    ABAP data processing directly with EXCEL. This is achieved by operating EXCEL OLE's. Now there DOI Operation EXCEL and other methods. After use do introduced. EXCEL files are not directly generated here, but from EXCEL template has been uploaded the

  • Arithmetic and logical shift operation differences and connections between the shift operation (transfer) 2011-03-19

    There are a variety of shift operation, generally known arithmetic shift operation (2), logical shift (2) of these four, the general procedure is sufficient. Shift operation to shift direction will cause some data loss, so take good control. Logical

  • oracle date type field of operation (transfer) 2011-03-30

    The operation of the oracle in java, the date field is a headache, in fact, a closer look it is not difficult to obtain. Example to illustrate: Table book has name varchar2 (20) / / book titles, buydate Date / / date of purchase two fields. Has creat

  • Table space operation (transfer) 2011-05-30

    Turn 1 Create table space: create tablespace tablespace_name datafile 'filepath' size filesize autoextend on next autosize maxsize filemaxsize [unlimited] eg: create tablespace sales datafile 'c: \ 1.txt' size 10m autoextend on next 1m maxsize 100m 2