Home Java Technology JSP JSP Sample Interview Questions 5

Login Form




JSP Sample Interview Questions 5 Print E-mail

1. What is JSP? Describe its concept
Answer: JSP is a technology that combines HTML/XML markup languages and elements of Java programming Language to return dynamic content to the Web client, It is normally used to handle Presentation logic of a web application, although it may have business logic.

2. What are the lifecycle phases of a JSP?
Answer: JSP page looks like a HTML page but is a servlet. When presented with JSP page the JSP engine does the following 7 phases.

   1. Page translation: -page is parsed, and a java file which is a servlet is created.
   2. Page compilation: page is compiled into a class file
   3. Page loading : This class file is loaded.
   4. Create an instance :- Instance of servlet is created
   5. jspInit() method is called
   6. _jspService is called to handle service calls
   7. _jspDestroy is called to destroy it when the servlet is not required.

3. How can I implement a thread-safe JSP page? What are the advantages and Disadvantages of using it?
Answer: You can make your JSPs thread-safe by having them implement the SingleThreadModel interface. This is done by adding the directive <%@ page isThreadSafe="false" %> within your JSP page. With this, instead of a single instance of the servlet generated for your JSP page loaded in memory, you will have N instances of the servlet loaded and initialized, with the service method of each instance effectively synchronized. You can typically control the number of instances (N) that are instantiated for all servlets implementing SingleThreadModel through the admin screen for your JSP engine. More importantly, avoid using the tag for variables. If you do use this tag, then you should set isThreadSafe to true, as mentioned above. Otherwise, all requests to that page will access those variables, causing a nasty race condition. SingleThreadModel is not recommended for normal use. There are many pitfalls, including the example above of not being able to use <%! %>. You should try really hard to make them thread-safe the old fashioned way: by making them thread-safe

4. How does a servlet communicate with a JSP page
Answer: 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 request scope via the useBean action.

jsp:useBean id="fBean" class="govi.FormBean" scope="request"
/ jsp:getProperty name="fBean" property="name"
/ jsp:getProperty name="fBean" property="addr"
/ jsp:getProperty name="fBean" property="age"
/ jsp:getProperty name="fBean" property="personalizationInfo" /

5. Why does JComponent have add() and remove() methods but Component does not?

because JComponent is a subclass of Container, and can contain other components and jcomponents. How can I implement a thread-safe JSP page? - You can make your JSPs thread-safe by having them implement the SingleThreadModel interface. This is done by adding the directive <%@ page isThreadSafe="false" % > within your JSP page.

6. How can I get to print the stacktrace for an exception occuring within my JSP page
Answer: By printing out the exception’s stack trace, you can usually diagonse a problem better when debugging JSP pages. By looking at a stack trace, a programmer should be able to discern which method threw the exception and which method called that method. However, you cannot print the stacktrace using the JSP out implicit variable, which is of type JspWriter. You will have to use a PrintWriter object instead. The following snippet demonstrates how you can print a stacktrace from within a JSP error page:
    <%@ page isErrorPage="true" %>
    <%
    out.println("    ");
     PrintWriter pw = response.getWriter();
     exception.printStackTrace(pw);
    out.println(" ");
    %>

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

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

8. How can my JSP page communicate with an EJB Session Bean
Answer: The following is a code snippet that demonstrates how a JSP page can interact with an EJB session bean:
    <%@ page import="javax.naming.*, javax.rmi.PortableRemoteObject, foo.AccountHome, foo.Account" %>
    <%!
    //declare a "global" reference to an instance of the home interface of the session bean
    AccountHome accHome=null;
    public void jspInit() {
    //obtain an instance of the home interface
    InitialContext cntxt = new InitialContext( );
    Object ref= cntxt.lookup("javAnswer:comp/env/ejb/AccountEJB");
    accHome = (AccountHome)PortableRemoteObject.narrow(ref,AccountHome.class);
    }
    %>
    <%
    //instantiate the session bean
    Account acct = accHome.create();
    //invoke the remote methods
    acct.doWhatever(...);
    // etc etc...
    %>

 

9. How do I mix JSP and SSI #include
Answer: If you're just including raw HTML, use the #include directive as usual inside your .jsp file.

But it's a little trickier if you want the server to evaluate any JSP code that's inside the included file. If your data.inc file contains jsp code you will have to use
    <%@ vinclude="data.inc" %>
The is used for including non-JSP files.

10. 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.