Hello Java和JSP大师请帮我理清一下。我一直试图解决这个问题2个星期,但完全失败了,它真的给我很多压力。我创建了三个jsp文件和一个java bean,但是收到了一条错误消息。 这些是bin和类路径根。
CLASSPATH: C:\ Program Files \ Apache Software Foundation \ Tomcat 7.0 \ lib \ jsp-api.jar;
路径: C:\ glassfish3 \ jdk \ bin;
首先让我提供jsp文件,然后提供我一直在研究的java bean文件。我对jsp很新。
<HTML>
<BODY>
<FORM METHOD = POST ACTION = "SaveName.jsp">
What's your name ? <INPUT TYPE = TEXT NAME = username SIZE = 20 > <BR>
What's your e-mail?<INPUT TYPE = TEXT NAME = email SIZE = 20 > <BR>
What's your age ? <INPUT TYPE = TEXT NAME = age SIZE = 4 >
<P> <INPUT TYPE = SUBMIT >
</FORM>
</BODY>
</HTML>
form.jsp
<jsp : useBean id = "user" class="user.UserData" scope="session"/>
<jsp : setProperty name = "user" property = "*"/>
<HTML>
<BODY>
<A HREF="NextPage.jsp">Continue</A>
</BODY>
</HTML>
SaveName.jsp
<jsp : useBean id = "user" class = "user.UserData" scope = "session"/>
<HTML>
<BODY>
You entered<BR>
Name: <%= user.getUsername()%> <BR />
Email: <%= user.getEmail()%> <BR />
Age: <%= user.getAge()%> <BR />
</BODY>
</HTML>
NextPage.jsp上
我已将以上三个文件保存在以下根路径中: C:\ Program Files \ Apache Software Foundation \ Tomcat 7.0 \ webapps \ jsp pages
package user;
public class UserData
{
String username;
String email;
int age;
public void setUsername ( String value)
{
username=value;
}
public void setEmail ( String value)
{
email=value;
}
public void setAge ( int value)
{
age=value;
}
public String getUsername()
{
return username;
}
public String getEmail()
{
return email;
}
public int getAge()
{
return age;
}
}
UserData.java编译成UserData.class
我已经在上面的根文件夹中保存了上面的java bean(.java和.class文件),虽然我刚刚创建了classes文件夹,因为它不存在。 C:\ Program Files \ Apache Software Foundation \ Tomcat 7.0 \ webapps \ ROOT \ WEB-INF \ classes \ user
以下是错误代码。
HTTP Status 500 - Unable to compile class for JSP:
type Exception report
message Unable to compile class for JSP:
description The server encountered an internal error that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: Unable to compile class for JSP:
An error occurred at line: 5 in the jsp file: /NextPage.jsp
user cannot be resolved
2: <HTML>
3: <BODY>
4: You entered<BR>
5: Name: <%= user.getUsername()%> <BR />
6: Email: <%= user.getEmail()%> <BR />
7: Age: <%= user.getAge()%> <BR />
8: </BODY>
An error occurred at line: 6 in the jsp file: /NextPage.jsp
user cannot be resolved
3: <BODY>
4: You entered<BR>
5: Name: <%= user.getUsername()%> <BR />
6: Email: <%= user.getEmail()%> <BR />
7: Age: <%= user.getAge()%> <BR />
8: </BODY>
9: </HTML>
An error occurred at line: 7 in the jsp file: /NextPage.jsp
user cannot be resolved
4: You entered<BR>
5: Name: <%= user.getUsername()%> <BR />
6: Email: <%= user.getEmail()%> <BR />
7: Age: <%= user.getAge()%> <BR />
8: </BODY>
9: </HTML>
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:468)
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:728)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.47 logs.
Apache Tomcat/7.0.47
答案 0 :(得分:1)
你宣布:
<jsp : useBean id = "user" class="user.UserData" scope="session"/>
这意味着在HttpSession中有一个名为“user”的属性。
您编码:
<%= user.getUsername()%>
这意味着必须有名为'user'的JSP VARIABLE。所以你错了。
正确的方式:
${user.username}