Servlet & JSP

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 13

Servlets

Servlet Interface
interface

Package(Jakarta.servlet)

Servlet interface has 5 methods:

1) public abstract void init ( javax.servlet.ServletConfig )

2) public ServletConfig getServletConfig ();

3) public void service (javax.servlet.ServletRequest, javax.servlet.ServletResponse)

4) public abstract java.lang.String getServletlnfo ();

5) public abstract void destroy ();


Life cycle methods:

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

Non-Life cycle methods:

1. getServletConfig- We can access servlet config by calling this method

2. getServletInfo- gets info about the servlet


code: implementing Servlet Interface
Generic Servlet Class (abstract)
GenericServlet class implements servlet interface.

It overrides of 4 methods (init, destroy, getServletConfig, getServletInfo) out of 5.

Service method is kept abstract

Hence, GenericServlet Class is an abstract class.

Code: overriding Generic Servlet


Http Servlet Class (concrete)
we override to use protocol specific functionalities.

With reference to client

 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.

Code: overriding http servlet


Welcome File and Welcome File list
<welcome-file-list>

<welcome-file>home.html</welcome-file> …….Html file created by us

<welcome-file>default.html</welcome-file>

</welcome-file-list>

Deployment Descriptor
File that contains configuration of your java web application.

• It resides in the WEB-INF folder.

<web-app>

servlet declaration

servlet-mapping

initialization parameter

welcome file config

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.

It has two methods:

1. forward ()
2. include ()

RequestDispatcher rd = request.getRequestDispatcher (servlet2-url);

rd.forward (request,response);

RequestDispatcher rd = request.getRequestDispatcher (servlet2-url);

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.

• Servlet only read that value for request processing.

• Parameter mostly send data using form, initialization (inside web.xml), etc.

How to get initialization parameter?

String name =request.getParameter("name_of_your_parameter"); in form of key-value pair


Or

String name= request.getInitParameter("name_of_your_parameter");

Now process you request.

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.

How to perform operation with attributes?

• setAttribute(String name,String value)

• Object value=getAttribute(String name)

• removeAttribute(String name)

Code: adding and multiplying two numbers

In first servlet(AddServlet) request is fetched which contain two number and sum is
calculated.

Then request is forwarded to another servlet(ProductServlet) which fetches the numbers


and calculates product and sends response back to the client.
Session Tracking in Servlet
Session Tracking is a way to maintain state (data) of a user.

It is also known as State Management.

Session Tracking Techniques

There are four techniques used in Session tracking:

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.

How to use Cookies in Java?

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.

Cookie c = new Cookie (“key” , value);


To add cookie:

 use addCookie(Cookie) method of response interface.

resp.addCookie(c);

To fetch cookie:

 use getCookie(cookie) method of request interface.

Cookies c[] =request.getCookies();


URL Rewriting using Java Servlet
URL rewriting is a process of appending or modifying any URL structure while loading a page.

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.

<input type=” hidden” name=”user_name" value="Durgesh Tiwari" />

Session Tracking using HttpSession


Session simply means small interval of time.

used for state management.

Create object of httpSession:

HttpSession s = req.getSession ();

Methods in httpSession:

setAttribute(“key”, value )

getAttribute(“key”)

getId()

removeAttribute(“key”)

invalidate()

You might also like