Archive for the ‘Servlet’ Category

Which are the different ways you can communicate between servlets?

√Using RequestDispatcher object.
√ Sharing resource using “ServletContext ()” object.
Include response of the resource in the response of the servlet.
√ Calling public methods of the resource.
Servlet chaining

What is functionality of “RequestDispatcher” object?

Above is the code snippet for “requestdispatcher”. Code snippet “getServletContext().getRequestDispatcher(“/servlet/clsAdd”); “
gets the “requestdisptacherobject”. In order to call the other servlet class we call the forward method which in turn invokes the service method of the other servlets.

How do we share data using “getServletContext ()”?

Using the “getServletContext ()” we can make data available at application level.
So servlets which lie in the same application can get the data. Below are important snippets which you will need to add,
get and remove data which can be shared across servlets.

You can see in Step1 how “getServletContext().setAttribute()” method can be used add new objects to
the “getServletContext” collection. Step2 shows how we can get the object back using “getAttribute()” and
manipulate the same. Step3 shows how to remove an already shared application object from the collection.
getServletContext()” object is shared across servlets with in a application and thus can be used to share data between servlets.

What are filters in JAVA?

Filters are nothing but simple java classes which can manipulate request before it reaches the resource on the web server. Resource can be a HTML file, servlet class, JSP etc. It can also intercept responses sent back to client and thus can manipulate the response before they reach the client browser.

The doFilter() method will do the actual logging of information. You can modify this method and add your code
which can modify request/session/response, add any attribute in request etc.


Read Full Post »

Cookies and Session

What is URL rewriting?
It’s based on the concept of attaching a unique ID (which is generated by the server) in the URL of response from the server.
So the server attaches this unique ID in each URL. When the client sends a requests it sends back this ID with the request
also which helps the server to identify the client uniquely.

What are cookies?
Cookies are piece of data which are stored at the client’s browser to track information regarding the user usage and habits.
Servlets sends cookies to the browser client using the HTTP response headers. When client gets the cookie information
it’s stored by the browser191 in the client’s hard disk. Similarly client returns the cookie information using the HTTP request headers.

When you say getSession(true), this method will check whether already a session is existing for the user.
If a session is existing, it will return that session object, OTHERWISE WILL CREATE A SESSION OBJECT EXPLICITLY AND RETURN TO THE CLIENT.
Difference between using getSession(true) and getSession(false) methods?
When you say getSession(false), this method will check whether a session is existing.
If yes, then it returns the reference of that session object, OTHERWISE IT WILL RETURN ‘null’.
request.getSession() will return the current session and if one does not exist, a new session will be cretaed.

request.getSession(true) will return the current session if one exists, if one doesn’t exits a new one will be created.

So there is actually no difference between the two methods HOWEVER, if you use request.getSession(false),
it will return the current session if one exists and if one DOES NOT exist a new one will NOT be cretaed.

Read Full Post »

Servlet Http

Interfaces in javax.servlet.http

HttpServletRequest Interface:-
This interface has list of methods’s in HttpServletRequest.

HttpServletResponse Interface
It enables a servlet to formulate an HTTP response to a client..

HttpSession Interface
HTTP protocol is a stateless protocol and this interface enables to maintain sessions between requests.

The HttpSessionBindingListener interface is implemented by objects that need to be notified when they are bound to or unbound from an HTTP session.

Cookie Class
The Cookie class encapsulates a cookie. A cookie is stored on a client and contains state information.
Cookies are valuable for tracking user activities. For example, assume that a user visits an online store.
A cookie can save the user’s name, address, and other information.
The user does not need to enter this data each time he or she visits the store.

HttpServlet Class
It is commonly used when developing servlets that receive and process HTTP requests.

HttpSessionEvent Class
HttpSessionEvent encapsulates session events. It extends EventObject and is generated when a change occurs to the session.

Servlet package in action

Why is HTTP protocol called as a stateless protocol?

A protocol is stateless if it can remember difference between one client request and the other.
HTTP is a stateless protocol because each request is executed independently without any knowledge of the requests that came before it.

Above is a pictorial presentation of how a stateless protocol operates. User first sends “request1” and server responds with “response1”.
When the same user comes back with “request2” server treats this as new user and has no idea that it’s the same user who has come with the request.
In short every request is a new request for the HTTP protocol so it’s called as a stateless protocol.

What are the different ways we can maintain state between requests?

√ URL rewriting √ Cookies √ Hidden fields √ Sessions

Read Full Post »


Servlets are small program which execute on the web server

Servlet Life Cycle

Interfaces in javax.servlet

Servlet Interface:-
This interface has the init( ), service( ), and destroy( ) methods that are called by the server during the life cycle of a servlet.

ServletConfig Interface
This interface is implemented by the servlet container. Servlet can access any configuration data when its loaded.

ServletContext Interface
It gives information about the environment, It represents a Servlet’s view of the Web Application.

ServletRequest Interface
The ServletRequest interface is implemented by the servlet container. It gives data regarding client request.

ServletResponse Interface
The ServletResponse interface is implemented by the servlet container Used to give response back to the client.

ServletInputStream Class
It is implemented by the servlet container and provides an input stream that a servlet developer can use to read the data from a client request.

ServletOutputStream Class
It is implemented by the servlet container and provides an output stream that a servlet developer can use to write data to a client response.

Servlet Exception Classes
javax.servlet defines two exceptions.
The first is ServletException, which indicates that a servlet problem has occurred.
The second is unavailableException, which extends ServletException. It indicates that a servlet is unavailable.

Architecture of a Servlet package:-

The basic architecture of the servlet packages is as shown below

At the top of all is the main servlet interface which is implemented by the generic servlet.
But generic servlet does not provide implementation specific to any protocol. HTTP
servlet further inherits from the generic servlet and provides HTTP implementation like
“Get” and “Post”. Finally comes our custom servlet which inherits from HTTP Servlet.

Read Full Post »

A specific set of rules used to communicate with each other through internet is called protocols.
A protocol is a set of rules that governs the communications between computers on a network.

Generic servlet Httpservlet

1. works for every protocol(FTP, TCP, SMTP, IP…) 1.works for only Http protocol.
2. Must have to override & only service method 2.we use doGet() & doPost().
3. It Can handle single request at a time. 3.It can handle many request at a time( bcoz its thread based )
(bcoz its process based one at a time)
4. The servlet that gets invoked with Common Gateway 4.The servlet that is invoked by HTTP protocal is know as HTTP Servlet.
Interface protocol(CGI) is known has Genaric Servlet.
5. Generic servlet doesnot maintain client session 5.Httpservlet maintains session.

Get Post

  1. the information is sent through URL and 1. The information is sent through the Headers
  2. it is visible to everyone 2. cant be visible to others.
  3. can sent only 2KB of information 3. can send unlimited data
  4. (bcoz it will go through URL) 4. (bcoz sent through Headers )
  5. used to get the data from the resources 5. Used to post or store date in DB

Read Full Post »