我不确定为什么这适用于 tomcat6 而不是 tomcat7,但我认为如果你在 test3.jsp 中进行更改:
<% sBean.getName() %>
至:
<% SessionBean testBean = (SessionBean) session.getAttribute("sBean"); //try changing name of SessionBean too so it doesn't conflict with the useBean name
testBean.getName();
%>
它应该工作。或者,您可以使用:
<jsp:getProperty name="sBean" property="name" />
更新
我把两个 JSP 页面放在一起。我在 tomcat 7 中快速测试了它,它对我有用。虽然我没有做表格,但我认为这是一般的想法。你是这样设置的吗?
test1.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ page import="my.project.SessionBean" %>
<jsp:useBean id="sBean" scope="session" class="my.project.SessionBean" />
<!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>Insert title here</title>
</head>
<body>
<%
SessionBean testBean = (SessionBean) session.getAttribute("sBean");
testBean.setName("Nate");
pageContext.forward("test2.jsp"); //forward to test2.jsp after setting name
%>
<jsp:getProperty name="sBean" property="name" />
</body>
</html>
test2.jsp:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<jsp:useBean id="sBean" scope="session" class="my.project.SessionBean" />
<%@ page import="my.project.SessionBean" %>
<!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>Insert title here</title>
</head>
<body>
<p>page 2</p>
<p>from jsp tag</p>
<jsp:getProperty name="sBean" property="name" /><br />
<p> from scriptlet</p>
<%
SessionBean testBean = (SessionBean) session.getAttribute("sBean");
out.print(testBean.getName());
%>
</body>
</html>