Servlet Java Interview Questions – Set 06

Difference between JSP include directive and JSP include action

  • <%@ include file=”filename” %> is the JSP include directive.At JSP page translation time, the content of the file given in the include directive is ‘pasted’ as it is, in the place where the JSP include directive is used. Then the source JSP page is converted into a java servlet class. The included file can be a static resource or a JSP page. Generally JSP include directive is used to include header banners and footers.The JSP compilation procedure is that, the source JSP page gets compiled only if that page has changed. If there is a change in the included JSP file, the source JSP file will not be compiled and therefore the modification will not get reflected in the output.
  • <jsp:include page=”relativeURL” /> is the JSP include action element.The jsp:include action element is like a function call. At runtime, the included file will be ‘executed’ and the result content will be included with the soure JSP page. When the included JSP page is called, both the request and response objects are passed as parameters.If there is a need to pass additional parameters, then jsp:param element can be used. If the resource is static, its content is inserted into the calling JSP file, since there is no processing needed.

What is HTTP session in servlets

Session tracking in Servlets is done by using Interface HttpSession. It helps to identify a client throughout many page requests or visiting a website and to store details about that client.

One of the recommended approaches is HTTP session. A request is being identified by the session which is originated from similar browser during the conversation time period. Same session could be shared by all servlets. The JSESSIONID gets generated by server and is passed via cookies to the client, Built in SSL mechanism or URL rewriting (if cookies get off). For minimizing the object’s size stored in session, care shall be taken.

To obtain the session in Java servlet proceed as following:

HttpSession session = request.getSession();

  • If user already has a session  the existing session is returned.
  • If no session exists a new one is created and returned.
  • If you want to know if this is a new session:

call the Session isNew() method.

How do you pass control from one JSP page to another

Use the following ways to pass control of a request from one servlet to another or one jsp to another.

  • First is RequestDispatcher object‘s forward method to pass the control.
  • Second is response.sendRedirect method.

How is JSP include directive different from JSP include action.

When a JSP include directive is used, the included file’s code is added into the added JSP page at page translation time, this happens before the JSP page is translated into a servlet. While if any page is included using action tag, the page’s output is returned back to the added page. This happens at runtime.

What is the difference between System.out & System.err output in a Servlet

System.out goes to ‘client side’ and is seen in browser, while System.err goes to ‘server side’ and is visible in error logs and/or on console.

What is the importance of the destroy() method in Servlet

  • The destroy() method is called only once at the end of the life cycle of a servlet.
  • 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.
  • After the destroy() method is called, the servlet object is marked for garbage collection.

It can be used to “clean up” environments such as database connections.

public void destroy()

{

// Finalization code…

}

What are the uses of Servlets

  • Servlets are implemented using java language so these have platform independent feature.
  • These are faster than CGI
  • These have declarative security management feature

It is server side component,so servlets inherit the security provided by web server

How to improve Servlet Performance

  • Cache static data using jspInit() method.
  • Release static data in jspDestroy() method.
  • To concatenate string use, StringBuffer.
  • Do not use println() method.
  • Do not use PrintWriter to send binary data. Use ServletOutputStream.
  • Always flush data in sections.
  • Use getLastModified() method to handle browser and server cache.
  • Use application server cache.
  • Use session in following order: HttpSession, Hidden fields, Cookies, URL rewriting.
  • Always remove HttpSession explicitly.
  • Disable Servlet auto reloading.

How do I prevent the output of my JSP or Servlet pages from being cached by the browser

You will need to set the appropriate HTTP header attributes to prevent the dynamic content output by the JSP page from being cached by the browser. Just execute the following scriptlet at the beginning of your JSP pages to prevent them from being cached at the browser. You need both the statements to take care of some of the older browser versions.

Can we use the constructor, instead of init(), to initialize servlet

Yes, of course you can use the constructor instead of init(). There’s nothing to stop you. But you shouldn’t. The original reason for init() was that ancient versions of Java couldn’t dynamically invoke constructors with arguments, so there was no way to give the constructur a ServletConfig. That no longer applies, but servlet containers still will only call your no-arg constructor. So you won’t have access to a ServletConfig or ServletContext.