Servlets are Java classes that extend HttpServlets and it is used in server-side to create a dynamic web page. These special classes are able to handle requests from clients.HttpServlet extends servlet interface which has five abstract methods among them init(): is used for initialization of servlet, service(): is used to give an appropriate response, destroy(): is used to destroy servlet and close opened resources.
When a client sends a request to a web server, the webserver forwards the request to the web container. A web container is responsible to pass the request to the corresponding servlet as per URL pattern mapping, web container also manages the servlet life cycle. That servlet process the request according to business logic and generates an appropriate response and sends back to the webserver and the webserver return that response back to the client.
Servlet life cycle
- Servlet class Loading: When the web container receives the first request for a servlet, it loads the class into memory and calls the default no-arg constructor.
- Servlet class initialization: After loading, the container initialized the servlet context object for the servlet and then invoke its init() method and pass the context object to initialize variables.
- Request Handling: Once the servlet initialized it ready to handle client request and invoke service method with request and response object is passed by the web container. Create a separate thread for every request.
- Remove servlet: destroy() method is invoked.
Servlet attribute and their scope
The servlet attribute is used for inter servlet communication which means one servlet can call another servlet and also pass parameters.
- Request Scope: Using request dispatcher we can forward requests to another servlet and with the request, we can attach some parameters using request.setAttribute(key,value)
- Session scope: Session scope is specific to the browser, in the same session we can access parameters within any servlet and use those values.
- Application scope: We can set key-value pairs in context scope and use those within the whole application. We can configure context in deployment descriptor ie: web.xml or by using context instance context.setAttribute(key,value).
Difference between requestDispatcher and Send redirect
Both of these are used to dispatch request to other web pages, but if a servlet uses requestDispatcher then the client will not get any information about request dispatching but in case of send redirect first response will be received and then from the client, a new request will be generated for desired redirected web page. Another main difference is using requestDispatcher we can not redirect to any web page that is present in another web container but in send redirect we can also redirect to any resources present outside the current web container. In both, we can transfer variables for send redirect we can add a variable in URL only, but for requestDispatcher setAttribute method is used.
Why do we need JSP
Internally JSP page is converted into servlets only but still, it has some advantages which make them more advanced and easy to maintain and develop.
- Using JSP we can add java code inside HTML because it provides some tags, but in servlets also we can write HTML but it is a difficult task to design a complicated HTML page inside a servlet because we need to use print writer instance to write each line of HTML.
- Developers enjoy JSP implicit objects that are automatically available to developers by web container, and they can be called directly without being explicitly declared.
- In JSP we can use custom tags which reduce the length of code.
Different tags we can use inside the JSP page:-
- java Scriplets: We can write normal java code inside this tag and it is equivalent of writing code inside the service method of the servlet, the syntax is -: <% java code %>
- Declaration tag: We can write normal java code inside this tag but those codes will be placed outside the service method after JSP to Servlet conversion syntax:<%! java code %>
- Expression tag: It evaluates and converts the expression into a string. Syntax: <%= java code %>
- Directive tag: It is used to import a package or to include other JSP code or we can declare external tags. syntax: <%@ %>
Type of Directive tags:
a)@page: It defines JSP page dependent properties which communicate with the web Container at the time of translation. <%@page attributr=”value” attribute=”value” … %>, attributes can be language, extends, import,session,errorPage,isThreadSafe,info,contentType etc.
b)@Include: Can be used to include codes that are present in other files.
c)@Taglib: We can declare external tags and use those in JSP code.
JSP implicit objects
These objects can directly be used inside the JSP page without declaring it explicitly. They are created by the web container automatically and translated during the conversion of JSP to servlet page. There are 9 types of implicit objects are present.
a)out: is same as PrintWriter out=response.getWriter(); in servlet.
b)Request: We don’t need to define explicitly the request object like we use to do in the servlet service method.
c)Response: Like request, we don’t need to define explicitly response object.
d)Session: The session holding HttpSession session=req.getSession();
e)exception: we can use this if Directive tag attribute isErrorPage is true.
- In spite of all these advantages in JSP still, they have some disadvantages that's why we have to use both servlets and JSP pages, As JSP pages are finally converted to servlets that's why performance will be lowed in JSP. So it is advised to write only view pages in JSP and business logics in servlets.