Misra’s blog

JSP/ Servlet interview questions

Posted by mtwinkle on May 18, 2006

http://java.sun.com/j2ee/tutorial/1_3-fcs/doc/JSPIntro4.html

Q1. How can I enable session tracking in JSP for JSP pages if the browser has disabled cookies?

Q2. How do you call stored procedure from JSP?

Q4. JSP lifecycle.
Ans 4.
When a request is mapped to a JSP page, it is handled by a special servlet that first checks whether the JSP page’s servlet is older than the JSP page. If it is, it translates the JSP page into a servlet class and compiles the class. During development, one of the advantages of JSP pages over servlets is that the build process is performed automatically.
Translation and Compilation

1. If an instance of the JSP page’s servlet does not exist, the container:
a. Loads the JSP page’s servlet class
b. Instantiates an instance of the servlet class
c. Initializes the servlet instance by calling the jspInit method
2. Invokes the _jspService method, passing a request and response object.
If the container needs to remove the JSP page’s servlet, it calls the jspDestroy method.

Q5. What JSP lifecycle methods can I override?

Q6. How to do browser redirection from a JSP page?
A server-side script can cause the server to send an appropriate HTTP response with a status code in the 3xx range and a Location header with another URL. When the browser receives this response, location bar changes and the browser makes a request with the new URL.

In Java Servlet or JavaServer Pages (JSP), developers can use HttpServletResponse.sendRedirect(String url).

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {… response.sendRedirect(“/newUserLogin.do”);}

This sends a response with a 302 status code (“Found”) and a Location header with the new URL to the user agent. It is also possible to set another status code with response.sendError(int code, String message) with one of the constants defined in the interface javax.servlet.http.HttpServletResponse as status code.

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {… response.sendError(response.SC_MOVED_PERMANENTLY, “/newUserLogin.do”);}

If an application uses HttpServletResponse.encodeURL(String url) for URL rewriting because the application depends on sessions, the method HttpServletResponse.encodeRedirectURL(String url) should be used instead of HttpServletResponse.sendRedirect(String url). It is also possible to rewrite a URL with HttpServletResponse.encodeURL(String url) and then pass this URL to HttpServletResponse.sendRedirect(String url).

Q7. How does JSP handle runtime exceptions?



Q9. Is there a way to reference ‘this’ variable within a JSP page?

Q10. How to create custom tags?




Q13. How can I set a cookie and delete a cookie from a JSP page?

Q14. How do you connect a database from a JSP?

Q15. What is the key difference between using a and HttpServletResponse.sendRedirect()?

Q16. Explain ‘RequestDispatcher’
Ans16. RD is an object that recieves request from the client and sends them to any resource located on the server (servlet, HTML file, JSP file). It is created by the servlet container and is used as a wrapper for any server resource located at a particular path.
public interface RequestDispatcher
void forward(ServletRequest req, ServletResponse res)
void include(ServletRequest req, ServletResponse res)

Q17. Difference between forward and include of RequestDispatcher interface?
Ans17.
These are the two methods for delegating requests from the Servlet to any resource on the server.
forward-
1. Forwards the request from the servlet to another resource on the server.
2. This method allows one servlet to do prelim processing and other servlet to generate the response.
3. ‘forward’ should be called before the response has been committed to the client, otherwise ‘IllegalStateException’ is thrown.
4. Outputs the response only from the servlet to which the request has been forwarded.
5. Any objects that are bound to the request can be accessed by the forwarded resource.
For example,
RequestDispatcher rd = getServletContext().getRequestDispatcher(“/template.htm”); request.setAttribute(“user”, user);
rd.forward(request, response);

include-
1. includes the content of the resource in the response. In essence, it is programmatic SSI
2. The included servlet cannot change the response status code or set headers; any attempt to do so is ignored.
3. Shows the output of both the servlets.

For example, to include the file footer.html in a servlet:
RequestDispatcher rd = getServletContext().getRequestDispatcher(“/footer.html”);
rd.include(request, response);

Q18. How to delegate request from JSP’s?
Ans18.
jsp: forward ,, include directive and include action can be used to forward the request from a JSP to any resource on the server.
1. jsp:forward
Calling JSP should not write anything to the response before calling the jsp:forward
For example:
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 parameters name1 and name2 using request.getParameter(“name1”)
2. jsp:include
It allows jsp to include either a static or a dynamic file. It will include the resource every time you access the jsp.
The static file is parsed and content included in the calling jsp. The dynamic file acts on the request and sends the response to the calling jsp.
include file=%=url% is valid here (i.e. scriptlets allowed)
3. include directive
includes only static files. It will include only the file at compliation time.
The include directive is used to insert a file (JSP, HTML, text file, java file) inside a jsp at translation time.
include file=%=url% is not valid. (i.e. scriptlets not allowed)

Q19. ____________ Causes an object to be notified when it is bound to or unbound from a session
Ans19. HttpSessionBindingListener

Q20. How to instantiate a bean from the JSP?
Ans20. By using “jsp:useBean” tag. It works in the following manner:
1. Attempts to locate the bean with the scope and name you specify.
2. Defines an object reference variable with the name you specify.
3. If it finds the bean, stores a reference to it in the variable. If you specified type, gives the bean that type.
4. if it does not find the bean, instantiates it from the class you specify, storing a reference to it in the new variable. Also, it executes any ‘body’ tag inside the ‘jsp:useBean’ tags. If the Bean already exists and “jsp:useBean” locates it, the body tags have no effect.
5. class=”package.class” type=”package.class”
Instantiates a Bean from the class named in class and assigns the Bean the data type you specify in type. The value of type can be the same as class, a superclass of class, or an interface implemented by class. The class you specify in class must not be abstract and must have a public, no-argument constructor

The body of a jsp:useBean element often contains a element that sets property values in the Bean.

Q21. Explain the attribute “scope” in jsp:useBean

Ans21. scope=”page request session application”
The default value is page. The meanings of the different scopes are shown below:

page – You can use the Bean within the JSP page with the element or any of the page’s static include files, until the page sends a response back to the client or forwards a request to another file.

request – You can use the Bean from any JSP page processing the same request, until a JSP page sends a response to the client or forwards the request to another file. You can use the request object to access the Bean, for example, request.getAttribute(beanInstanceName).

session – You can use the Bean from any JSP page in the same session as the JSP page that created the Bean. The Bean exists across the entire session, and any page that participates in the session can use it. The page in which you create the Bean must have a page directive with session=true.

application – You can use the Bean from any JSP page in the same application as the JSP page that created the Bean. The Bean exists across an entire JSP application, and any page in the application can use the Bean.

Q22. Can a JSP page instantiate a serialized bean?

A22. Yes.

Q23. What page directive is used to prevent a JSP page from automatically creating a session?

Ans23. page session=”false”

Q24. How to implement a threadsafe JSP page?

Ans24. By making it implement the singleThreadModel interface. i.e. page isThreadSafe=”false”

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

Ans25.

response.setHeader(“Cache-Control”,”no-store”); //HTTP 1.1
response.setHeader(“Pragma”,”no-cache”); //HTTP 1.0
response.setDateHeader (“Expires”, 0); //prevents caching at the proxy server

Q27.

4 Responses to “JSP/ Servlet interview questions”

  1. Taking a look forward to peer you. 615569

  2. Wow, incredible weblog layout! How long have you been blogging for? you make blogging appear straightforward. The overall appear of your web site is excellent, as properly as the content material! xrumer 426950

  3. click here said

    Aw, this was an exceptionally good post. In concept I would like to spot in writing such as this moreover – spending time and actual effort to create a exceptional article but so what can I say I procrastinate alot by way of no indicates uncover a strategy to go completed. 924118

  4. Really fascinating information !Perfect just what I was looking for! 280837

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: