i create a class called “Data” and call it in JSP.. when i comple it, the error says,
Data cannot be resolved to a type
2: pageEncoding="ISO-8859-1"%>
3: <%@ page import= " Data" %>
4: <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
5: <jsp:useBean id="user" class="Data" scope="session"/>
6: <jsp:setProperty name="user" property="*"/>
7: <html>
8: <head>
public class Data
{
String Classname ;
String Individualname;
String Link;
public void Setclassname(String value)
{
Classname = value;
}
public String GetClassname()
{
return Classname;
}
}
jsp file
<jsp:useBean id="user" class="Data" scope="session"/>
<jsp:setProperty name="user" property="*"/>
<body>
You entered<BR>
Class Name: <%= user.GetClassname() %><BR>
</body>
i didn’t deploy the project….
You don’t need
@page import. This is only linked into the scriptlet scope, not into taglibs. You don’t want to use scriptlets here. The following should work:Further, you should always put classes in a package whenever you’d like to import/reuse it in other classes. Packageless classes are not importable in other classes. Add a
packagedeclaration and put the class in the right location in the folder structure.When building manually, it should end up in
/WEB-INF/classes/com/example/Data.class. When building using an IDE, like Eclipse, put it insrc/com/example/Data.javaand the IDE will worry about compiling and putting it in the right location.You should also really fix the naming conventions of the variables and getters/setters. Follow the Java Naming Conventions.
Update as per the comments:
First: please, follow the Java Naming Conventions. Package names should not start with uppercase. Further, this error message means that there’s no getter for the named property. You should also really follow the Javabean specifications. Property names should start with lowercase, e.g.
propertynameand getter methods should bepublicand be named like sogetPropertyname(), aget, then first letter of propertyname uppercased and the remnant exactly the same as original propertyname. The same applies for setters like sosetPropertyname().Here’s a rewrite of your
Dataclass:Most IDE’s, for example Eclipse, can autogenerate Javabeans in this flavor. Take benefit of it.
See also: