1. What is a servlet?
– Servlet is a Java program that runs on a Webserver.
– It is similar to an applet, but it runs on the server instead of the client machine.
2. What is a cookie?
A cookie is a piece of information stored on a client browser. A cookie has a name, value, and domain qualifiers.
3. What’s the distinction between PrintWriter and ServletOutputStream?
– PrintWriter is a Character Stream Class., used to write only character-based information.
– ServletOutputStream can be used to write primitive values. and bytes steam-based information
4. The most frequently used annotations in servlets ?
- @WebServlet – For Servlet Class
- @WebFilter – Used for Filtering Mechanism
5. Life Cycle of the Servlet?
- Servlet Loaded
- Servlet is instantiated
- Servlet is initialized (Init method invoked)
- Service the request (Service method is invoked)
- Servlet is destroyed (destroy method is invoked)
6. Difference between “GET” vs “POST”?
GET :
- Limit of data sent because data sent through the header
- Not Secured
- Can be bookmarked
POST :
- A large amount of data can be passed, sen through the body
- Secured
- Cant be bookmarked
7. DifferenceBetween GenericServlet and HTTPServlet ?
GenericServlet :
- Protocol Independent
- It supports only service() method
HTTPServlet :
- HTTP protocol specific
- It Supports doGet(), doPost()
8. What is servlet collaboration?
When one servlet communicates with another servlet is servlet collaboration.
- RequestDispatcher()
- SendRedirect()
9. Difference between RequestDispatcher vs SendRedirect?
RequestDispatcher :
- Dispatch the request from Servelt to another resource, resource can be servlet, jsp, html.
- Object created using request.getRequestDispatcher.
- two methods forward(), include().
- Send the same request to the destination
- Work withing the Server.
SendRedirect :
- sendReddirect is a method of HttpServletResponse. used to redirect the request to another servlet, JSP, HTML.
- Always send the new Request to the destination, because it uses the URL bar to send the request
- Even work outside of the servers, can be invoked 3rd party URL
- response.sendRedirect() will be used to send the request.
11. Difference Between ServletConfig vs ServletContext ?
ServletConfig:
- Servlet Container Creates the ServletConfig for every servlet.
- Configured in Web.xml, Will be loaded during Servlet initialization and will be passed to every servlet.
- Can be accessed by getServletConfig()
- Will be configured as below in Web.xml
<servlet>
<init-param>
<param-name>Email</param-name>
<param-value>forApplicant@xyz.com</param-value>
</init-param>
</servlet>
ServletContext:
– Servlet Container Creates the ServletContext for every application, so the information shared by the servlet context will be available for all the servlet of the application.
- Can be accessed by getServletContext()
- Will be configured as below in Web.xml
<context-param>
<param-name>Website-name</param-name>
<param-value>NewWebsite.tg</param-value>
</context-param>
12. What is servlet mapping?
The process of mapping between the URL Context and to a Servet is called servlet mapping. So servlet these mapping we are done through web.xml
13. What is Web.xml in Servlet?
Web.xml is a configuration file in Servlet which is used to do the mapping for the servlet. All the navigations and restrictions for Servlet Classes done thorough Web.xml
<web-app>
<servlet>
<servlet-name>homeServlet</servlet-name>
<servlet-class>com.example.HomeServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>homeServlet</servlet-name>
<url-pattern>*.*</url-pattern>
</servlet-mapping>
</web-app>
14. What are all the usages of Web.xml?
- Servlet Mappings
- Parameter Mappings
- Servet Context Mappings
- Load on Startup Control
- Filter Mappings
15. What is a servlet filter ?
Servlet Filter is a Section in Web.xml and it is optional to configure. These are not necessary for the functional operations of the applications.
Servlet Filters can be invoked before/after of processing of the request.
It is used for Security Mechanism(authentications, cache, cookies) applied through servlet filters. It sits on the top layer of the application.
16. What are the advantages of Servlet?
- We can have the Servlet Coding in a JSP file
- Servlet can create a view Page Dynamically and render it.
- It is pure java, So it can have all the specifications of java (Threading, Exception Handlings, etc)
- Easy to handle / Segregate the Business logic.
- Easy way to handle the URL Navigations
- Allows to create of a Web application with a pure Java Code
- Supports Security features with Filters.
17. What init() methods do in Servlet ?
It is a simple abstract class under HttpServlet. This will be invoked by the servlet container and called one time only.
The purpose of the init() method is to create the data that are required for the entire servlet lifecycle. and these data will be used across entire sessions. We can override this init() and do our initial setup here before starting our operations.
18. What is load-on-startup in Web.xml?
load-on-startup is an element in Web.xml, used to invoke the elements defined when the server begins with the first request. So whatever the action that we are required to load while initializing the server goes here.
<servlet>
<servlet-name>servlet1</servlet-name>
<servlet-class>com.exampl.HomeServlet</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>
The server will load the servlet based on the defined Integer value, 0 takes the highest priority.
19. What is Servlet Chaining ?
Process the client request through a Chain process is called Servlet Chaining. Servlet chaining can be achieved by include(), forward() methods that are used to link the communication between the servlets.
20. How to get the server and Client details in Servlet?
ServletRequest from HttpServlet provides the methods to get the server informations such as
- getServerName()
- getServerPort()
- getServerInfo()
- getAttribute()
To get the Client details
- getRemoteAddr()
- getRemoteHost()
21. What is javax.servlet ?
It is the root level package of the servlet API, it provides the interfaces and classes request for Servlet processes. Some of the interfaces are
- ServletRequest
- ServletResponse
- ServletConfig
- ServletContext
Servlet Classes are
- ServletInputStream
- ServletOutputStream
- GenericServlet
- ServletException