Tomcat7 and session



我的Tomcat7和我的应用程序有问题。

我会创建一个bean并设置一个属性。我会在另一个jsp(get)中使用相同的属性而不重新创建bean。我声明了作用域为"session"的bean,但当我尝试获取该属性时,它为null。为什么?我做错了什么?

在我的网络应用程序中,我有:

test1.jsp

call test2.jsp and pass the parameter "name"="mm"

test2.jsp

<jsp:useBean id="sBean" scope="session" class="my.package.SessionBean" />
<jsp:setProperty name="sBean" property="*" />

属性"name"的值直接为"mm"

test3.jsp

<jsp:useBean id="sBean" scope="session" class="my.package.SessionBean" />
<% sBean.getName() %>

属性"name"的值为NULL,而不是"mm"

public Sessionbean implements Serializable
{
  private String name;
  public SessionBean(){}
  //get and set of name
}

tomcat6中的相同功能可以完美地实现

我不知道为什么这在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>

我认为原始页面几乎都很好
test3.jsp中的scriptlet缺少等号(=)。它应该<%=sBeanetname()%>
它应该是()IS/GET)字段名(或伪字段名)一个公共方法名。包含"(SessionBean)session.getAttribute("sBean")"的答案有效。但是它没有使用JSP中包含的bean机制。因此,这是错误的。

最新更新