Java and JSP - java.lang.NoSuchMethodError? - java

How can I set property and get it on a jsp page?
my Hello.class,
package Foo;
public class Hello
{
private String message;
public String sayHello ()
{
return "Hello World!";
}
public String getMessage() {
return this.message;
}
public void setMessage(String x) {
this.message = x;
}
}
My index.jsp,
<html>
<head>
<title>Custom Class</title>
</head>
<body>
<%# page import="Foo.Hello" %>
<%
Hello hello = new Hello();
hello.setMessage("Hello There!");
out.print(hello.getMessage());
out.print(hello.sayHello());
%>
</body>
</html>
Error,
org.apache.jasper.JasperException: An exception occurred processing JSP page /custom-class/index.jsp at line 12
9: <%
10: Hello hello = new Hello();
11:
12: hello.setMessage("Hello There!");
13: out.print(hello.getMessage());
14: out.print(hello.sayHello());
15: %>
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:574)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:461)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause
javax.servlet.ServletException: java.lang.NoSuchMethodError: Foo.Hello.setMessage(Ljava/lang/String;)V
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:908)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:837)
org.apache.jsp.custom_002dclass.index_jsp._jspService(index_jsp.java:122)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:438)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause
java.lang.NoSuchMethodError: Foo.Hello.setMessage(Ljava/lang/String;)V
org.apache.jsp.custom_002dclass.index_jsp._jspService(index_jsp.java:103)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:438)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
I'm new to Java and JSP. What have done incorrectly?

I don't find any problem in your code it is working fine i think you have to clean your project and redeploy it on your server and also clean and restart your server then it will surely work.

For people who encounter this problem in the future:
I just had the same problem and I removed tomcat server from "IntelliJ IDEA > Preferences > Application Servers" and I added it again. and it works fine now.

Related

The method getBuild() is undefined for the type MvcUriComponentsBuilder.MethodArgumentBuilder

Problem on building URIs to Controllers and methods from views.
Using spring 4.1 ; My Controller
#Controller
#RequestMapping("/produtos")
public class ProductsController {
#Autowired
private ProductDAO productDAO;
#Transactional
#RequestMapping(method=RequestMethod.POST)
public ModelAndView save(Product product){
productDAO.save(product);
return new ModelAndView("redirect:produtos");
}
And my JSP
<form:form action="${spring:mvcUrl('PC#save').build()}" method="post" >
I get a error on JSP:
The method getBuild() is undefined for the type MvcUriComponentsBuilder.MethodArgumentBuilder
if i use the syntax http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-ann-requestmapping-uri-templates:
<form:form action="${spring:mvcUrl(''PC#save'').build()}" method="post" >
appears to have no error on JSP, but in web:
org.apache.jasper.JasperException: /WEB-INF/views/products/form.jsp (line: 20, column: 1) "${spring:mvcUrl(''PC#save'').build()}" contains invalid expression(s): javax.el.ELException: Failed to parse the expression [${spring:mvcUrl(''PC#save'').build()}]
org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:41)
org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:275)
org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:91)
org.apache.jasper.compiler.Validator$ValidateVisitor.getJspAttribute(Validator.java:1422)
org.apache.jasper.compiler.Validator$ValidateVisitor.checkXmlAttributes(Validator.java:1231)
org.apache.jasper.compiler.Validator$ValidateVisitor.visit(Validator.java:879)
org.apache.jasper.compiler.Node$CustomTag.accept(Node.java:1536)
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:464)
org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2376)
org.apache.jasper.compiler.Validator.validateExDirectives(Validator.java:1853)
org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:217)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:356)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:336)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:323)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:585)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:363)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:172)
org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:303)
org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1228)
org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1011)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:955)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:961)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:852)
javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
I also try to delete JSP error on eclipse
Add a taglib
<%# taglib uri="http://www.springframework.org/tags" prefix="spring" %>

Only a type can be imported. com.journaldev.model.Employee resolves to a package

index22.jsp
<%# page import="java.util.List" %>
<%# page import="com.journaldev.model.Employee" %>
<%# page import="com.journaldev.model.Person" %>
<%# page import="com.journaldev.model.Address" %>
when i am accessing index22.jsp on browser getting the following error:
Jul 12, 2015 8:53:37 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [jsp] in context with path [/JSTL] threw exception [Unable to compile class for JSP:
An error occurred at line: [15] in the generated java file: [C:\Users\piyush\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\work\Catalina\localhost\JSTL\org\apache\jsp\index22_jsp.java]
Only a type can be imported. com.journaldev.model.Employee resolves to a package
An error occurred at line: [16] in the generated java file: [C:\Users\piyush\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\work\Catalina\localhost\JSTL\org\apache\jsp\index22_jsp.java]
Only a type can be imported. com.journaldev.model.Person resolves to a package
An error occurred at line: [17] in the generated java file: [C:\Users\piyush\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\work\Catalina\localhost\JSTL\org\apache\jsp\index22_jsp.java]
Only a type can be imported. com.journaldev.model.Address resolves to a package
Stacktrace:] with root cause
org.apache.jasper.JasperException: Unable to compile class for JSP:
An error occurred at line: [15] in the generated java file: [C:\Users\piyush\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\work\Catalina\localhost\JSTL\org\apache\jsp\index22_jsp.java]
Only a type can be imported. com.journaldev.model.Employee resolves to a package
An error occurred at line: [16] in the generated java file: [C:\Users\piyush\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\work\Catalina\localhost\JSTL\org\apache\jsp\index22_jsp.java]
Only a type can be imported. com.journaldev.model.Person resolves to a package
An error occurred at line: [17] in the generated java file: [C:\Users\piyush\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\work\Catalina\localhost\JSTL\org\apache\jsp\index22_jsp.java]
Only a type can be imported. com.journaldev.model.Address resolves to a package
Stacktrace:
at org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:103)
at org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:366)
at org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:485)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:379)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:354)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:341)
at org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:657)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:357)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:314)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
I have also included some JSTL jars and using Tomcat7v
javax.servlet.jsp.jstl-1.2.1.jar
taglibs-standard-compat-1.2.5.jar
taglibs-standard-impl-1.2.5.jar
taglibs-standard-jstlel-1.2.5.jar
taglibs-standard-spec-1.2.5.jar
I guess getting this error because of JSTL I have included but not confirmed.
any idea about the error I am getting?
Employee.java
package com.journaldev.model;
public class Employee implements Person {
private String name;
private int id;
private Address address;
public Employee() {
}
#Override
public String getName() {
return this.name;
}
#Override
public void setName(String nm) {
this.name = nm;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Address getAddress() {
return address;
}
public void setAddress(Address address) {
this.address = address;
}
#Override
public String toString(){
return "ID="+id+",Name="+name+",Address="+address;
}
}
error was because of exclamation sign on project. I created new web dynamic project and it was running smoothly.

Tomcat JSP error 500

I have experienced this strange error, and now I will explain when this happens:
basically what I have to do is having an HTML form (or JSP, doesn't really matter) with a simple textfield and two buttons, one for submitting to the .jsp page and the other one is just to reset the textfield.
Everything is working fine, except when I try to write in the textfield something with spaces (example: "abc 123").
If I simply write "abc123" it doesn't give any error 500 though.
I hope some of you can help me, here are the sources:
<html>
<head>`<html>
<head>
<meta charset="ISO-8859-1">
<title>HTML Form</title>
</head>
<body>
<form name = "intoll" method = "post" action = "send.jsp">
Insert text: <input type = "text" name = "food" id = "food">
<br/>
<input type = "submit" name = "send" value = "Send">
<input type = "reset" name = "Reset">
</form>
</body>
send.jsp:
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# page import="com.athirat.intolleranze.foods.*" %>
<%
String food = request.getParameter("food").trim();
if (food != null && food != "") {
food = food.toLowerCase();
out.print(food);
NetMaster.init(food);
}
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body>
<%
try {
out.print(NetMaster.showContent());
} catch (Exception e) {
throw new Exception("String error");
}
%>
</body>
</html>
Thanks for helping me.
EDIT:
Stacktrace
giu 16, 2015 3:06:12 PM org.apache.catalina.core.StandardWrapperValve invoke
GRAVE: Servlet.service() for servlet [jsp] in context with path [/intoll] threw exception [An exception occurred processing JSP page /send.jsp at line 22
19: try {
20: out.print(NetMaster.showContent());
21: } catch (Exception e) {
22: throw new Exception("String error");
23: }
24: %>
25: </body>
Stacktrace:] with root cause
java.lang.Exception: Errore nella stringa
at org.apache.jsp.send_jsp._jspService(send_jsp.java:113)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:438)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:617)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:668)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1521)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1478)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
giu 16, 2015 3:23:56 PM org.apache.catalina.core.StandardWrapperValve invoke
GRAVE: Servlet.service() for servlet [jsp] in context with path [/intoll] threw exception [An exception occurred processing JSP page /send.jsp at line 22
19: try {
20: out.print(NetMaster.showContent());
21: } catch (Exception e) {
22: throw new Exception("String error");
23: }
24: %>
25: </body>
Stacktrace:] with root cause
java.lang.Exception: Errore nella stringa
at org.apache.jsp.send_jsp._jspService(send_jsp.java:113)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:438)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:617)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:668)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1521)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1478)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
This is the method showContent with it's called methods:
public static String showContent() throws Exception {
if (isFood())
return getContent();
else
return "LOG Creato: " + generateLogFile();
}
private static String generateLogFile() throws Exception {
Calendar cal = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
String logname = "foodLog_" + sdf.format(cal.getTime());
String logdir = ps.getProperty("logfolder");
File f = new File ("." + logdir + logname + ".log");
FileOutputStream fout = new FileOutputStream(f);
DataOutputStream dout = new DataOutputStream(fout);
System.out.println(logname);
System.out.println(logdir);
System.out.println(f.getParent());
dout.writeBytes(food + " non esistente, si prega di aggiungerlo");
fout.close();
return logname;
}
private static String getContent() {
String result = "{[";
#SuppressWarnings("resource")
Scanner sc = new Scanner(inputReader).useDelimiter("\\A");
result += sc.hasNext() ? sc.next() : "";
return result;
}
I solved:
showContent() throws an Exception, and I only need to catch that.
The error was caused because I used to throw a new exception in the try-catch of showContent(), something I couldn't do for the reason above.
Thanks everybody.

implementing static methods - need assistance

Working on a web application and implementing a class called Calculator that calculates certain things about your body to determine how to focus your diet.
I am implementing this caluclator but I am getting an error. Here is the error; and following it below is an example of the source code. If you could tell me where my problem is that would be incredibly appreciated.
The Error:
org.apache.jasper.JasperException: Unable to compile class for JSP:
An error occurred at line: 83 in the jsp file: /updateProfile.jsp
Calculator cannot be resolved
80: a6 = "checked";
81: }
82:
83: out.println(Calculator.percentBodyFat("f","145","38","38","38","27","27","27",67.0,"6.5"));
84:
85: String theName = request.getParameter("name");
86: if(theName != null)
Stacktrace:
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:102)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:331)
org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:457)
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: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)
The Source code:
import java.io.BufferedReader;
package hygeia;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;
public class Calculator {
public static double percentBodyFat(String sex, String w, String h1,
String h2, String h3, String a1, String a2, String a3, double height, String wr)
{
//statements
}
public static double leanBodyMass(double weight, double percentBodyFat)
{
//statements
}
public static double protein(double leanBodyMass, int activLevel)
{
//statements
}
}
It is able to compile but I cannot get it to work with the jsp.
It seems like it should be very simple.
You have to include the Calculator class in your JSP page
<%#page import="Calculator"%> <%-- with the complete package of Calculator class--%>
This does not compile, package should be at the top.
You may fix the problem by importing your Calculator class.
<%#page import="Calculator"%>

How can I remove NullPointerException in Struts1.x

I am new to STRUTS and am trying to work out simple example . When I execute the example I get the following error on the internet browser..
<< javax.servlet.ServletException: javax.servlet.jsp.JspException: Exception creating bean of class com.example.LoginForm >>
and in the Eclipse console see a similar error with additional line on Null pointer exception
<< SEVERE: Error creating form bean of class com.example.LoginForm
java.lang.NullPointerException >>
Listed below is part of my struts-config.xml
struts-config.xml - Extract
<struts-config>
< form-beans>
< form-bean name="loginRequest" type="com.example.LoginForm" />
</form-bean>
< /form-beans>
< !-- =========================================== Global Forward Definitions -->
< global-forwards>
< forward
name="welcome"
path="/Welcome.do"/>
< /global-forwards>
< !-- =========================================== Action Mapping Definitions -->
< action-mappings>
< action path="/login"
name="loginRequest"
type="com.example.LoginAction">
< forward name="success"
path="/success.jsp">
< /forward>
< forward name="failure"
path="/login.jsp">
< /forward>
</action>
<action path="/Welcome"
forward="/pages/Welcome.jsp"/>
< /action-mappings>
< !-- ======================================== Message Resources Definitions -->
< message-resources parameter="MessageResources" />
< /struts-config>
My JSP login.jsp
< %# taglib prefix="bean" uri="/WEB-INF/struts-bean.tld" %>
< %# taglib prefix="html" uri="/WEB-INF/struts-html.tld" %>
< bean:message key="title" />
< html:form action="login">
<bean:message key="login.username"/>
<html:text property="username"></html:text>
<br>
<bean:message key="login.password"/>
<html:text property="password"></html:text>
<br>
<html:submit>
<bean:message key="login.submit"/>
</html:submit>
< /html:form>
The LoginForm class
package com.example;
import org.apache.struts.action.ActionForm;
public class LoginForm extends ActionForm
{
private String username;
private String password;
public LoginForm()
{
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
The LoginAction class
package com.example;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class LoginAction extends Action
{
#Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception
{
LoginForm login = (LoginForm)form;
String name = login.getUsername();
String pass = login.getPassword();
if(name.equals("hello") && pass.equals("hello"))
return mapping.findForward("success");
else
return mapping.findForward("failure");
}
}
The MessageResources.properties class
title=Welcome
login.username=Username
loguin.password=Password
login.submit=Submit
Stack Trace
org.apache.struts.util.RequestUtils createActionForm
SEVERE: Error creating form bean of class com.example.LoginForm
java.lang.NullPointerException
=========== ======================
at org.apache.struts.config.FormBeanConfig.createActionForm(FormBeanConfig.java:289)
at org.apache.struts.util.RequestUtils.createActionForm(RequestUtils.java:259)
at org.apache.struts.util.RequestUtils.createActionForm(RequestUtils.java:213)
at org.apache.struts.taglib.html.FormTag.initFormBean(FormTag.java:526)
at org.apache.struts.taglib.html.FormTag.doStartTag(FormTag.java:503)
at org.apache.jsp.login_jsp._jspx_meth_html_005fform_005f0(login_jsp.java:122)
at org.apache.jsp.login_jsp._jspService(login_jsp.java:79)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
at java.lang.Thread.run(Unknown Source)
Jun 23, 2011 12:56:47 PM org.apache.struts.util.PropertyMessageResources loadLocale
WARNING: Resource org/apache/struts/taglib/html/LocalStrings_en_US.properties Not Found.
Jun 23, 2011 12:56:47 PM org.apache.struts.util.PropertyMessageResources loadLocale
WARNING: Resource org/apache/struts/taglib/html/LocalStrings_en.properties Not Found.
Jun 23, 2011 12:56:47 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet jsp threw exception <br/><br/>
javax.servlet.jsp.JspException: Exception creating bean of class com.example.LoginForm under form name loginRequest
at org.apache.struts.taglib.html.FormTag.initFormBean(FormTag.java:536)
at org.apache.struts.taglib.html.FormTag.doStartTag(FormTag.java:503)
at org.apache.jsp.login_jsp._jspx_meth_html_005fform_005f0(login_jsp.java:122)
at org.apache.jsp.login_jsp._jspService(login_jsp.java:79)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:374)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:849)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:454)
at java.lang.Thread.run(Unknown Source)
Can anyone figure out what I am doing wrong and why this error is occuring.
Thanks,
Ankit
You forgot a / in your action on html:form.
This is how you should call your action in the form.
<html:form action="/login">
Update, Make sure you clean all your XML documents including your JSP's
The following (examples) were not allowed in the XML validator:
< form-beans>
< /struts-config>
XML doesn't allow spaces inside the <></>, </> tags. This also applies to the tags too.
Update, I found the correct problem. It's your declaration for form beans in your struts-config.xml.
You have:
<form-beans>
<form-bean name="loginRequest" type="com.example.LoginForm" />
</form-bean>
</form-beans>
You're closing your <form-bean> twice.
Solution:
< form-beans>
<form-bean name="loginRequest" type="com.example.LoginForm" />
</form-beans>
Now, this must work. :-) (Sorry for the late reply, work demands me.)
Also, I suggest using an IDE such as Eclipse or NetBeans to do your Struts project (and not do this by hand-typing) especially for your XML declarations.
Check Your jsp.Its using login as action form instead of LoginForm.
Well that error is because of you have not configure you Struts-config.xml properly.
you have to give particular Form-Bean name when action of the Form is called Accordingly.
now look at you code you have give action like this.
< action path="/login" name="loginRequest" type="com.example.LoginAction">
< forward name="success" path="/success.jsp">< /forward>
but while u are calling POJO also together but you have to call that bean before you make POST action for putting any data into the user interaction.
so make one new action which can call bean class before you main action is called.
for example:
< action path="/loginPre" name="loginRequest" forward="/youloginformname.jsp">
</action>
that way problem will get solved.

Categories

Resources