Servlet Java Interview Questions – Set 08

What is JSP tag file

A source file containing a reusable fragment of JSP code that is translated into a tag handler when a JSP page is translated into a servlet.

What is JSP container

A container that provides the same services as a servlet container and an engine that interprets and processes JSP pages into a servlet.

What is the difference between and response.sendRedirect(url)?

The element forwards the request object containing the client request information from one JSP file to another file. The target file can be an HTML file, another JSP file, or a servlet, as long as it is in the same application context as the forwarding JSP file.

sendRedirect sends HTTP temporary redirect response to the browser, and browser creates a new request to go the redirected page. The response.sendRedirect also kills the session variables

What is JSP Implicit Objects

Certain objects that are available for the use in JSP documents without being declared first. These objects are parsed by the JSP engine and inserted into the generated servlet. The implicit objects re listed below:

  • request :It represents the request made by the client. The request implicit object is generally used to get request parameters, request attributes, header information and query string values.
  • response :The JSP implicit response object is an instance of a java class that implements the javax.servlet.http.HttpServletResponse interface. It represents the response to be given to the client. The response implicit object is generally used to set the response content type, add cookie and redirect the response.
  • pageContext : This is used to access page attributes and also to access all the namespaces associated with a JSP page. The lass or the interface name of the object PageContext is jsp.pageContext. The object PageContext is written: Javax.servlet.jsp.pagecontext The PageContext object has a page scope. It is an instance of the javax.servlet.jsp.PageContext class.
  • session :The session object has a scope of an entire HttpSession. It is an instance of the javax.servlet.http.HttpSession class. It represents the session created for the requesting client, and stores objects between client’s requests. The session object views and manipulates session information, such as the session identifier, creation time, and last accessed time. It also binds objects to a session, so that the user information may persist across multiple user connections.
  • application :The application object has an application scope. It is an instance of the javax.servlet.ServletContext class. It represents the context within which the JSP is executing. It defines a set of methods that a servlet uses to communicate with its servlet container.
  • out :The JSP implicit out object is an instance of the javax.servlet.jsp.JspWriter class. It represents the output content to be sent to the client. The out implicit object is used to write the output content.
  • config :The JSP implicit config object is an instance of the java class that implements javax.servlet.ServletConfig interface. It gives facility for a JSP page to obtain the initialization parameters available.
  • page :The Page object denotes the JSP page, used for calling any instance of a Page’s servlet. The class or the interface name of the Page object is jsp.HttpJspPage. The Page object is written: Java.lang.Object.
  • exception :The JSP implicit exception object is an instance of the java.lang.Throwable class. It is available in JSP error pages only. It represents the occured exception that caused the control to pass to the JSP error page.

Is Servlets thread-safe

Servlets are not thread safe. If you want to make it Servlet as Thread safe, you can implement SingleThreadInterface.

There are two different ways of making a servlet thread safe namely

  • By implementing SingleThreadModel:
    • By implementing a SingleThreadModel it will be possible to create a Thread safe servlet.There can only be one user at a given point of time.
  • Synchornize the part of sensitive code:

We can allow a single user at a given point of time by making that part of the code which is sensitive as synchronized

Difference between ServletContext and ServletConfig

ServletConfig: 

  • One ServletConfig Object is created per servlet
  • It can be used to access ServletContext
  • Parameters are configured in DD(deployment description)

ServletContext

  • One ServletContext will be created per web application.
  • Can be used to access web app parameter.
  • Can be used to get server Info.

Difference between the request attribute and request parameter

  • Request parameters are the result of submitting an HTTP request with a query string that specifies the name/value pairs, or of submitting an HTML form that specifies the name/value pairs. The name and the values are always strings. For example, when you do a post from html, data can be automatically retrieved by using getParameter(). Parameters are Strings, and generally can be retrieved, but not set.
  • Let’s take a real example, you have one html page named htmland JSP page named Register.jsp. The RegisterForm.html contains a form with few parameters for user registration on web application and those parameters you want to store in database.

<html>

<body>

<form name=”regform” method=”post” action=”../Register.jsp”>

<table ID=”Table1″>

<tr>

<td>

First Name : <input type=”text” name=”FIRSTNAME” size=”25″ value=””>

</td>

</tr>

<tr>

<td>

<input type=”Submit” NAME=”Submit” value=”Submit” >

</td>

</tr>

</table>

</form>

</body>

</html>

When user will enter first name in the text filed and press submit button, it will callRegister.jsp page. Now you want the value entered in text field by user in jsp/servlet so there you use request.getParameter() method.

<%

String lFirstName = request.getParameter(“FIRSTNAME”);

……………….

%>

On the server side, the request.getParameter() will retrieve a value that the client has submitted in the First Name text field. Using this method you can retrive only one value. This method i.e. getParameter method is in ServletRequest interface which is part of javax.servlet package.

Request attributes (more correctly called “request-scoped variables”) are objects of any type that are explicitly placed on the request object via a call to the setAttribute() method. They are retrieved in Java code via the getAttribute() method and in JSP pages with Expression Language references. Always use request.getAttribute() to get an object added to the request scope on the serverside i.e. using request.setAttribute().

Attributes are objects, and can be placed in the request, session, or context objects. Because they can be any object, not just a String, they are much more flexible. You can also set attributes programaticly and retrieve them later. This is very useful in the MVC pattern. For example, you want to take values from database in one jsp/servlet and display them in another jsp. Now you have resultset filled with data ready in servlet then you use setAttributemethod and send this resultset to another jsp where it can be extracted by using getAttributemethod.

Once a servlet gets a request, it can add additional attributes, then forward the request off to another servlet for processing. Attributes allow servlets to communicate with one another.

Life cycle methods in JSP

Life-cycle methods of the JSP are:

  • jspInit(): The container calls the jspInit() to initialize the servlet instance. It is called before any other method, and is called only once for a servlet instance.
  • _jspService():The container calls the _jspservice() for each request and it passes the request and the response objects. _jspService() method cann’t be overridden.

The container calls this when its instance is about to destroyed.The jspInit() and jspDestroy() methods can be overridden within a JSP page.

What is the difference between ServletContext and PageContext

ServletContext: Gives the information about the container

PageContext: Gives the information about the Request

Can you make use of a ServletOutputStream object from within a JSP page

No. You are supposed to make use of only a JSPWriter object (given to you in the form of the implicit object out) for replying to clients.

A JSPWriter can be viewed as a buffered version of the stream object returned by response.getWriter(), although from an implementational perspective, it is not.