Category Archives: Servlet and JSP

The method getJspApplicationContext(ServletContext) is undefined for the type JspFactory

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

An error occurred at line: [31] in the generated java file: [H:\eclipseWorkspace\J2EEspace\GlossaryPortal\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\work\Catalina\localhost\Spring3HibernateApp\org\apache\jsp\WEB_002dINF\views\index_jsp.java]
The method getJspApplicationContext(ServletContext) is undefined for the type JspFactory

Stacktrace:
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:103)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:366)
org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:476)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:378)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:353)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:340)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:657)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:357)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:238)
org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:264)
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1208)
org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:992)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:939)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:856)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:915)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:811)
javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:796)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

The below line from the generated java file causing the issue from _jspInit() method

_el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();

Previous to this exception, I was facing the below exception

java.lang.NoClassDefFoundError: javax/servlet/jsp/jstl/core/Config

To resolve this, I have downloaded the following JAR files from the web and placed it in /WEB_INF/lib folder

1) jsp-api-2.0.jar
2) jstl-1.2.jar

But you know, the jsp-api-2.0.jar is now lead to the exception “The method getJspApplicationContext(ServletContext) is undefined for the type JspFactory”.

Solution

We should refer the target runtime in our project, not copy some libraries of an arbitrary servletcontainer make/version into our project. It would make our project incompatible with servletcontainers of a different make and/or version.

As simple, if you are using Tomcat 7 as server, then refer the jsp-api.jar from your Tomcat home directory’s lib folder. Don’t arbitrarily download some container version’s jsp=api file.

The project jar files should compatible with your servlet containers, which is the Tomcat Server that you are using as the Target Runtime.

So

You need to reference a server runtime in your project.

In Eclipse:

1) Choose “Project | Properties | Java Build Path | Libraries Tab”
2) Click “Add Library | Server Runtime | Apache Tomcat 7”
3) Click OK.
4) Clean your project, and build it again (“Project | Clean”, then “Project | Build All”)

That should do it!

Happy Learning ūüôā

java.lang.NoClassDefFoundError: Lorg/apache/commons/logging/Log;

Caused by: java.lang.NoClassDefFoundError: Lorg/apache/commons/logging/Log;
at java.lang.Class.getDeclaredFields0(Native Method)
at java.lang.Class.privateGetDeclaredFields(Unknown Source)
at java.lang.Class.getDeclaredFields(Unknown Source)
at org.apache.catalina.util.Introspection.getDeclaredFields(Introspection.java:106)
at org.apache.catalina.startup.WebAnnotationSet.loadFieldsAnnotation(WebAnnotationSet.java:270)
at org.apache.catalina.startup.WebAnnotationSet.loadApplicationServletAnnotations(WebAnnotationSet.java:139)
at org.apache.catalina.startup.WebAnnotationSet.loadApplicationAnnotations(WebAnnotationSet.java:65)
at org.apache.catalina.startup.ContextConfig.applicationAnnotationsConfig(ContextConfig.java:403)
at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:879)
at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:374)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:117)
at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5378)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
... 6 more
Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.Log
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)
... 20 more

Solution

Adding the commons-logging.jar to the class path will solve the problem.

Download the same from http://commons.apache.org/proper/commons-logging/download_logging.cgi

Happy Learning ūüôā

SEVERE: Servlet /Spring3HibernateApp threw load() exception java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet

I was working on the Spring MVC example. When I deployed my Spring web application into the Apache Tomcat 7 server, I end up with the following exception


Oct 12, 2014 8:24:01 PM org.apache.catalina.core.StandardContext loadOnStartup
SEVERE: Servlet /Spring3HibernateApp threw load() exception
java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)
at org.apache.catalina.core.DefaultInstanceManager.loadClass(DefaultInstanceManager.java:530)
at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:512)
at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:139)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1148)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1087)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5229)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5516)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1575)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1565)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

Solution

ClassNotFoundException should always makes you to think “JAR is missing”. That is, the eclipse could not able to locate the class file ‘DispatcherServlet” here. The ClassNotFoundException clearly indicates that you are missing org.springframework.web.servlet classes.

So make sure the Spring MVC JAR files is available in the classpath. The Spring JARs and all their dependencies should be in your /WEB-INF/lib.

Add all JAR files in the Project build path, which is, Right-click on Project -> Properties -> Java Build Path -> Libraries. Add all external JARS as like either user library or web app libraries. Click on “Add External JARs” to add all the JAR files.

Even after this, if the exception occurs, try the below instructions

1) Project Properties -> Deployment Assembly
2) Add -> Java Build Path Entries
3) You should now see the list of libraries on your build path that you can specify for inclusion into your finished WAR.
4) Select the ones you want and hit Finish.

Happy Learning ūüôā

Can not find the tag library descriptor for “http://java.sun.com/jsp/jstl/core”

When you get this error in eclipse or anywhere, make sure that you have the Apache’s standard taglib JARs, an implementation of the JSP Standard Tag Library (JSTL) specification,

Goto the following link

http://tomcat.apache.org/taglibs/standard/

and navigate to the

http://tomcat.apache.org/download-taglibs.cgi

and download the below JAR

taglibs-standard-impl-1.2.1.jar

Thanks!! ūüôā

Difference between MVC1 and MVC2

First of all there is no MVC1 pattern.  Surprised?

Initially, the server side web development was achieved using Servlet in the space of JAVA technology.  But developing web page response using Servlet technology was tedious and error-prone.  In Servlet, we use to write all presentation logic in out.print, as response.  So it was very difficult for web designers to maintain or alter the persentation logic code, written in Servlet. 

To overcome this issue, JSP technology arised.¬† With JSP, the web designer need not have JAVA knowledge.¬† It is very easy to maintain code in JSP.¬† Thanks to Scriplets and Custom tags.¬† We can have JAVA code, if needed to retrieve data, within scriptlet.¬†¬† But there is also drawback in JSP programming. Developers use to have all code logic in one scriptlet in JSP.¬† All code logic means database code, HTML (presentation) code and business logic or any Javascript validation logics.¬† Developers use to retrieve data from database with the help of bean class directly from JSP and render it the same.¬† This leads to lack of code reusability.¬† There is no separation between presentation and business logic.¬†¬† This concept has been called as “Model 1 JSP programming”.¬† Not MVC1.¬† Got it?

In order to achieve the separation, Trygve Reenskaug invented MVC pattern.  MVC pattern refers to separation of Model, View and Controller.   With this pattern, developers have to write Bean or POJO classes (Plain old JAVA class) in Model layer, with SERVLET as Controller which forwards the response to JSP technology to present to the users, View layer.

So there is a clear separation of business, mapping or contol and presentation logic. 

– User request goes to Contoller component (SERVLET).

РController (SERVLET) fetch required Models (POJO class) to accomplish business requirements.

РController (SERVLET) frames the response and forward the response to View component (JSP).

– View component (JSP), formats and renders the response to present it to the user.

 

This kind of programming has been called as “Model 2 JSP programming”.¬†¬† Sometimes called as “MVC2”.¬† This leads to confusion in the name that there exist MVC1.

But the fact is, there is no MVC1 or MVC2, only the MVC.

cool!!

JasperException: java.lang.NullPointerException: _el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();

I am learning about JSTL for the first time.  Here is my JSP code

<%@ page language=”java” contentType=”text/html; charset=ISO-8859-1″
pageEncoding=”ISO-8859-1″%>
<%@ taglib prefix=”c” uri=”http://java.sun.com/jsp/jstl/core&#8221; %>¬†¬† ¬†
<!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3.org/TR/html4/loose.dtd”&gt;
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=ISO-8859-1″>
<title>Insert title here</title>
</head>
<body>
<c:forEach var=”object” items=”${mylist}”>
    ${object}
</c:forEach>
</body>
</html>

I got red-line alert over the line uri=”http://java.sun.com/jsp/jstl/core&#8221;

To correct this, I included jsp-api.jar file first.  But error was persist.  Then I downloaded JSTL JAR files from Apache Tablib website http://tomcat.apache.org/taglibs/

and included @ /WebContent/WEB-INF/lib folder.

Here is my home page

index.jsp  (My home page)

</head>
<body>
<a href=”JSTLOneController”>JSTL One</a>
</body>
</html>

My /WebContent/WEB-INF/lib folder

jstl.jar

jsp-api.jar

standard.jar

When I run the application, I got below error

exception

org.apache.jasper.JasperException: java.lang.NullPointerException
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:522)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:356)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
root cause

java.lang.NullPointerException
org.apache.jsp.index_jsp._jspInit(index_jsp.java:22)
org.apache.jasper.runtime.HttpJspBase.init(HttpJspBase.java:52)
org.apache.jasper.servlet.JspServletWrapper.getServlet(JspServletWrapper.java:159)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:329)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

The above exception refers to the below line in the container generate Servlet of my index.jsp file
public void _jspInit() {
    _el_expressionfactory = _jspxFactory.getJspApplicationContext(getServletConfig().getServletContext()).getExpressionFactory();
_jsp_annotationprocessor = (org.apache.AnnotationProcessor) getServletConfig().getServletContext().getAttribute(org.apache.AnnotationProcessor.class.getName());
}

Tried hard to resolve the above exception and found that it is the mistake of that I included jsp-api.jar file.

Removing jsp-api.jar resolved the issue.

JasperException: Attribute value request.getParameter is quoted with ” which must be escaped when used within the value

My JSP Code

 

<jsp:useBean id=”person1″ type=”com.saravanan.domain.Person”
¬†¬†class=”com.saravanan.domain.Employee” >
¬†<jsp:setProperty name=”person1″ property=”empID” value=”<%= request.getParameter(“userID”) %>“/>
¬†<jsp:setProperty name=”person1″ property=”name” value=”<%= request.getParameter(“userName”) %>“/>
</jsp:useBean>

 

got below error

 

org.apache.jasper.JasperException: /TestBean.jsp (line: 12, column: 60) Attribute value¬† request.getParameter(“userID”)¬† is quoted with ” which must be escaped when

used within the value
 org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:42)
 org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:408)
 org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:89)
 org.apache.jasper.compiler.Parser.parseAttributeValue(Parser.java:280)
 org.apache.jasper.compiler.Parser.parseAttribute(Parser.java:229)
 org.apache.jasper.compiler.Parser.parseAttributes(Parser.java:162)
 org.apache.jasper.compiler.Parser.parseAttributes(Parser.java:153)
 org.apache.jasper.compiler.Parser.parseSetProperty(Parser.java:913)
 org.apache.jasper.compiler.Parser.parseStandardAction(Parser.java:1134)
 org.apache.jasper.compiler.Parser.parseElements(Parser.java:1451)
 org.apache.jasper.compiler.Parser.parseBody(Parser.java:1664)
 org.apache.jasper.compiler.Parser.parseOptionalBody(Parser.java:1002)
 org.apache.jasper.compiler.Parser.parseUseBean(Parser.java:958)
 org.apache.jasper.compiler.Parser.parseStandardAction(Parser.java:1136)
 org.apache.jasper.compiler.Parser.parseElements(Parser.java:1451)
 org.apache.jasper.compiler.Parser.parse(Parser.java:138)
 org.apache.jasper.compiler.ParserController.doParse(ParserController.java:242)
 org.apache.jasper.compiler.ParserController.parse(ParserController.java:102)
 org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:198)
 org.apache.jasper.compiler.Compiler.compile(Compiler.java:373)
 org.apache.jasper.compiler.Compiler.compile(Compiler.java:353)
 org.apache.jasper.compiler.Compiler.compile(Compiler.java:340)
 org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:646)
 org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:357)
 org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
 org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

 
Solution:
We should use single quotes on the value parameter
<jsp:useBean id=”person1″ type=”com.saravanan.domain.Person”
¬†¬†class=”com.saravanan.domain.Employee” >
¬†<jsp:setProperty name=”person1″ property=”empID” value=<%= request.getParameter(“userID”) %>/>
¬†<jsp:setProperty name=”person1″ property=”name” value=<%= request.getParameter(“userName”) %>/>
</jsp:useBean>

org.apache.jasper.JasperException: Cannot find any information on property in a bean of type

This is my bean class,

package com.saravanan.domain;

public class Student {
 
 String StudentName;

 public String getStudentName() {
  return StudentName;
 }

 public void setStudentName(String studentName) {
  StudentName = studentName;
 }

}

 
The below is my servlet’s doGet() method logic,

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  Student st = new Student();
¬†¬†st.setStudentName(“saravanan”);
¬†¬†request.setAttribute(“student”, st);
¬†¬†RequestDispatcher rd = request.getRequestDispatcher(“/display.jsp”);
  rd.forward(request, response);
 }

 
The below is my display.jsp that renders the content using jsp:useBean standard action tag,
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=ISO-8859-1″>
<title>display page</title>
</head>
<body>
<jsp:useBean id=”student” scope=”request”/>
<jsp:getProperty name=”student” property=”StudentName”/>
</body>
</html>

 
I got below error,

 
org.apache.jasper.JasperException: Cannot find any information on property ‘StudentName’ in a bean of type ‘com.saravanan.domain.Student’
 org.apache.jasper.runtime.JspRuntimeLibrary.getReadMethod(JspRuntimeLibrary.java:824)
 org.apache.jasper.compiler.Generator$GenerateVisitor.visit(Generator.java:1075)
 org.apache.jasper.compiler.Node$GetProperty.accept(Node.java:1126)
 org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2376)
 org.apache.jasper.compiler.Node$Visitor.visitBody(Node.java:2428)
 org.apache.jasper.compiler.Node$Visitor.visit(Node.java:2434)
 org.apache.jasper.compiler.Node$Root.accept(Node.java:475)
 org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2376)
 org.apache.jasper.compiler.Generator.generate(Generator.java:3489)
 org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:250)
 org.apache.jasper.compiler.Compiler.compile(Compiler.java:373)
 org.apache.jasper.compiler.Compiler.compile(Compiler.java:353)
 org.apache.jasper.compiler.Compiler.compile(Compiler.java:340)
 org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:646)
 org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:357)
 org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
 org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
 com.csc.ssivaji.controllers.StudentServlet.doGet(StudentServlet.java:37)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
 javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

 
The issue here is, Bean property names begin with lowercase letters,  It is potentially confusing that the initial lowercase letter by convention appears uppercase in

getter and setter names; for example, getStundentName() and setStudentName(String) would be the getter and setter for property “studentName”.

If Jasper couldn’t find the bean class then it would have said so; if it goes so far as to check the presence of a particular property then it has already introspected the bean class.

Doesnt matter if you created your properties in uppercase, but it is recommended that you have to makesure that property is in lower case.

 
Solution to fix the problem:

change property=”StudentName” to property=”studentName”

 

like below,

 

<jsp:useBean id=”student” scope=”request”/>
<jsp:getProperty name=”student” property=”studentName”/>

SendRedirect in Servlet – An example program

 

SenRedirect means lets the Servlet off from the work completely.  It does not handle the request anymore and just redirects the client browser to call someone.  The client browser makes the new call on behalf of user.

 

 

It’s a stupid thing if we try to invoke SendRedirect method when the response has already been sent.¬† Sent in the sense, that the data has been flushed to the stream.¬† It will throw IllegalStatException if we try to invoke this method after the response has been committed already.

The same rules applies to setting headers, status codes or content-types.