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.

Servlets: Cookie name is a reserved token

Topic:  Servlets 2.4

Application Server: JBoss 4.0.2 / Tomcat 5.5.9

Exception:

java.lang.IllegalArgumentException: Cookie name "ab,cd" is a reserved token
	javax.servlet.http.Cookie.<init>(Cookie.java:140)
	com.HelloWorldServlet2.doGet(HelloWorldServlet2.java:28)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:697)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
	org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:81)

Resolution:

Cookie 'names' can only consist of Alphanumeric characters and cannot contain the following: commas, semicolons, whitespace and they cannot start with the $ character.

Usage of the prohibited characters in the Cookie constructor for the first String argument which sets the name of the Cookies results in an IllegalArgumentException. Removal of the prohibited character/s removes this exception.

August 2, 2009

Servlets: A note on HttpSession Timeout

Setting Session Timeout Programmatically:

HttpSession session = request.getSession();
session.setMaxInactiveInterval(15 * 60); // timeout in seconds
// –1: session never expires, 0: session times out immediately 

Setting Session Timeout Declaratively/Configuratively:

In the web.xml:

<web-app ...>
...
   <session-config>
      <session-timeout>15<session-timeout> <!—Timeout in minutes -->
   </session-config>
</web-app>

Servlet: java.lang.IllegalStateException - Session already invalidated

Topic:  Servlets 2.4

Application Server: JBoss 4.0.2 / Tomcat 5.5.9

Exception:

java.lang.IllegalStateException: getCreationTime: Session already invalidated
 org.apache.catalina.session.StandardSession.getCreationTime(StandardSession.java:938)
 org.apache.catalina.session.StandardSessionFacade.getCreationTime(StandardSessionFacade.java:73)
 com.HelloWorldServlet2.doGet(HelloWorldServlet2.java:28)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:697)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
 org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:81)

Resolution:

Most HttpSession methods throw an IllegalStateException when the method is invoked on a HttpSession that has already been invalidated, that is, the invalidate() method has been invoked on the HttpSession object. The HttpSession object is still a valid object on the memory heap, however, not representing a legitimate client session.

This is different from the case, where the HttpSession object representing a particular client is invalidated and the object removed from the memory heap. This happens with the session times out or the HttpSession.getSession(false) returns null. In this case, a NullPointerException will be thrown on invoking a method on the HttpSession.

Remove the code that threw the exception and put it above the HttpSession.invalidate() to resolve this exception.

Servlet: Cannot forward after response has been committed

Topic:  Servlets 2.4

Application Server: JBoss 4.0.2 / Tomcat 5.5.9

Exception:

java.lang.IllegalStateException: Cannot forward after response has been committed
        at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationD
ispatcher.java:313)
        at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDis
patcher.java:301)
        at com.HelloWorldServlet.doGet(HelloWorldServlet.java:49)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:697)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Appl
icationFilterChain.java:252)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationF
ilterChain.java:173)
        at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFi
lter.java:81)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Appl
icationFilterChain.java:202)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationF
ilterChain.java:173)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperV
alve.java:213)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextV
alve.java:178)
        at org.jboss.web.tomcat.security.CustomPrincipalValve.invoke(CustomPrinc
ipalValve.java:39)
        at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(Securit
yAssociationValve.java:153)
        at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValv
e.java:59)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.j
ava:126)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.j
ava:105)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineVal
ve.java:107)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.jav
a:148)
        at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java
:856)
        at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.proce
ssConnection(Http11Protocol.java:744)
        at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpo
int.java:527)
        at org.apache.tomcat.util.net.MasterSlaveWorkerThread.run(MasterSlaveWor
kerThread.java:112)
        at java.lang.Thread.run(Thread.java:619)

Resolution:

If the response has already been committed (that is, the response output buffer has been flushed) in the forwarding web component (Servlet/JSP), then the forward will not take place and the invocation to RequestDispatcher.forward() will result in an IllegalStateException. The client will then receive this already committed response only.

Only one servlet can commit a response, either the forwarding servlet or the servlet where the request has been forwarded.

Either remove the call to RequestDispatcher.forward() in the forwarding Servlet or remove the println() calls on the response (the objects that write to the response) in the forwarding Servlet.

Servlet: getInputStream() has already been called for this request

Topic:  Servlets 2.4

Application Server: JBoss 4.0.2 / Tomcat 5.5.9

Exception:

java.lang.IllegalStateException: getInputStream() has already been called for this request
 org.apache.catalina.connector.Request.getReader(Request.java:1081)
 org.apache.catalina.connector.RequestFacade.getReader(RequestFacade.java:458)
 com.HelloWorldServlet.doGet(HelloWorldServlet.java:30)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:697)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
 org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:81)

Resolution:

The input from the request body can be either done with the BufferedReader object procured from the request.getReader() method or with the ServletInputStream object procured from the request.getInputStream() method, but not both, in the Servlet.

If the getReader() is already called on the request object, the call to getInputStream() on the same request object will throw an IllegelStateException and vice versa.

Servlet: getReader() has already been called for this request

Topic:  Servlets 2.4

Application Server: JBoss 4.0.2 / Tomcat 5.5.9

Exception:

java.lang.IllegalStateException: getReader() has already been called for this request
 org.apache.catalina.connector.Request.getInputStream(Request.java:934)
 org.apache.catalina.connector.RequestFacade.getInputStream(RequestFacade.java:328)
 com.HelloWorldServlet.doGet(HelloWorldServlet.java:34)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:697)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
 org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:81)

Resolution:

The input from the request body can be either done with the BufferedReader object procured from the request.getReader() method or with the ServletInputStream object procured from the request.getInputStream() method, but not both, in the Servlet.

If the getReader() is already called on the request object, the call to getInputStream() on the same request object will throw an IllegelStateException and vice versa.

Servlet: getOutputStream() has already been called for this response

Topic:  Servlets 2.4

Application Server: JBoss 4.0.2 / Tomcat 5.5.9

Exception:

java.lang.IllegalStateException: getOutputStream() has already been called for this response
 org.apache.catalina.connector.Response.getWriter(Response.java:596)
 org.apache.catalina.connector.ResponseFacade.getWriter(ResponseFacade.java:186)
 com.HelloWorldServlet.doGet(HelloWorldServlet.java:19)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:697)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
 org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:81)

Resolution:

The output to the response can be either done with the PrintWriter object procured from the respone.getWriter() method or with the ServletOutputStream object procured from the response.getOutputStream() method, but not both, in the Servlet.

If the getWriter() is already called on the response object, the call to getOutputStream() on the same response object will theow an IllegelStateException and vice versa.

Servlet: getWriter() has already been called for this response

Topic:  Servlets 2.4

Application Server: JBoss 4.0.2 / Tomcat 5.5.9

Exception:

java.lang.IllegalStateException: getWriter() has already been called for this response
 org.apache.catalina.connector.Response.getOutputStream(Response.java:565)
 org.apache.catalina.connector.ResponseFacade.getOutputStream(ResponseFacade.java:171)
 com.HelloWorldServlet.doGet(HelloWorldServlet.java:31)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:697)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
 org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:81)

Resolution:

The output to the response can be either done with the PrintWriter object procured from the respone.getWriter() method or with the ServletOutputStream object procured from the response.getOutputStream() method, but not both, in the Servlet.

If the getWriter() is already called on the response object, the call to getOutputStream() on the same response object will theow an IllegelStateException and vice versa.

HTTP Status 405 - HTTP method POST is not supported by this URL

Topic:  Servlets 2.4

Application Server: JBoss 4.0.2 / Tomcat 5.5.9

Exception:

HTTP Status 405 - HTTP method POST is not supported by this URL


type Status report

message HTTP method POST is not supported by this URL

description The specified HTTP method is not allowed for the requested resource (HTTP method POST is not supported by this URL).

Resolution:

Either you are sending a GET request from the browser with only a doPost() method in your Servlet class or the opposite – sending a POST request with only a doGet() in your Servlet class.

Servlet methods – doGet(), doPost() catch only the respective requests, so to handle a GET you need to put a doGet() and to receive a POST you need a doPost() in the Servlet class.

If you did put the right method, make sure you got the method signature as well as the method name right – its doGet() with a small 'd' and a capital 'G'. Same for doPost()

Servlets: java.lang.IllegalStateException with response.sendRedirect()

Topic:  Servlets 2.4

Application Server: JBoss 4.0.2 / Tomcat 5.5.9

Exception:

ERROR [[abcde]] Servlet.service() for servlet hello threw exception

java.lang.IllegalStateException
        at org.apache.catalina.connector.ResponseFacade.sendRedirect(ResponseFac
ade.java:423)
        at com.HelloWorldServlet.doGet(HelloWorldServlet.java:33)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:697)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Appl
icationFilterChain.java:252)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationF
ilterChain.java:173)
        at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFi
lter.java:81)
        at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(Appl
icationFilterChain.java:202)
        at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationF
ilterChain.java:173)
        at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperV
alve.java:213)
        at org.apache.catalina.core.StandardContextValve.invoke(StandardContextV
alve.java:178)
        at org.jboss.web.tomcat.security.CustomPrincipalValve.invoke(CustomPrinc
ipalValve.java:39)
        at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(Securit
yAssociationValve.java:153)
        at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValv
e.java:59)
        at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.j
ava:126)
        at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.j
ava:105)
        at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineVal
ve.java:107)
        at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.jav
a:148)
        at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java
:856)
        at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.proce
ssConnection(Http11Protocol.java:744)
        at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpo
int.java:527)
        at org.apache.tomcat.util.net.MasterSlaveWorkerThread.run(MasterSlaveWor
kerThread.java:112)
        at java.lang.Thread.run(Thread.java:619)

Resolution:

The java.lang.IllegalStateException exception thrown in the context of the response.sendRedirect() method indicates that the output response had already been committed after which the sendRedirect() method was invoked, i.e., after the out.close() statement

The solution is to either to put sendRedirect() before out.close() or better still, if a redirection is what was desired in any case, the user will not see any response that was sent, therefore to remove all statements that write to the Servlet output response.

Servlet: The server encountered an internal error () that prevented it from fulfilling this request

Topic:  Servlets 2.4

Application Server: JBoss 4.0.2 / Tomcat 5.5.9

Exception:

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

java.lang.NullPointerException
 com.HelloWorldServlet.doGet(HelloWorldServlet.java:15)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:697)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:810)
 org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:81)

Resolution:


The cause for this exception is the absence of an explicit call in the overloaded init() method that has the signature: public void init(ServletConfig config)

The first line of the method should be super.init(config) to remove this exception.

A better way to resolve this is to override the parameter-less init() method, the one that has the signature: public void init()

In this version of the init(), there is no need for the explicit super.init(config) call

HTTP Status 405 - HTTP method GET is not supported by this URL

Topic:  Servlets 2.4

Application Server: JBoss 4.0.2

Exception:

HTTP Status 405 - HTTP method GET is not supported by this URL


type Status report

message HTTP method GET is not supported by this URL

description The specified HTTP method is not allowed for the requested resource (HTTP method GET is not supported by this URL).

Resolution:

Either you are sending a GET request from the browser with only a doPost() method in your Servlet class or the opposite – sending a POST request with only a doGet() in your Servlet class.

Servlet methods – doGet(), doPost() catch only the respective requests, so to handle a GET you need to put a doGet() and to receive a POST you need a doPost() in the Servlet class.

If you did put the right method, make sure you got the method signature as well as the method name right – its doGet() with a small 'd' and a capital 'G'. Same for doPost()

Servlets: The requested resource is not available

Topic:  Servlets 2.4

Application Server: JBoss 4.0.2

Exception: The following message is displayed in the browser:

HTTP Status 404 -


type Status report

message

description The requested resource ( … ) is not available.

Resolution:

Either you are sending a GET request from the browser with only a doPost() method in your Servlet class or the opposite – sending a POST request with only a doGet() in your Servlet class.

Servlet methods – doGet(), doPost() catch only the respective requests, so to handle a GET you need to put a doGet() and to receive a POST you need a doPost() in the Servlet class.

If you did put the right method, make sure you got the method signature as well as the method name right – its doGet() with a small 'd' and a capital 'G'. Same for doPost()

Servlet mapping specifies an unknown servlet

Topic:  Servlets 2.4

Application Server: JBoss 4.0.2

Exception:

ERROR [org.apache.catalina.startup.ContextConfig] Parse error in application web.xml
java.lang.IllegalArgumentException: Servlet mapping specifies an unknown servlet name hello
    at org.apache.tomcat.util.digester.Digester.createSAXException(Digester.java:2719)
    at org.apache.tomcat.util.digester.Digester.createSAXException(Digester.java:2745)
    at org.apache.tomcat.util.digester.Digester.endElement(Digester.java:1060)
    at org.apache.xerces.parsers.AbstractSAXParser.endElement(Unknown Source)
    at org.apache.xerces.impl.dtd.XMLDTDValidator.endNamespaceScope(Unknown Source)
    at org.apache.xerces.impl.dtd.XMLDTDValidator.handleEndElement(Unknown Source)
    at org.apache.xerces.impl.dtd.XMLDTDValidator.endElement(Unknown Source)
    at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanEndElement(Unknown Source)
    at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDispatcher.dispatch(Unknown Source)
    at org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
    at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
    at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
    at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
    at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
    at org.apache.tomcat.util.digester.Digester.parse(Digester.java:1561)
    at org.apache.catalina.startup.ContextConfig.applicationWebConfig(ContextConfig.java:339)
    at org.apache.catalina.startup.ContextConfig.start(ContextConfig.java:1031)

Resolution:

This is a web.xml parse exception. Most probably, the <servlet-mapping> element has been put before the <servlet> element. Reverse the order of the 2 elements to make things work.

Servlets: ERROR [MainDeployer] could not start deployment

 

Topic:  Servlets 2.4

Application Server: JBoss 4.0.2

Exception:

13:09:51,265 ERROR [MainDeployer] could not start deployment: file:/C:/StechProj
ect/jboss-4.0.2/server/default/deploy/DeployedAppName.ear
org.jboss.deployment.DeploymentException: URL file:/C:/StechProject/jboss-4.0.2/
server/default/tmp/deploy/tmp5794225230259092371DeployedAppName.ear-contents/DeployedAppName-exp.war
/ deployment failed
        at org.jboss.web.tomcat.tc5.TomcatDeployer.performDeployInternal(TomcatD
eployer.java:356)
        at org.jboss.web.tomcat.tc5.TomcatDeployer.performDeploy(TomcatDeployer.
java:91)
        at org.jboss.web.AbstractWebDeployer.start(AbstractWebDeployer.java:357)

        at org.jboss.web.WebModule.startModule(WebModule.java:68)
        at org.jboss.web.WebModule.startService(WebModule.java:46)
        at org.jboss.system.ServiceMBeanSupport.jbossInternalStart(ServiceMBeanS
upport.java:272)
        at org.jboss.system.ServiceMBeanSupport.jbossInternalLifecycle(ServiceMB
eanSupport.java:222)
        at sun.reflect.GeneratedMethodAccessor70.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.jboss.mx.interceptor.ReflectedDispatcher.invoke(ReflectedDispatch
er.java:141)
        at org.jboss.mx.server.Invocation.dispatch(Invocation.java:80)
        at org.jboss.mx.server.Invocation.invoke(Invocation.java:72)
        at org.jboss.mx.server.AbstractMBeanInvoker.invoke(AbstractMBeanInvoker.
java:249)
        at org.jboss.mx.server.MBeanServerImpl.invoke(MBeanServerImpl.java:644)
        at org.jboss.system.ServiceController$ServiceProxy.invoke(ServiceControl
ler.java:897)
        at $Proxy0.start(Unknown Source)
        at org.jboss.system.ServiceController.start(ServiceController.java:418)
        at sun.reflect.GeneratedMethodAccessor9.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.jboss.mx.interceptor.ReflectedDispatcher.invoke(ReflectedDispatch
er.java:141)
        at org.jboss.mx.server.Invocation.dispatch(Invocation.java:80)
        at org.jboss.mx.server.Invocation.invoke(Invocation.java:72)
        at org.jboss.mx.server.AbstractMBeanInvoker.invoke(AbstractMBeanInvoker.

Resolution:

One possible resolution to this deployment issue on JBoss is an XML element error in the Web Deployment Descriptor – the web.xml

Go back to the web.xml and check the modification you did in the web.xml and check if you missed something small such as the '/' in the closing element or mis-typed the element name, etc.

A look in the JBoss log file, located in the JBoss/Server/Default/log/server.log file tells the exact, problematic element in the web.xml

Servlets: java.lang.ClassNotFoundException

 

Topic:  Servlets 2.4

Application Server: JBoss 4.0.2

Exception:

javax.servlet.ServletException: Wrapper cannot find servlet class com.HelloWorldServle or a class it depends on
 org.jboss.web.tomcat.security.CustomPrincipalValve.invoke(CustomPrincipalValve.java:39)
 org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:153)
 org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:59)
 org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
 org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
 org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856)
 org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:744)
 org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
 org.apache.tomcat.util.net.MasterSlaveWorkerThread.run(MasterSlaveWorkerThread.java:112)
 java.lang.Thread.run(Thread.java:619)
java.lang.ClassNotFoundException: com.HelloWorldServle
 org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1332)
 org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1181)
 org.jboss.web.tomcat.security.CustomPrincipalValve.invoke(CustomPrincipalValve.java:39)
 org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:153)
 org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:59)
 org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
 org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
 org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856)
 org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:744)
 org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
 org.apache.tomcat.util.net.MasterSlaveWorkerThread.run(MasterSlaveWorkerThread.java:112)
 java.lang.Thread.run(Thread.java:619)

Resolution:

The Servlet class name in the web.xml file is incorrect. Make sure the package name and the class name in the <servlet-class> element are exact, including the case.