This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes JSP and the fly likes Beans in JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Beans in JSP" Watch "Beans in JSP" New topic
Author

Beans in JSP

Gaurav x Jain
Ranch Hand

Joined: Feb 01, 2011
Posts: 39
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

Joined: Jan 05, 2011
Posts: 477

Please use code tags for your code .
Thanks!


Learning and Learning!-- Java all the way!
Mike Zal
Ranch Hand

Joined: May 04, 2011
Posts: 144

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.


OCJP6, OCWCD5
Gaurav x Jain
Ranch Hand

Joined: Feb 01, 2011
Posts: 39
Thank you it's working now.
 
jQuery in Action, 2nd edition
 
subject: Beans in JSP
 
Similar Threads
value for the useBean class attribute Invalid Problem
jspuseBean error??
jsp cannot call bean in websphere application server
Simple Jsp-Bean problem !!
Error In Bean PageScope