Professional Documents
Culture Documents
Adobe Scan Oct 21, 2023
Adobe Scan Oct 21, 2023
13 cOOKIES
HTTP is statciess. This mcans that every HHTTP rcquest is diflerent from others Sometinnes it is
Cssary lo keep lrack of asequcnce of related requests sent by a cient to pertormsone designated
This is calicd seSsIOn Iackng. Cookies are one of the solutions to session tracking, A cookie s? [
Servlets 667
created by the
value)par
firstWime
A
server and is installed in the
forthe subsequent browser also maintains a list of client's browser when the client makes arequest
apart of HTTP requests. The cookics installcd in it and sends it to the server as
sequence related
of
requests. server can then casily identify that this request is a part of
This way, cookics provide
a
cervlet APIsupports cookies. an clegant to session tracking. The
Acookic is solution
is
created.using the
following constructor:represented using the javax . servlet .http.cookie class and
Cookie (String key, string
value)
Acookie is added by the addCookie() method of the class. Similarly, the
servercan get
all cookies sent by
tRequestclass. There are two
thc web browser using the HttpservletPesponse
getCookies () method of the HttpServie-
types of cookies in servBets, non-persistent cookies and persistent
skies. A non-peISIstent cOOKie is valid for a single sessionnamely
and it is removed each time the user closes
the browser. Apersistent cookie however is valid for multiple sessions and it IS not removed when the
user closesthe browser. Acookie can be defined as asmall piece of information that is persisted with
multiple client requests., 1I he following example illustrates the handling of servlet cookies.
1/index.html
<form action="servlet1" method="post">
Name:<input type="text" name="userNane "/><br />
<input type="submit" value="go"/>
</form>
1/FirstServlet.java
import java.io. *;
import javax.servlet.*;
import javax.servlet.http.*;
{
public class FirstServlet extends HttpServlet
request,
public void doPost (HttpServletRequest
HttpServletResponse response)
try
response.setCont.entType ("t.ext/html "):
response.getWriter(0:
PrintWriter out = "userName") ;
Paramet,er (
String n=request.get
out.print (Welcome "+n):
cookie
("uname", n);//Creating
Cookie ck=new Cookie
object (ck);//adding cookie in the
response. addcookie
respornse
//creating subnit button
action='servlet2
out.print ("<form
method='post'>"); valuo'qo "}:
type submit
out.print ("<input
out.print ("</form,"):
Qut.close();
out.printli (e):)
(Exception e) (System.
catch
7SecondServlet.java
import java.io. *;
import javax.servlet.*i
1mport javax.servlet. http.;
PuoliC class Second Servlet ext ends Ht tpServlet HttpservletResponse
publ ic void doPost
do (HttpServletReguest request,
try{ responae) (
response.setContentType("Lext/html )i
PrintWriter out = response.getWr iter0;
Cookie ckl)=request.get Cookies);
out.print ("Hello "+ck |0).getVa lue 0);
out.close ():
}catch (Exception e) (Syst em.out.println (e) ;}
<web-app>
<Servlets
<description>This is the description of my J2EE Component</description,
<£isplay-name> This is the display name of Iny J2EE Component</display-hames
Kservlet-name> Servletl</servlet-name>
<servlet-class>Firstservlet</servlet-class>
</servlet
<servlet
<descriptionThis is the description of my J}EE component</descriptions
<aisplay-name>This is the dispiay name of my J2EE component</display-tamey
<servlet-name>SecondServlet</servlet-name>
servlet-class>SecondServlet</servlet-class>
</servlet>
<servet-mapping>
servlet-name>Servlet1</servlet-name>
Furl-pattern/servlet1</url-pattern>
/servlet-napping>
servlet-mapping>
servlet-name>SecondServlet</servlet-name>
url-pattern>/servlet2</url-pattern>
iservlet-napping>
welcone-file-list>
welcone-file>index.html</welcome-file>
/welcome- file-list>
Iweb-app>
Welcome HeadlSacredines
go
Sacred Hean Colteqt
Met College
Hrelox
19.14 FILTERS
between the client and the server to inspect requests and responses.
Filters are objects that are installed
the response or
both. The filterS are not servlets and hence can-
Ihey can transform the request or modify
before they reach a servlet and/or process responses
not create actual responses. Filtersprocessrequests following:
can do the
after leaving a servlet. In general, afilter
before dispatchingtotheservlets.
requests
IntModiercept
and inspect
fy requests headers and data and discard or
filter requests.
dispatching responscs.
responses before discard or
filter Similarly, zero or
Intercept and inspect servlets.
" Modify requests' headers and data
and
servlct
of
or a groupfollowingarcas: more fih.
behalf of a single
typicallyuscdinthe
Afilter can work on
installed for a scrvlct. Filters are
can be
" Authentication
Logging and auditing
Image compression
" Data compression
Encryption
a
" Tokenization
which provides
" XML transfornation
Web
container
<o response
destroy() { destroy() f destroy() i
}
Filter Filter Filter Servlet