To find the Variables assigned in JSP page use Windows->Show View->Display

  • In debug perspective: Window -> Show View -> Display
  • Put a break point in your code

Lets have a expression like one below

 <c:if test="${user.isSuccess}">
 .
 . 
 </c:if>

Now i need to find the value of user.isSuccess you can run the below code in in Display to see the values
set in the userObject

_jspx_page_context.findAttribute("user") 

It takes the value from the Page context and displays the values stored in the User Object

Note : The pageContext, _jspx_page_context are different variable names used for based on IDE while debugging JSP page.

 <%@ page session="false" %>

One reason would be performance and memory. If you have a page that doesn’t need to be involved in a session (like say, an about.jsp or faq.jsp) then the default behaviour of involving every JSP in a session will impose the overhead of creating a new session object (if one doesn’t already exist) and increased memory usage as more objects reside on the heap.

This effect will be greatly exaggerated in case of a single page seeing high traffic from many unique users combined with a high bounce rate i.e. they users do not continue to browse but leave the site immediately after viewing that one page- the container will create a new session object per user which will never be used again and will ultimately be garbage collected after it times out – added over head of object creation, memory usage and garbage collection without giving you any real value.

Posted in JSP.
getParameter() getAttribute()
getParameter will return the value of a parameter that was submitted by an HTML form or that was included in a query string getAttribute returns an object that you have set in the request, the only way you can use this is in conjunction with a RequestDispatcher. You use a RequestDispatcher to forward a request to another resource
the return type for a parameter is a String The return type for attributes is an Object
The Scope of parameter is per individual request attribute is a server variable that exists within a specified scope
application, available for the life of the entire application
session, available for the life of the session
request, only available for the life of the request
page (JSP only), available for the current JSP page only
request.getParameter(“parameterName”) in java is used for accessing variable from JSP or HTML RequestDispatcher.forward(request, response) in java and request.getAttribute(“attributeName”) in jsp are used for accessing variables
Posted in JSP.

parent.jsp

<c:forEach var="instanceVar" items="${instanceList}">
    <jsp:include page="child.jsp">
        <jsp:param name="myVar" value="${instanceVar}"/>
    </jsp:include>
</c:forEach>

child.jsp

<c:out value="${param.myVar}"/>

Another alternative is using JSTL tag c:set and request scope.

<c:set var="instance" value="${your.value}" scope="request"/>
<jsp:include page="instance.jsp"/>

instance.jsp
You should take out the variable from request Scope

<c:out value="${instance}"/>
Posted in JSP.

Exception Handling in JSP

  1. By Using Error Page
  2. By Using PageContext
  3. By Using Try…Catch Block
  4. Specifying default error page in the web.xml

Using Error Page
To set up an error page, use the <%@ page errorPage="xxx" %> directive

<%@ page errorPage="ShowError.jsp" %>
<html>
<head>
   <title>Error Handling Example</title>
</head>
<body>
<%
   // Throw an exception to invoke the error page
   int x = 1;
   if (x == 1)
   {
      throw new RuntimeException("Error condition!!!");
   }
%>
</body>
</html>

Error-handling page includes the directive <%@ page isErrorPage="true" %>. This directive causes the JSP compiler to generate the exception instance variable.

<%@ page isErrorPage="true" %>
<html>
<head>
<title>Show Error Page</title>
</head>
<body>
<h1>Opps...</h1>
<p>Sorry, an error occurred.</p>
<p>Here is the exception stack trace: </p>
<pre>
<% exception.printStackTrace(response.getWriter()); %>
</pre>
</body>
</html>

Using JSTL tags in PageContext

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@page isErrorPage="true" %>
<table width="100%" border="1">
<tr valign="top">
<td width="40%"><b>Error:</b></td>
<td>${pageContext.exception}</td>
</tr>
<tr valign="top">
<td><b>URI:</b></td>
<td>${pageContext.errorData.requestURI}</td>
</tr>
</table>

Using Try Catch Block:

<html>
<head>
   <title>Try...Catch Example</title>
</head>
<body>
<%
   try{
      int i = 1;
      i = i / 0;
      out.println("The answer is " + i);
   }
   catch (Exception e){
      out.println("An exception occurred: " + e.getMessage());
   }
%>
</body>
</html>

Specifying default error page in the web.xml
web.xml

<error-page>
   <error-code>404</error-code>
   <location>/error404.html</location>
</error-page>

Sample.jsp

<%@ page errorPage="error.jsp" %>  
<%  
   String s=null;
   s.length();
%>

error.jsp

<%@ page isErrorPage="true" %>
<%= exception.getMessage() %>
Posted in JSP.

tag
The useBean action declares a JavaBean for use in a JSP. Once declared, the bean becomes a scripting variable that can be accessed by both scripting elements and other custom tags used in the JSP.

JSP Syntax

<jsp:useBean
        id="beanInstName"
        scope="page | request | session | application"       
        class="package.class"    
        type="package.class"
</jsp:useBean>

Example

  <jsp:useBean id="stock" scope="request" class="market.Stock" />

Accessing Values

<jsp:useBean id="date" class="java.util.Date" /> 
<p>The date/time is <%= date %>

Using getPropertyName() and setPropertyName()

<jsp:useBean id="students" 
                    class="com.tutorialspoint.StudentsBean"> 
   <jsp:setProperty name="students" property="firstName"
                    value="Zara"/>   
</jsp:useBean>

<p>Student First Name: 
   <jsp:getProperty name="students" property="firstName"/>
</p>

Setting Request Scope

PersonBean myBean = (PersonBean)request.getAttribute("myBean");

if(myBean == null)
{
   myBean = new PersonBean();
   request.setAttribute("myBean", myBean);
}

Accessing in JSP

<jsp:useBean id="myBean" class="PersonBean" scope="request" />
Posted in JSP.

The path with reference to root directory is called absolute. The path with reference to current directory is called relative.

Completely relative:

 <img src="kitten.png"/>   

Absolute in all respects:

<img src="http://www.foo.com/images/kitten.png">
Posted in JSP.
  1. Include directive includes the file at translation time (the phase of JSP life cycle where the JSP gets converted into the equivalent servlet) whereas the include action includes the file at runtime.
  2. If the included file is changed but not the JSP which is including it then the changes will reflect only when we use include action tag. The changes will not reflect if you are using include directive as the JSP is not changed so it will not be translated for request processing and hence the changes will not reflect.
  3. When using include action tag we can also pass the parameters to the included page by using param action tag but in case of include directive it’s not possible.
    <jsp:include page="file_name" />
     <jsp:param name="parameter_name" value="parameter_value" />
    </jsp:include>
    

JSP Include Action tag

<html>
<head>
<title>JSP include Action example</title>
</head>
<body>
<jsp:include page="display.jsp" />
</body>
</html>

JSP Include Directive

<html>
<head>
<title>JSP include Directive example</title>
</head>
<body>
<%@ include file="display.jsp" %>
</body>
</html>
Posted in JSP.

1.Utility Classes should have constants for variable

public class PolicyUtils 
{
 public static final String TOTAL_POLICY_LIMIT = "totalpolicylimit";
 requestHelper.setRequestAttribute(TOTAL_POLICY_LIMIT, policyLimit);
}

Since multiple Users can access the JSP page at same time assigning variable name to constant and changing it later makes it easy to change the actual variable name in case needed in future.

Posted in JSP.

Importing a Java Method in to JSP Page

package com.mugil.servlet;

public class Sample1 
{
  static int pincode = 600018;
  
  public String toString()
  {	
    return "600018";
  }
}
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ page import = "com.mugil.servlet.Sample1"%>    
 <body>
   <%=new Sample1()%>
 </body>

Note:
The following Code wont work

  <%=new Sample1();%>
  <% =new Sample1()%>