Javax.servlet.http.Cookie osztály a Java nyelven
Sok webhely apró, úgynevezett cookie-kat használ a kliensoldali állapot állandó tárolására a kapcsolatok között. A cookie-k a szerverről a kliensre, majd vissza a kérések és válaszok HTTP-fejlécébe kerülnek. A kiszolgáló cookie-kat használhat a munkamenet-azonosítók, a bevásárlókosár tartalmának, a bejelentkezési hitelesítő adatok felhasználói preferenciáinak és egyebeknek a jelzésére.
Amint a fenti diagramból látható, amikor a felhasználó először kér egy oldalt, a szerver az erőforrással együtt egy cookie objektumot küld, amelyet az ügyfél gépén tárolnak. Ez az objektum tartalmazhatja a kérés részleteit. Később, ha a felhasználó újra kéri ugyanazt az erőforrást, akkor a kéréssel együtt elküldi a tárolt cookie-t, amelyet a szerverek felhasználhatnak a felhasználói élmény további javítására. A süti tulajdonságai:
Hogyan működnek a sütik?
Amint a fenti diagramból látható, amikor a felhasználó először kér egy oldalt, a szerver az erőforrással együtt egy cookie objektumot küld, amelyet az ügyfél gépén tárolnak. Ez az objektum tartalmazhatja a kérés részleteit. Később, ha a felhasználó újra kéri ugyanazt az erőforrást, akkor a kéréssel együtt elküldi a tárolt cookie-t, amelyet a szerverek felhasználhatnak a felhasználói élmény további javítására. A süti tulajdonságai: - Először a szervlet beállít egy cookie-t teszt_cookie néven. A program többi sora beállítja a cookie attribútumait, például max age domain value stb.
- Másodszor, a szervlet a request.getCookies segítségével megkeresi az összes bejövő cookie-t, és megjeleníti a nevüket és az egyéb megfelelő attribútumokat.
- Ha nem találunk cookie-kat, mint az első kérésnél, egy egyszerű kijelző üzenet jelenik meg, amely jelzi, hogy ez az oldal első látogatása.
Set-Cookie:session-id = 187-4969589-3049309
Set-Cookie: user = geek ;Domain =.foo.example.com
Set-Cookie: user = geek; Path =/ restricted
Set-Cookie: user = geek; expires = Wed 21-Feb-2017 15:23:00 IST
Set-Cookie: user = 'geek'; Max-Age = 3600Konstruktőr : Creates a cookie with specified name-value pair.
Syntax : public Cookie(String name String value) Parameters : name : name of the cookie value : value associated with this cookieMódszerek:
Syntax : public void setDomain(String pattern) Parameters : pattern : string representing the domain in which this cookie is visible.
Syntax : public String getDomain()
Syntax : public void setComment(String purpose) Parameters : purpose : string representing the purpose of this cookie.
Syntax : public String getComment()
Syntax : public void setMaxAge(long time) Parameters : time : time in seconds before this cookie expires
Syntax : public String getMaxAge()
Syntax : public void setPath(String path) Parameters : path : path where this cookie is returned
Syntax : public String getMaxAge()
Syntax : public void setSecure(boolean secure) Parameters: secure - If true the cookie can only be sent over a secure protocol like https. If false it can be sent over any protocol.
Syntax : public boolean getSecure()
Syntax : public String getName()
Syntax : public void setValue(String newValue) Parameters : newValue - a String specifying the new value
Syntax : public String getValue()
Syntax : public int getVersion()
Syntax : public void setVersion(int v) Parameters : v - 0 for original Netscape specification; 1 for RFC 2965/2109
Syntax : public Cookie clone()Below is a Java implementation of a simple servlet program which stores a cookie in the browser when user first requests for it and then for further requests it displays the cookies stored. Java
// Java program to illustrate methods // of Cookie class import java.io.IOException ; import java.io.PrintWriter ; import java.util.List ; import javax.servlet.ServletException ; import javax.servlet.annotation.WebServlet ; import javax.servlet.http.Cookie ; import javax.servlet.http.HttpServlet ; import javax.servlet.http.HttpServletRequest ; import javax.servlet.http.HttpServletResponse ; /** * Servlet implementation class cookieTest */ @WebServlet ( '/cookieTest' ) public class cookieTest extends HttpServlet { private static final long serialVersionUID = 1L ; /** * @see HttpServlet#HttpServlet() */ public cookieTest () { super (); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request HttpServletResponse * response) */ protected void doGet ( HttpServletRequest request HttpServletResponse response ) throws ServletException IOException { response . setContentType ( 'text/html' ); // Create a new cookie with the name test cookie // and value 123 Cookie cookie = new Cookie ( 'test_cookie' '123' ); // setComment() method cookie . setComment ( 'Just for testing' ); // setDomain() method // cookie.setDomain('domain'); // setMaxAge() method cookie . setMaxAge ( 3600 ); // setPath() method cookie . setPath ( '/articles' ); // setSecure() method cookie . setSecure ( false ); // setValue() method cookie . setValue ( '321' ); // setVersion() method cookie . setVersion ( 0 ); response . addCookie ( cookie ); PrintWriter pw = response . getWriter (); pw . print ( ' ' ); Cookie ck [] = request . getCookies (); if ( ck == null ) { pw . print ( ' This is first time the page is requested.
' ); pw . print ( ' And therefore no cookies found
' ); } else { pw . print ( ' Welcome Again...Cookies found
' ); for ( int i = 0 ; i < ck . length ; i ++ ) { // getName() method pw . print ( ' Name :'
+ ck [ i ] . getName () + ' ' ); // getValue() method pw . print ( ' Value :'
+ ck [ i ] . getValue () + ' ' ); // getDomain() method pw . print ( ' Domain :'
+ ck [ i ] . getDomain () + ' ' ); // getPath() method pw . print ( ' Name :'
+ ck [ i ] . getPath () + ' ' ); // getMaxAge() method pw . print ( ' Max Age :'
+ ck [ i ] . getMaxAge () + ' ' ); // getComment() method pw . print ( ' Comment :'
+ ck [ i ] . getComment () + ' ' ); // getSecure() method pw . print ( ' Name :'
+ ck [ i ] . getSecure () + ' ' ); // getVersion() method pw . print ( ' Version :'
+ ck [ i ] . getVersion () + ' ' ); } pw . print ( ' ' ); } pw . close (); } /** * @see HttpServlet#doPost(HttpServletRequest request HttpServletResponse * response) */ protected void doPost ( HttpServletRequest request HttpServletResponse response ) throws ServletException IOException { doGet ( request response ); } }
KIMENET: A következő kimenetek egy webböngészőből származnak- Az első kéréshez: This is first time the page is requested. And therefore no cookies found.A második kéréshez:
Welcome Again...Cookies found Name :test_cookie Value :321 Domain :null Name :null Max Age :-1 Comment :null Name :false Version :0
Hogyan kell futtatni a fenti programot?
Először győződjön meg arról, hogy telepítve van valamilyen kiszolgáló, például az Apache Tomcat, és be van állítva az Ön által használt eszközzel, például az Eclipse-el. Egyszerűen futtassa a fenti programot a kiszolgálón vagy a helyi böngészőben úgy, hogy megadja a használt szerverkönyvtár teljes címét. A CookieTest szervlet egy servlet, amely három feladatot hajt végre: