Friday, June 3, 2011

JSP Short Notes for interview

 
  -------------------------------------------------------------------------------------------------------------------------
When isThreadSafe is set to true, multiple requests will go to JSP page. This is the default setting. If using true, you must ensure that you properly synchronize access to any shared objects defined at the page level. This includes

1) objects created within declarations

2) JavaBeans components with page scope

3) attributes of the page scope object.



If isThreadSafe is set to false, requests are dispatched one at a time. However, you still must ensure that access to attributes of the

1) application or session scope objects

2) JavaBeans components with application or session scope must be properly synchronized.
 
  ---------------------------------------------------------------------------------------------------

What are implicit object in jsp

1) request

2) response

3) page

4) session

5) application

6) pageContext

7) exception

8) config

9) out
 
------------------------------------------------------------------------------------------------------

Variables declared in <%! %> are instance variable of servlet class and can be accessible multiple thread.
------------------------------------------------------------------------------------------------------
Variables declared in  scriptlets and expressions are Local variables of the JSP page's servlet class.
in Page import directive <%@ page import="java.util.*, cart.*, com.sss.*" %> multiple imports are saperated by comma, and not by semicolon.
semicolon is not allowed within a JSP expression <%= %>
------------------------------------------------------------------------------------------------------
Can you define methods in declaratin tag

YES,

<%!

   private BookDBEJB bookDBEJB;
   public void jspInit() {

      ...

   }

   public void jspDestroy() {

      ...

   }

%>

 
=============================
How to handle exceptions in jsp
=============================
----------------------------------------------------------------------------------------------------- 
Case 1
-----------------------------------------------------------------------------------------------------
We can handle the exceptions in jsp by specifying errorPage in the page directive, <%@ page errorPage = "myErrorPage.jsp">

Here at the first line of jsp, we are intimating container that if any exception comes in this jsp, then control must go to myErrorPaeg.jsp
 
Funciton.jsp

<%@ page errorPage="="/WEB-INF/templates/myErrorPage.jsp" %>

<html>

<body><%
 int fno;

 int sno;  
   fno = Integer.parseInt(request.getParameter("fno")); 
    sno = Integer.parseInt(request.getParameter("sno"));

    int div=fno/sno;%>

<p>Division is : <%= div %></p><p><a href="form.html">Back</a>.</p></body>

</html>

Below is the source for myErrorPage.jsp. Here we have to declare isErrorPage="true" property, so that container will come to know that this jsp is capable of handling error

<%@ page isErrorPage="true" import="java.io.*" %>

</head><body<%= exception.toString() %><br></head>


        When we specify errorPage=true, we get access to variable exception. This exception variable will not be avaiable in other pages.
----------------------------------------------------------------------------------------------------- 
Case 2
-----------------------------------------------------------------------------------------------------

But the disadvantage of this method is you have to specify errorPage attribute of page directive for evey jsp that is repeatation of coding. To avoid this you can specify tag in web.xml

 <error-page>

    <exception-type>java.lang.Throwable</exception-type>

    <location>/WEB-INF/templates/errorPage.jsp</location>

  </error-page>

You can also assign error page jsp for HTTP error codes
<error-page>
                               <error-code>404</error-code>
                              <location>exceptions/Page404.jsp</location>
</error-page>
 
Many times we write custom tags in jsp, like struts-tag library, velocity templates, scriplet. These libraries throws JspException in their tag callback methods


 Home

No comments:

Post a Comment