Servlet Java Interview Questions – Set 07

When init() and Distroy() will be called

  • init() is called whenever the servlet is loaded for the first time into the webserver.it performs certain one time activities which are required during the lifetime of the servlet.It may be some initialisation of variables or a database connection.
  • Destroy will be called whenever the servlet is removed from the webserver. Various resources which are held by the servlet will be released,database connections which were opened will be closed.Later the servlet is destroyed.

Is there a way I can set the inactivity lease period on a per-session basis

Typically, a default inactivity lease period for all sessions is set within your JSPengine admin screen or associated properties file. However, if your JSP engine supports the Servlet 2.1 API, you can manage the inactivity lease period on a per-session basis.This is done by invoking the HttpSession.setMaxInactiveInterval() method, right after the session has been created.

What is JSP technology

Java Server Page is a standard Java extension that is defined on top of the servlet Extensions. The goal of JSP is the simplified creation and management of dynamic Web pages. JSPs are secure, platform-independent, and best of all, make use of Java as a server-side scripting language.

Difference Between Forward and Include in JSP

The <jsp:forward> action enables you to forward the request to a static HTML file, a servlet, or another JSP.

<jsp:forward page=”url” />

The JSP that contains the <jsp:forward> action stops processing, clears its buffer, and forwards the request to the target resource. Note that the calling JSP should not write anything to the response prior to the <jsp:forward> action.

You can also pass additional parameters to the target resource using the <jsp:param> tag.

<jsp:forward page=”test.htm” >

<jsp:param name=”name1″ value=”value1″ />

<jsp:param name=”name2″ value=”value2″ />

</jsp:forward>

In this example, test.jsp can access the value of name1 using request.getParameter(“name1”).

To “include” another resource with a JSP, you have two options: the include directive and the include action.The include directive executes when the JSP is compiled, which parses any JSP elements in the included file for a static result that is the same for every instance of that JSP. The syntax for the include directive is

<@ include file=”some-filename” %>.

The include action, on the other hand, executes for each client request of the JSP, which means the file is not parsed but included in place. This provides the capability to dynamically change not only the content that you want to include, but also the output of that content. The syntax for the include action is <jsp:include page=”some-filename” flush=”true” />. Note that the flush attribute must always be included (in JSP 1.1) to force a flush of the buffer in the output stream.

You can also pass parameters to the included file using the same process

<jsp:forward> action:

<jsp:include page=”template.htm” flush=”true” >

<jsp:param name=”name1″ value=”value1″ />

</jsp:include>

If a servlet is not properly initialized, what exception may be thrown

During initialization or service of a request, the servlet instance can throw an UnavailableException or a ServletException.

Why there is no constructor in servlet

  • Every java class will have aleast one constructor and servlets are no exception to this.
  • But while using servlets ,we never instantiate the servlet rather the container does it for us by using the newInstance() defined in the Class class;  which in turn uses the empty(Default) constructor to create a new instance of the servlet.
  • If we define parameterised constructor in the serlet ,then container will fail in instantiating the servlet.
  • The rest is left for you to decide whether a servlets need a user defined constructor or not.

Why to use Servlet

To develop a web application we need to handle multiple request and give the particular page, Servlet can handle multiple requests concurrently, and can synchronize requests

What do you understand by context initialization parameters

The context-param element contains the declaration of a web application’s servlet context initialization parameters.

Namevalue

The Context Parameters page lets you manage parameters that are accessed through the ServletContext.getInitParameterNames and ServletContext.getInitParameter methods.

How does a servlet communicate with a JSP page

The following code snippet shows how a servlet instantiates a bean and initializes it with FORM data posted by a browser. The bean is then placed into the request, and the call is then forwarded to the JSP page, Bean1.jsp, by means of a request dispatcher for downstream processing.

public void doPost (HttpServletRequest request, HttpServletResponse response)

{
try {

govi.FormBean f = new govi.FormBean();

String id = request.getParameter(“id”);

f.setName(request.getParameter(“name”));

f.setAddr(request.getParameter(“addr”));

f.setAge(request.getParameter(“age”));

//use the id to compute

//additional bean properties like info

//maybe perform a db query, etc.

// . . .

f.setPersonalizationInfo(info);
request.setAttribute(“fBean”,f);
getServletConfig().getServletContext()

.getRequestDispatcher(“/jsp/Bean1.jsp”).forward(request, response);

}

catch (Exception ex) {}

}

The JSP page Bean1.jsp can then process fBean, after first extracting it from the default requestscope via the useBean action.

  • <jsp:useBean id=”fBean”class=”govi.FormBean” scope=”request”/>
  • <jsp:getPropertyname=”fBean” property=”name”/>
  • <jsp:getProperty name=”fBean” property=”addr”/>
  • <jsp:getProperty name=”fBean” property=”age”/>
  • <jsp:getProperty name=”fBean” property=”personalizationInfo”/>

How can a servlet refresh automatically if some new data has entered the database

You can use a client-side Refresh or Server Push.