Servlets Flashcards

1
Q

What is a web server?

A

A Web server is a computer system that processes request by HTTP. This can refer to the hardware of the software that accepts the HTTP request. The web server store, process and deliver web pages to the clients.

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

What is an application server?

A

An application server is a software framework that provides both facilities to create web applications and a server environment to run them. The server behaves like an extended virtual machine for running applications, handling connections to the database on one side and often connections to the Web client on the other.

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

What is the difference between the application server and the web server?

A

Web server: serves content to the web using http protocol.

Application server: hosts and exposes business logic and processes.

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

What is a servlet?

A

A Java servlet is a Java program that extends the capabilities of a server. They are most often used to process or store a Java class in Java EE and are most often used in HTTP protocol. It is an object that receives a request and generates a response based on that request.

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

How do you deploy a servlet?

A

The servlet specification defines the element which can be specified in the deployment descriptor to make the servlet container load and initialize the servlet as soon as it starts up.

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

What is HTTP?

A

It is Hypertext Transfer Protocol, which is basically structured text.

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

What is a HTTP Request?

A

When your computer is on the browser and tries to fetch a file from a web server, it sends a request for some file.

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

What is a HTTP Response?

A

The Web Server sends back a response by sending back a file that can be used to display the web page.

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

What is Web.xml?

A

It is the configuration file of web application in Java. It instructs the servlet container which classes to load, what parameters to set in the context and how to intercept requests coming from the browsers.

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

What is HTTP GET

A

GET is used to retrieve remote data

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

What is HTTP Post

A

POST is used to insert/update remote data

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

Difference between HTTP GET and POST

A

GET is for retrieving data. It should have no side effects, you should be able to request the same URL over and over harmlessly.

POST is for writing data. It may have side effects. Making multiple identical write requests will likely result in multiple writes. Browsers typically give you warnings about this. POST is not secure. The data is in the body of the request instead of the URL but it is trivially simple to view/edit.

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

Servlet Class Hierarchy

A

Servlet Interface
Generic Servlet
HttpServlet
(User created Servlet)

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

What is the Servlet life cycle?

A

Init
Service
Destroy

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

What is the Servlet Init method?

A

The init() method simply creates or loads some data that will be used throughout the life of the servlet.

public void init() throws ServletException {
  // Initialization code...
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What is the Servlet Service method?

A

The service() method is the main method to perform the actual task. The servlet container (i.e. web server) calls the service() method to handle requests coming from the client( browsers) and to write the formatted response back to the client.

public void service(ServletRequest request,
ServletResponse response)
throws ServletException, IOException{
}

17
Q

What is the servlet destroy method?

A

This method gives your servlet a chance to close database connections, halt background threads, write cookie lists or hit counts to disk, and perform other such cleanup activities.

public void destroy() {
    // Finalization code...
  }
18
Q

What are the 4 scope in a servlet?

A

Page
Request
Session
Application

19
Q

What is the Page scope?

A

Page: ​The page scope restricts the scpoe and lifetime of attributes to the same page where it was created.

20
Q

What is the Request scope?

A

Request: Request scope start from the moment an HTTP request hits a servlet in our web container and end when the servlet is done with delivering the HTTP response.

21
Q

What is the Session scope?

A

Session: A session scope starts when a client (e.g. browser window) establishes connection with our web application till the point where the browser window is closed.

22
Q

What is the Application scope?

A

Application: Context scope or application scope starts from the point where a web application is put into service (started) till it is removed from service (shutdown) or the web application is reloaded. Parameters/attributes within the application scope will be available to all requests and sessions.

23
Q

What is instantiate?

A

Instantiation means defining or creating new object for class to access all properties like methods, fields, etc. from class.

24
Q

What is initialization?

A

Initialization means assigning initial value to variables after declaring or while declaring. All variables are always given an initial value at the point the variable is declared. Thus, all variables are initialized.

25
Q

What is RequestDispatcher.Forward?

A

A forward is performed internally by the application (servlet). the browser is completely unaware that it has taken place, so its original URL remains intact
any browser reload of the resulting page will simple repeat the original request, with the original URL

26
Q

What is HttpServletResponse.sendRedirect?

A

A redirect is a two step process, where the web application instructs the browser to fetch a second URL, which differs from the original
a browser reload of the second URL will not repeat the original request, but will rather fetch the second URL
redirect is marginally slower than a forward, since it requires two browser requests, not one
objects placed in the original request scope are not available to the second request.

27
Q

What is the difference between RequestDispatcher.Forward and HttpServletResponse.sendRedirect?

A

An important difference between RequestDispatcher.forward() and HttpServletResponse.sendRedirect() is that forward() is handled completely on the server side while redirect() sends a redirect message to the browser. In that sense, forward() is transparent to the browser while redirect() is not. Both javax.servlet.ServletContext and javax.servlet.ServletRequest have the method getReqeustDispatcher(path). Response.sendRedirect( ) does not forward the request and response objects, therefore request parameters will go out of scope. Response.sendRedirect can send a user outside of the context of the web application (to another site)

28
Q

What is a Web container?

A

A Web Container is the component of a web server that interacts with Java servlets. A web container is responsible for managing the lifecycle of servlets, mapping a URL to a particular servlet and ensuring that the URL requester has the correct access-rights.

29
Q

What is HTTP Servlet

A

HTTP Servlet: javax.servlet.http.HTTPServlet represents a HTTP capable servlet and is an abstract class that extends GenericServlet. It provides the support for HTTP protocol. It adds a new service () method with the signature: service (HttpServletRequest, HttpServletResponse). For every HTTP method, there is a corresponding method in the HTTPServlet class, e.g.; doGet (), doHead (), doPost (), doDelete (), doPut (), doOptions () and doTrace (). Depending on the HTTP method, the corresponding doXXX () method is called on the servlet.

30
Q

What is Generic Servlet?

A

GenericServlet: javax.servlet.GenericServlet class implements the Servlet interface. It is an abstract class that provides implementation for all methods except the service () method of the Servlet interface. We can extend this class and implement the service () method to write any kind of servlet.

31
Q

What is the Servlet API?

A
javax.servlet.Servlet is the central interface in the Servlet API. Every servlet class must directly or indirectly implement this interface. 
It has five methods: init (), service (), destroy (), getServletConfig () and getServletInfo (). The service (ServletRequest, ServletResponse) method handles requests and creates responses. The servlet container automatically calls this method when it gets any request for this servlet.
32
Q

What is the difference between Generic Servlet and HTTP Servlet?

A

GENERICSERVLET

  1. Can be used with any protocol (means, can handle any protocol). Protocol independent.
  2. All methods are concrete except service() method. service() method is abstract method.
  3. service() should be overridden being abstract in super interface.
  4. It is a must to use service() method as it is a callback method.
  5. Extends Object and implements interfaces Servlet, ServletConfig and Serializable.
  6. Direct subclass of Servet interface.
  7. Defined javax.servlet package.
  8. All the classes and interfaces belonging to javax.servlet package are protocol independent.
  9. Not used now-a-days.

HTTPSERVLET

  1. Should be used with HTTP protocol only (can handle HTTP specific protocols) . Protocol dependent.
  2. All methods are concrete (non-abstract). service() is non-abstract method.
  3. service() method need not be overridden.
  4. Being service() is non-abstract, it can be replaced by doGet() or doPost() methods.
  5. Extends GenericServlet and implements interface Serializable
  6. Direct subclass of GenericServlet.
  7. Defined javax.servlet.http package.
  8. All the classes and interfaces present in javax.servlet.http package are protocol dependent (specific to HTTP).
  9. Used always.
33
Q

How would you preload a servlet?

A

The servlet specification defines the element which can be specified in the deployment descriptor to make the servlet container load and initialize the servlet as soon as it starts up.

34
Q

What is a ServletConfig?

A

ServletConfig object is created by web container for each servlet to pass information to a servlet during initialization.This object can be used to get configuration information from web.xml file.

when to use : if any specific content is modified from time to time. you can manage the Web application easily without modifing servlet through editing the value in web.xml

It provides four methods: getInitParameter (String), Enumeration getInitParameterNames (), getServletContext () and getServletName ().

35
Q

What is a ServletContext?

A

ServletContext interface is a window for a servlet to view its environment. Every web application has one and only one ServletContext and it is available to all the servlets and filters of that application. It is also used by the servlets to share data with one another. A servlet can use this interface to get information such as initialization parameters for the web application or the servlet container’s version. This interface also provides utility methods for retrieving MIME type for a file, for retrieving shared resources, for logging and so forth.

36
Q

What are the different ways of handling a session?

A

Cookies, URL rewriting, and the session object.

URL Rewriting – We can append a session identifier parameter with every request and response to keep track of the session. This is very tedious because we need to keep track of this parameter in every response and make sure it’s not clashing with other parameters.

Cookies – Cookies are small piece of information that is sent by web server in response header and gets stored in the browser cookies. When client make further request, it adds the cookie to the request header and we can utilize it to keep track of the session. We can maintain a session with cookies but if the client disables the cookies, then it won’t work.

HttpSession getSession() – This method always returns a HttpSession object. It returns the session object attached with the request, if the request has no session attached, then it creates a new session and return it.

37
Q

How can you pre-initialize a servlet?

A

Using the ServletConfig object or override the no-arg init( ) method. Never override the init(ServletConfig) method, as this is used by the servlet container to initialize a servlet using web.xml init-param’s.

38
Q

How would you write a message back to the browser from inside a servlet?

A
PrintWriter out = response.getWriter();
    // Use out.println("…") to send content to the browser.