Servlet and JSP Interview Questions | Page 2

Question 11:

How to prevent caching for JSP page?

It is possible to prevent caching jsp response. To do so we need to add following headers in response :-

<% response.setHeader("Pragma", "No-cache"); 
response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate"); 
response.setDateHeader("Expires", -1); %>

NOTE: Pragma: no-cache prevents caching only when used over a secure connection, Expires: -1 should do the job over unsecure conection.

Question 12:

What is the difference between request.getParameter() and request.getAttribute()?

getParameter() - is used for getting the information you need from the Client's HTML page.

For example:-

request.getParameter("id") returns 1

getAttribute() -  is used for getting the parameters set previously in another or the same JSP or Servlet page.

For example :-

For example, we have first.jsp page

//First Page : first.jsp
 <%@ page import="java.util.*" import="*"%>
 <% request.setAttribute("ATTRIBUTE", "MyPage");%>
 <jsp:forward page="/second.jsp"/>

and second.jsp

<%@ page import="java.util.*" import="*"%>
 From Which Page : <%=request.getAttribute("ATTRIBUTE")%>
Question 13:

Is tomcat application server? If No, Why?

Tomcat is not an application server instead it is web server which implements Java-J2EE specification. Hence it is also known as servlet/jsp container.

Question 14:

What is a Web Server?

A server which is capable to handle HTTP request. In other words it processes the HTTP request sent by client, generates HTTP response and send back to client.
For example:- Apache Web Server
Question 15:

Name the default value of the scope atribute of <jsp:usebean>.

scope attribute has following values in <jsp:usebean>


The default value of scope is "page".

Question 16:

What does 404 status code represent in response?

The 404 or Not Found error message is a HTTP standard response code indicating that the client was able to communicate with a given server, but the server could not find what was requested.

Question 17:

What does 500 status code represent in response?

The 500 Internal Server Error is a generic error suggesting a problem with the website's server.

Question 18:

Can you override _jspService method in JSP?

Basically, if you tried to override the _jspService method, the code generated by the JSP compiler would end up with two copies of the method: the one you wrote and the one created by the compiler. This would result in a Java compilation error.

Question 19:

Can you override jspInit() and jspDestroy() method in JSP?

You can override the jspInit() and jspDestroy() methods within a JSP page. jspInit() can be useful for allocating resources like database connections, network connections, and so forth for the JSP page. It is good programming practice to free any allocated resources within jspDestroy(). The jspInit() and jspDestroy() methods are each executed just once during the lifecycle of a JSP page and are typically declared as JSP declarations:

<%!public void jspInit() {. . .}%>
<%!public void jspDestroy() {. . .}%>
Question 20:

How can we destroyed session in servlet?

Using session.invalidate() method we can destroy session in servlet.

Share interview questions


comments powered by Disqus


Social Media