Professional Documents
Culture Documents
Servlet & JSP
Servlet & JSP
Servlet & JSP
Servlet Interface
interface
Package(Jakarta.servlet)
1. Init- when servlet is called object is created, to create the object server calls init method
2. Service- to process request
3. destroy- to destroy the object created to release resources
doGet()
if client wants to fetch data from server (Ex- to send form data from client to
servlet)
doPost()
if client wants to send data to server (Ex- to fetch data from database by client)
doPut()
doDelete() ,etc.
<welcome-file>default.html</welcome-file>
</welcome-file-list>
Deployment Descriptor
File that contains configuration of your java web application.
<web-app>
servlet declaration
servlet-mapping
initialization parameter
filter
listener
session config
etc
</web-app>
RequestDispatcher
It is responsible for dispatching the request to another resource it may be html, servlet or JSP.
1. forward ()
2. include ()
rd.forward (request,response);
rd.include (request,response);
Parameters and attributes in servlet
Parameters — These are those values which are provided by user to any servlet to process the
request during the request operation.
• Parameter mostly send data using form, initialization (inside web.xml), etc.
Attributes- these are the objects that are attached by one servlet to object (session, request,
config, context etc.) and another servlet can fetch that object to process to logic.
Servlet can easily modify, add and remove the content of attribute when required.
• removeAttribute(String name)
In first servlet(AddServlet) request is fetched which contain two number and sum is
calculated.
1. Cookies
2. Hidden Form Field
3. URL Rewriting
4. HttpSession
Cookies in Servlet
Cookies are the textual information which are stored in key value pair format to the client's
browser during multiple requests.
In order to use cookies in java, there is a Cookie class in java present in javax.servlet.http package.
To make cookie:
just create an object of Cookie class and pass name and its value.
resp.addCookie(c);
To fetch cookie:
Code- session management by storing the value of username using URL rewriting.
Hidden Form Field
Hidden Form Field a hidden (invisible) text field is used for maintaining the state of a user.
Methods in httpSession:
setAttribute(“key”, value )
getAttribute(“key”)
getId()
removeAttribute(“key”)
invalidate()