Servlets Flashcards

1
Q

What is a servlet? What about a servlet container? Which servlet container have you worked with?

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Describe the servlet class inheritance hierarchy. What methods are declared in each class or interface?

A

Servlet Interface -> Generic Servlet -> HttpServlet -> MyServlet

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How would you create your own servlet?

A
  1. Create a directory structure
  2. Create a Servlet
  3. Compile the Servlet
  4. Create a Deployment Descriptor
  5. Start the Server and Deploy the Project
  6. Access the Servlet
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the deployment descriptor? What file configures your servlet container?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Explain the lifecycle of a servlet. What methods are called and when are they called?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are some tags you would find in the web.xml file?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the difference between the ServletConfig and ServletContext objects? How do you retrieve these in your servlet?

A

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()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the purpose of the RequestDispatcher?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Explain the difference between RequestDispatcher.forward() and HttpServletResponse.sendRedirect()

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are some of the different ways of tracking a session with servlets?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is object mapping? Any Java libraries for this?

A

Mapping enables you to relate objects in your application to data in a database.

Libraries: 
Jackson
Dozer
MapStruct
ModelMapper
JMapper
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How would you send text or objects back in the body of the HTTP response from a servlet

A

Using PrintWriter to send HTML back to the browser

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the difference between getParameter() and getAttribute() methods?

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly