出于个人原因,我需要在 jsp 上回忆一些事情 :) 我有一个简单的登录页面:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Login</title>
</heaf>
<body>
<form action="LoginServlet.do" method="POST">
<p>
First name: <input type="text" size="20" name="first">
</p>
<p>
Last name: <input type="text" size="20" name="last">
</p>
<input type="submit" value="send">
</form>
</body>
</html>
豆类:
package web.model;
public class User {
private String firstName;
private String lastName;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
}
我从表单中收集数据,创建 User 实例,将其设置为请求属性并将请求转发到 result.jsp 页面:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
HttpSession session = request.getSession();
User user = new User();
user.setFirstName(request.getParameter("first"));
user.setLastName(request.getParameter("last"));
request.setAttribute("user", user);
RequestDispatcher view = request.getRequestDispatcher("result.jsp");
view.forward(request, response);
}
页面代码:
<%@ page
language="java"
contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"
import="web.utils.Utils, web.model.User"
%>
<!DOCTYPE html>
<html>
<head>
<title>Result</title>
</head>
<body>
<%= ((User) request.getAttribute("user")).getFirstName() %>
<jsp:useBean id="user" class="web.model.User" scope="request" />
<jsp:getProperty property="user" name="firstName" />
</body>
</html>
脚本代码正常工作,堆栈跟踪:
SEVERE: Servlet /Web threw load() exception
org.apache.jasper.JasperException: file:/result.jsp(15,0) jsp:getProperty for bean with name 'firstName'. Name was not previously introduced as per JSP.5.3
at org.apache.jasper.compiler.Generator$GenerateVisitor.visit(Generator.java:1110)
at org.apache.jasper.compiler.Node$GetProperty.accept(Node.java:1125)
at org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2375)
at org.apache.jasper.compiler.Node$Visitor.visitBody(Node.java:2427)
at org.apache.jasper.compiler.Node$Visitor.visit(Node.java:2433)
at org.apache.jasper.compiler.Node$Root.accept(Node.java:474)
at org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2375)
at org.apache.jasper.compiler.Generator.generate(Generator.java:3517)
at org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:250)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:373)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:353)
at org.apache.jasper.compiler.Compiler.compile(Compiler.java:340)
at org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:646)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:357)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
at org.apache.jasper.servlet.JspServlet.init(JspServlet.java:138)
at org.apache.catalina.core.StandardWrapper.initServlet(StandardWrapper.java:1280)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1193)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1088)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:5176)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5460)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.StandardContext.reload(StandardContext.java:3954)
at org.apache.catalina.loader.WebappLoader.backgroundProcess(WebappLoader.java:426)
at org.apache.catalina.core.ContainerBase.backgroundProcess(ContainerBase.java:1345)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1530)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1540)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1540)
at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1519)
at java.lang.Thread.run(Thread.java:722)
提前致谢