Showing posts with label JSP. Show all posts
Showing posts with label JSP. Show all posts

August 28, 2011

JSP Life-cycle methods

The 3 life-cycle methods of a JSP are,
  • jspInit()
  • _jspService()
  • jspDestroy()
The jspInit() executes when the object of the JSP servlet class is created by the Container, right after the constructor finishes executing.

The jspDestroy() executes when the JSP Servlet object is removed or taken down by the Container.

The _jspService() method executes everytime a request is sent to the JSP. Code in scriptlets and expressions lands in the _jspService() method.

Code in the JSP declaration element lands outside the _jspService() method but inside the JSP Servlet class, after translation.

The jspInit() and jspDestroy() methods execute only once in the life of the JSP Servlet object. The _jspService() executes n times, where n = number of requests sent to the JSP

Create a JSP - JspLifecycleDemo.jsp - in the WebContent folder of the web application,

<html>
<head>
<title>JSP lifecycle methods</title>
</head>
<body>
<%!
public void jspInit() {
   System.out.println("JSP initialized"); 
}

public void jspDestroy() {
   System.out.println("JSP Destroyed"); 
}
%>

<%= "Inside _jspService() method"  %>

</body>
</html>

Test the code with the following URL in the browser,

http://localhost:8080/WebAppName/JspLifecycleDemo.jsp

Check the server/console log for the output of the jspInit() method. Stopping the server will display the output of the jspDestroy() on the server/console log.

JSP Implicit Objects Demo

The 9 JSP implicit objects are,
  • request (HttpServletRequest)
  • response (HttpServletResponse)
  • out (JspWriter)
  • session (HttpSession)
  • config (ServletConfig)
  • application (ServletContext)
  • page (Object)
  • pageContext (PageContext)
  • exception (Throwable)
Create a JSP - ImplicitObjectsDemo.jsp - in the WebContent folder of the web application,

<html>
<head>
   <title>Implicit Objects Demo</title>
</head>
<body>
<%= "test = " + request.getParameter("test") %>

<% response.setHeader("MyHeader", "MyValue"); %>
<br/><br/>
<%= "Session Id= " + session.getId() %>
<br/><br/>
<%= "Servlet API version supported (through ServletContext): " + application.getMinorVersion() + "." + application.getMinorVersion() %>
<br/><br/>
<%= "The JSP Servlet name (through ServletConfi) " + config.getServletName() %>
<br/><br/>
<%= "JSP Servlet class name (through page) is " + page.getClass().getName() %>

</body>
</html>

Test the code with the following URL in the browser,

http://localhost:8080/WebAppName/ImplicitObjectsDemo.jsp?test=abcd

June 26, 2011

Creating a JSP custom tag in a custom tag library (TLD)

To create your own JSP tag described in your own custom Tag Library Descriptor (TLD), do the following steps,

1) Create a Java class that will do the work of the tag when the tag is encountered in the JSP - MyTagHandler.java - in the src folder of the web application

package info.icontraining.jsp;

import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;

public class MyTagHandler extends SimpleTagSupport {

   private String value;
 
   public void setValue(String value) {
      this.value = value;
   }
 
   public String getValue() {
      return this.value;
   }
 
   public void doTag() throws JspException, IOException {
      getJspContext().getOut().write("Output from tag: ");
      getJspContext().getOut().write("Value is " + getValue());
   }
}

2) Create a TLD file - mytld.tld - in the WebContent/WEB-INF folder of the web application with the following configuration for the tag

<?xml version="1.0" encoding="UTF-8" ?>

<taglib xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
    version="2.1">
    
    <description>Test description</description>
    <display-name>My TLD</display-name>
    <tlib-version>1.1</tlib-version>
    <short-name>mine</short-name>
    <uri>myUriForMyTLD</uri>
    
    <tag>
        <description>My Tag Description</description>
        <name>testTag</name>
        <tag-class>info.icontraining.jsp.MyTagHandler</tag-class>
        <body-content>empty</body-content>
        <attribute>
              <description>My attribute description</description>
              <name>value</name>
              <required>true</required>
              <rtexprvalue>false</rtexprvalue>
        </attribute>
    </tag>
</taglib>

3) Create a JSP - myTagTest.jsp - and add it to the WebContent folder of the web application with the following code

<%@ taglib prefix="mine" uri="myUriForMyTLD" %>

<html>
<body>

<mine:testTag value="something" />

</body>
</html>

4) Test the code with the following URL in the browser,

http://localhost:8080/WebAppName/myTagTest.jsp

The include directive, JSP include action and JSP forward action

JSP Include Directive

The JSP include directive allows including another, outside JSP file (containing text or code) into a JSP. This allows creating modular pages and avoid duplication of content across JSPs. The insertion of the outside JSP content is done during the translation of the JSP to a Java class by the Container.

1) Create a JSP - includeDirectiveTest.jsp - in the WebContent folder of the web application

<html>
<body>

<%@ include file="header.jsp"  %>

This is the body<br/>

<%@ include file="footer.jsp"  %>

</body>
</html>

2) Create 2 JSPs - header.jsp & footer.jsp - also in the WebContent folder of the web application

header.jsp
This is the header<br/>

footer.jsp
This is the footer<br/>

3) Test the code with the following URL in the browser:

http://localhost:8080/WebAppName/includeDirectiveTest.jsp

JSP Include Action

The JSP include action works similar to the include directive with a fundamental difference - instead of inserting the contents of the included outside JSP into the including JSP (at translation time), the JSP include action inserts the response from the included JSP at runtime.

1) Create a new JSP - includeActionTest.jsp - in the WebContent folder of the web application

<html>
<body>
<jsp:include page="header.jsp" />

This is the body<br/>

<jsp:include page="footer.jsp" />

</body>
</html>

2) Test the code with the following URL in the browser:

http://localhost:8080/WebAppName/includeActionTest.jsp


JSP Forward Action

The JSP forward action forwards the request from one JSP page to another.

Similar to the JSP include action, the JSP forward action creates a RequestDispatcher with the page attribute value as an argument to the RequestDispatcher and then invokes the forward() method on the RequestDispatcher.

1) Create a JSP - forwardActionTest.jsp - in the WebContent folder of the web application

<html>
<body>
<jsp:forward page="anotherJsp.jsp" />

</body>
</html>

2) Test the code with the following URL in the browser:

http://localhost:8080/WebAppName/forwardActionTest.jsp

Accessing the forwardActionTest.jsp will cause anotherJsp.jsp to be rendered in the browser.

April 12, 2011

JSP Scripting Elements Demo


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%-- This is a JSP Comment. And above this is a JSP directive (page) --%>
<html>
<head>
<title>Scripting Elements Demo</title>
</head>
<body>
<p><% out.println("This is a scriplet. It can contain any Java code " +
" which lands up in the service method of the translated JSP Servlet"); %>
</p>
<br/>
<p><%= "This is a JSP Expression" %></p>
<br/>
<p><%! 
    public void m1() {
       System.out.println("Inside the method in a JSP Declaration");
    }
%>
</p>
<br/>
<% this.m1(); %>
</body>
</html>

Error Handling by Error codes in a Web Application

Error Handling in JSPs can be configured to set up custom error pages to be rendered to the response for a particular error code (of the status code) of the HTTP response message.

Status codes from 400 to 499 are designated as error codes.

1) Configure error handling for the error code 404 (resource not available) in the WebContent/WEB-INF/web.xml file, as follows,

<error-page>
    <error-code>404</error-code>
    <location>/my404page.jsp</location>
</error-page>


2) Create a custom error page for error code 404 - my404page.jsp - in the WebContent folder. Make sure that the isErrorPage attribute of the page directive is set to true to designate the page as an error page.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1" isErrorPage="true" %>
<html>
<head>
<title>My 404 Page</title>
</head>
<body>

<p>This is a custom 404 page</p>

</body>
</html>


3) Test the code by typing the following URL (of an unavailable JSP) in the browser,

http://localhost:8080/WebAppName/notthere.jsp

Note:
If the custom error page does not show up in Internet Explorer, do the following:
- Go to 'Tools' menu and under it 'Internet Options'
- Click on "Advanced"
- Scroll down and uncheck the checkbox, "Show friendly HTTP error messages"

If the custom error page does not show up in Google Chrome, do the following:
- Go to Options
- Click on "Under the Hood"
- Uncheck the checkbox, "Use a web service to help resolve navigation errors"

Exception Handling in a JSP


Write a JSP - test.jsp. Within a scriptlet in this JSP, write code that throws an Exception (for example: divide by zero Arithmetic Exception). Write another JSP - error.jsp. Display a message in this JSP that says "An unexpected error occured"
Make the error.jsp display when the client requests test.jsp

Solution


1) Create a JSP - test.jsp - in the WebContent folder with the following code that throws an ArithmeticException

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<html>
<head>
<title>Test Page</title>
</head>
<body>

<% int i = 3/0; %>
<%-- This line will throw an ArithmeticException --%>

</body>
</html>

2) Configure exception-handling for ArithmeticException in the WebContent/WEB-INF/web.xml file

<error-page>
   <exception-type>java.lang.ArithmeticException</exception-type>
   <location>/error.jsp</location>
</error-page>

3) Create an error.jsp JSP in the WebContent folder with the isErrorPage attribute of the page directive set to true. This JSP has access to the exception JSP implicit object since it is now a designated error page.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1" isErrorPage="true" %>
<html>
<head>
<title>Custom Error Page</title>
</head>
<body>

<p>This is a custom error page that displays when an ArithmeticException is thrown in any JSP</p>

<br/>
${pageContext.exception.message}

</body>
</html>

4) Test the code by typing the following URL in the browser

http://localhost:8080/WebAppName/test.jsp

Note: If the EL expression ${ ... } does not print anything, set the isELIgnored attribute of the page directive to false as follows,

<%@ page ... isELIgnored="false" %>

Java Server Pages (JSP) - Hello World Example

Create a new file - HelloWorld.jsp - in the WebContent folder of the web application and add the following code in it,

<html> 
<body> 
<%  int i = 3;  %> 
Writing the value of i = <%= i %>
</body> 
</html> 


Test the JSP by typing the following URL in the browser

http://localhost:8080/WebAppName/HelloWorld.jsp 

August 4, 2009

Mapping JSPs in web.xml

JSPs are internally translated to Java Servlets and their mapping is similar to that of Servlets. JSP mapping is not required (it is optional) and used to set JSP Initialization Parameters


<web-app ...>
 ...

   <servlet>
      <servlet-name>hello</servlet-name>
      <jsp-file>/hello.jsp</jsp-file>
      <init-param>
         <param-name>p1</param-name>
         <param-value>v1</param-value>
      </init-param>
   </servlet>
   <servlet-mapping>
      <servlet-name>hello</servlet-name>
      <url-pattern>/hello.jsp</url-pattern>
   </servlet-mapping>

...
</web-app>

JSP: Duplicate local variable session

Topic:  JSP 2.0

Application Server: JBoss 4.0.2 / Tomcat 5.5.9

Exception:

An error occurred at line: 10 in the jsp file: /Test.jsp
Generated servlet error:
Duplicate local variable session


	org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:84)
	org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:328)
	org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:397)
	org.apache.jasper.compiler.Compiler.compile(Compiler.java:288)
	org.apache.jasper.compiler.Compiler.compile(Compiler.java:267)
	org.apache.jasper.compiler.Compiler.compile(Compiler.java:255)
	org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:556)
	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:293)
	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
	org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:81)

Resolution:

There are 9 implicit objects available for use in JSP pages – these objects are out, request, response, session, application, config, exception, pageContext and page. Any attempt to declare variables within a JSP scriptlet with any of the 9 implicit object names results in a Duplicate local variable error. Changing the declared variable name will remove the error.

JSP: Syntax error on token(s), misplaced construct(s)

Topic:  JSP 2.0

Application Server: JBoss 4.0.2 / Tomcat 5.5.9

Exception:

An error occurred at line: 10 in the jsp file: /Test.jsp
Generated servlet error:
Syntax error on token(s), misplaced construct(s)

An error occurred at line: 10 in the jsp file: /Test.jsp
Generated servlet error:
Syntax error on token ""abcd"", delete this token


 org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:84)
 org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:328)
 org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:397)
 org.apache.jasper.compiler.Compiler.compile(Compiler.java:288)
 org.apache.jasper.compiler.Compiler.compile(Compiler.java:267)
 org.apache.jasper.compiler.Compiler.compile(Compiler.java:255)
 org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:556)
 org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:293)
 org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
 org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
 org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:81)

Resolution:

The JSP Declaration element starts with the sequence <%! – any code that goes within a JSP declaration lands up outside the service() method and within the translated JSP class. Therefore, this code should be a legitimate class level code, that is, either an instance variable declaration or a method definition. Program statements within a declaration element cannot be outside a method. Removal of program statements from the JSP declaration removes this exception.

JSP: Syntax error, insert ")" to complete Expression

Topic:  JSP 2.0

Application Server: JBoss 4.0.2 / Tomcat 5.5.9

Exception:

An error occurred at line: 8 in the jsp file: /Test.jsp
Generated servlet error:
Syntax error, insert ")" to complete Expression

An error occurred at line: 8 in the jsp file: /Test.jsp
Generated servlet error:
Syntax error on token ")", delete this token


 org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:84)
 org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:328)
 org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:397)
 org.apache.jasper.compiler.Compiler.compile(Compiler.java:288)
 org.apache.jasper.compiler.Compiler.compile(Compiler.java:267)
 org.apache.jasper.compiler.Compiler.compile(Compiler.java:255)
 org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:556)
 org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:293)
 org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
 org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
 org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:81)

Resolution:

The JSP Expression opening tag starts with the sequence '<%=' – the contents of the JSP Expression must always evaluate to a value (the return from the code within a JSP Expression should not be a void) and there should be no semi-colon or multiple program statements within the code. This is because the contents of a JSP Expression become an argument to the out.println() method after translation. For example,

<%= (String)request.getAttribute("abcd") %> gets translated to the following Java code:

out.println((String)request.getAttribute("abcd"));

Removal of the semi-colon or multiple program statements from within the JSP Expression will eliminate this exception.

August 3, 2009

JSP: Syntax error on token ";", delete this token

Topic:  JSP 2.0

Application Server: JBoss 4.0.2 / Tomcat 5.5.9

Exception:

An error occurred at line: 8 in the jsp file: /Test.jsp
Generated servlet error:
Syntax error on token ";", delete this token


	org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:84)
	org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:328)
	org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:397)
	org.apache.jasper.compiler.Compiler.compile(Compiler.java:288)
	org.apache.jasper.compiler.Compiler.compile(Compiler.java:267)
	org.apache.jasper.compiler.Compiler.compile(Compiler.java:255)
	org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:556)
	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:293)
	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
	org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:81)

Resolution:

The JSP Expression opening tag starts with the sequence '<%=' – the contents of the JSP Expression must always evaluate to a value (the return from the code within a JSP Expression should not be a void) and there should be no semi-colon within the code. This is because the contents of a JSP Expression become an argument to the out.println() method after translation. For example,

<%= (String)request.getAttribute("abcd") %> gets translated to the following Java code:

out.println((String)request.getAttribute("abcd"));

Removal of the semi-colon from within the JSP Expression will eliminate this exception.

JSP: Unterminated &lt;% tag

Topic:  JSP 2.0

Application Server: JBoss 4.0.2 / Tomcat 5.5.9

Exception:

org.apache.jasper.JasperException: /Test.jsp(1,1) Unterminated &lt;%@ page tag
	org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:39)
	org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:405)
	org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:130)
	org.apache.jasper.compiler.Parser.parseDirective(Parser.java:511)
	org.apache.jasper.compiler.Parser.parseElements(Parser.java:1543)
	org.apache.jasper.compiler.Parser.parse(Parser.java:126)
	org.apache.jasper.compiler.ParserController.doParse(ParserController.java:211)
	org.apache.jasper.compiler.ParserController.parse(ParserController.java:100)
	org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:146)
	org.apache.jasper.compiler.Compiler.compile(Compiler.java:286)
	org.apache.jasper.compiler.Compiler.compile(Compiler.java:267)
	org.apache.jasper.compiler.Compiler.compile(Compiler.java:255)
	org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:556)
	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:293)
	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
	org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:81)

Resolution:

A JSP element closing tag, the combination of % and > characters without whitespace between them is missing. Make sure the tags in the JSP are matched and that they are not inter-nested (a JSP element within another JSP element) or there is no whitespace between the 2 closing tag characters.

JSP: Syntax error on tokens, delete these tokens

Topic:  JSP 2.0

Application Server: JBoss 4.0.2 / Tomcat 5.5.9

Exception:

org.apache.jasper.JasperException: Unable to compile class for JSP

An error occurred at line: 1 in the jsp file: /Test.jsp
Generated servlet error:
Syntax error on tokens, delete these tokens

 org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:84)
 org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:328)
 org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:397)
 org.apache.jasper.compiler.Compiler.compile(Compiler.java:288)
 org.apache.jasper.compiler.Compiler.compile(Compiler.java:267)
 org.apache.jasper.compiler.Compiler.compile(Compiler.java:255)
 org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:556)
 org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:293)
 org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
 org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
 org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:81)

Resolution:

This Exception indicates a syntax error in a JSP Scripting element. In this particular example, a whitespace between the % and the @ characters of the JSP page directive caused the exception to be thrown.

Check the JSP directives as well as other JSP elements and make sure there are no spaces between the % and the respective element characters, as well as other JSP syntax is intact.

JSP: cannot be resolved or is not a type

Topic:  JSP 2.0

Application Server: JBoss 4.0.2 / Tomcat 5.5.9

Exception:

org.apache.jasper.JasperException: Unable to compile class for JSP

An error occurred at line: 7 in the jsp file: /Test.jsp
Generated servlet error:
ArrayList cannot be resolved or is not a type

An error occurred at line: 7 in the jsp file: /Test.jsp
Generated servlet error:
ArrayList cannot be resolved or is not a type


	org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:84)
	org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:328)
	org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:397)
	org.apache.jasper.compiler.Compiler.compile(Compiler.java:288)
	org.apache.jasper.compiler.Compiler.compile(Compiler.java:267)
	org.apache.jasper.compiler.Compiler.compile(Compiler.java:255)
	org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:556)
	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:293)
	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
	org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:81)

Resolution:

Just as in a normal Java Class, any collaborative classes (or dependencies) are required to be imported with the import statement above the class declaration, dependencies should be imported in the JSP as well.

The JSP page directive’s import attribute is used to import dependent packages/classes. Unlike in a Java Class where there can be multiple import statements, multiple dependencies in the JSP are indicated by separating them with commas as follows:

<%@ page import="java.util.ArrayList,java.io.*" %>

By convention, page directives are placed at the top of the JSP file, but they can be located anywhere within the page.

JSP: Syntax error, insert ";" to complete Statement

Topic:  JSP 2.0

Application Server: JBoss 4.0.2 / Tomcat 5.5.9

Exception:

org.apache.jasper.JasperException: Unable to compile class for JSP

An error occurred at line: 4 in the jsp file: /Test.jsp
Generated servlet error:
Syntax error, insert ";" to complete Statement


	org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:84)
	org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:328)
	org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:397)
	org.apache.jasper.compiler.Compiler.compile(Compiler.java:288)
	org.apache.jasper.compiler.Compiler.compile(Compiler.java:267)
	org.apache.jasper.compiler.Compiler.compile(Compiler.java:255)
	org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:556)
	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:293)
	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
	org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:81)

Resolution:

Java code within JSP scripting elements must comply with standard Java syntax. The exception indicates that the Web Container was unable to compile the translated JSP class because of the reason: Ending a Program statement with a semi-colon has been missed out in the line mentioned in the stack trace.