Servlets Flashcards
What is a servlet? What about a servlet container? Which servlet container have you worked with?
A servlet is a class which responds to HTTP requests and responses. Servlets are used to implement web applications.
A servlet container handles the networking side by parsing HTTP requests, connection handling, etc (Tomcat)
Describe the servlet class inheritance hierarchy. What methods are declared in each class or interface?
Servlet Interface -> Generic Servlet -> HttpServlet -> MyServlet
How would you create your own servlet?
- Create a directory structure
- Create a Servlet
- Compile the Servlet
- Create a Deployment Descriptor
- Start the Server and Deploy the Project
- Access the Servlet
What is the deployment descriptor? What file configures your servlet container?
The deployment descriptor is the file used by the servlet container to define which servlets match up with which URL’s. It also defines which servlet or resource provides the landing page for the root of the service
web.xml is used to configure individual servlets. Resides in the WEB-INF directory
Explain the lifecycle of a servlet. What methods are called and when are they called?
The servlet is initialized by calling the init() method.
The servlet calls service() method to process a client’s request.
The servlet is terminated by calling the destroy() method.
Finally, servlet is garbage collected by the garbage collector of the JVM.
What are some tags you would find in the web.xml file?
servlet - contains declarative data of a servlet
servlet-name: Defines name of the servlet. Used to reference the servlet elsewhere
servlet-class: The full class name of the servlet
servlet-mapping: defines mapping between a servlet and url
url-pattern: pattern of the url that uses this servlet
What is the difference between the ServletConfig and ServletContext objects? How do you retrieve these in your servlet?
The ServletConfig parameters are specified for a particular servlet and are unknown to other servlets. It is used for intializing purposes. getServletConfig()
The ServletContext parameters are specified for an entire application outside of any particular servlet and are available to all the servlets within that application. It is application scoped and thus globally accessible across the pages. getServletContext()
What is the purpose of the RequestDispatcher?
The RequestDispatcher interface provides the facility of dispatching the request to another resource. This interface can also be used to include the content of another resource also
Explain the difference between RequestDispatcher.forward() and HttpServletResponse.sendRedirect()
A RequestDispatcher forward() is used to forward the same request to another resource whereas ServletResponse sendRedirect(), a web application sends a new request to a different url
What are some of the different ways of tracking a session with servlets?
Cookies - information sent by a servlet to a web browser
Hidden Form Field - hidden fields in the page that hold info
URL Rewritting - info is added at the end of the url
HttpSession - timed session that persists info between pages
What is object mapping? Any Java libraries for this?
Mapping enables you to relate objects in your application to data in a database.
Libraries: Jackson Dozer MapStruct ModelMapper JMapper
How would you send text or objects back in the body of the HTTP response from a servlet
Using PrintWriter to send HTML back to the browser
What is the difference between getParameter() and getAttribute() methods?
getParameter() returns http request parameters, those that are passed from client to the server
getAttribute() is for server-side usage only, you fill the request with attributes that you can use within the same request