Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Beans in JSP

 
Gaurav x Jain
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Friends

I am trying the following code using beans in JSP

TestBean.JSP
------------


<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!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">
<title>Test JSP BEAN</title>
</head>
<body>
<jsp:useBean id="Tbean" class="JSPTest.TestBean"/>
<jsp:getProperty name="emp" property="fname"/>
<jsp:getProperty name="emp" property="age"/>


</body>
</html>

TestBean.java
-------------
package JSPTest;

public class TestBean {
protected String fname;
protected int age;



public String getfname() {
fname = "nicks";
return fname;
}

public int getage() {
age = 27;
return age;
}

}
-------------

Facing following error:

org.apache.jasper.JasperException: jsp:getProperty for bean with name 'emp'. Name was not previously introduced as per JSP.5.3
org.apache.jasper.compiler.Generator$GenerateVisitor.visit(Generator.java:1086)
org.apache.jasper.compiler.Node$GetProperty.accept(Node.java:1124)
org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2361)
org.apache.jasper.compiler.Node$Visitor.visitBody(Node.java:2411)
org.apache.jasper.compiler.Node$Visitor.visit(Node.java:2417)
org.apache.jasper.compiler.Node$Root.accept(Node.java:495)
org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2361)
org.apache.jasper.compiler.Generator.generate(Generator.java:3416)
org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:231)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:347)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:327)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:314)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:589)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:317)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

Please help
 
Vic Hood
Ranch Hand
Posts: 477
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please use code tags for your code .
Thanks!
 
Mike Zal
Ranch Hand
Posts: 144
Fedora Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The name attribute of the getProperty tag must match the id attribute of the useBean tag. Your getter methods in TestBean.java should use camel casing, i.e. they should be getFname() and getAge(). If you make those changes you can just use El expressions ${TBean.fname} and ${TBean.age} instead of the getProperty tags. I believe you also need to a directive to include the TestBean.java file.
 
Gaurav x Jain
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you it's working now.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic