Q1. What is a servlet?
- Java Servlet is server-side technologies to extend the capability of web servers by providing support for dynamic response and data persistence.
- The javax.servlet and javax.servlet.http packages provide interfaces and classes for writing our own servlets.
- All servlets must implement the javax.servlet.Servlet interface, which defines servlet lifecycle methods. When implementing a generic service, we can extend the GenericServlet class provided with the Java Servlet API. The HttpServlet class provides methods, such as doGet() and doPost(), for handling HTTP-specific services.
- Most of the times, web applications are accessed using HTTP protocol and thats why we mostly extend HttpServlet class. Servlet API hierarchy is shown in below image.
Q2. What are the differences between Get and Post methods?
|Limited amount of data can be sent because data is sent in header.||Large amount of data can be sent because data is sent in body.|
|Not Secured because data is exposed in URL bar.||Secured because data is not exposed in URL bar.|
|Can be bookmarked||Cannot be bookmarked|
|It is more efficient and used than Post||It is less efficient and used|
Q3. What is Request Dispatcher?
RequestDispatcher interface is used to forward the request to another resource that can be HTML, JSP or another servlet in same application. We can also use this to include the content of another resource to the response.
There are two methods defined in this interface:
Q4. What are the differences between forward() method and sendRedirect() methods?
|forward() method||SendRedirect() method|
|forward() sends the same request to another resource.||sendRedirect() method sends new request always because it uses the URL bar of the browser.|
|forward() method works at server side.||sendRedirect() method works at client side.|
|forward() method works within the server only.||sendRedirect() method works within and outside the server.|
Q5. What is the life-cycle of a servlet?
There are 5 stages in the lifecycle of a servlet:
- Servlet is loaded
- Servlet is instantiated
- Servlet is initialized
- Service the request
- Servlet is destroyed
Q6. How does cookies work in Servlets?
- Cookies are text data sent by server to the client and it gets saved at the client local machine.
- Servlet API provides cookies support through javax.servlet.http.Cookie class that implements Serializable and Cloneable interfaces.
- HttpServletRequest getCookies() method is provided to get the array of Cookies from request, since there is no point of adding Cookie to request, there are no methods to set or add cookie to request.
- Similarly HttpServletResponse addCookie(Cookie c) method is provided to attach cookie in response header, there are no getter methods for cookie.
Q7. What are the differences between ServletContext vs ServletConfig?
The difference between ServletContext and ServletConfig in Servlets JSP is in below tabular format.
|Servlet config object represent single servlet||It represent whole web application running on particular JVM and common for all the servlet|
|Its like local parameter associated with particular servlet||Its like global parameter associated with whole application|
|It’s a name value pair defined inside the servlet section of web.xml file so it has servlet wide scope||ServletContext has application wide scope so define outside of servlet tag in web.xml file.|
|getServletConfig() method is used to get the config object||getServletContext() method is used to get the context object.|
|for example shopping cart of a user is a specific to particular user so here we can use servlet config||To get the MIME type of a file or application session related information is stored using servlet context object.|
Q8. What are the different methods of session management in servlets?
Session is a conversational state between client and server and it can consists of multiple request and response between client and server. Since HTTP and Web Server both are stateless, the only way to maintain a session is when some unique information about the session (session id) is passed between server and client in every request and response.
Some of the common ways of session management in servlets are:
- User Authentication
- HTML Hidden Field
- URL Rewriting
- Session Management API