我尝试学习Java EE,这是一个非常简单的例子:
在我的servlet中:
public class Servlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public Servlet() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String message1 = "Alo alo";
int mess2 = 3;
request.setAttribute("test", mess2);
request.setAttribute("aloMessage", message1);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
}
在我的jsp文件中:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
String getMessage = (String)request.getAttribute("aloMessage");
out.println( getMessage+" abc" );
%>
<br>
<%
Integer k = (Integer)request.getAttribute("test");
out.println( k+" abc" );
%>
</body>
</html>
结果如下: null abc null abc
我不知道为什么getAttribute返回值为null?
答案 0 :(得分:0)
可能是你的jsp页面正在丢失请求会话。
还有其他方法可以做到。
你可以在servlet中设置
request.getSession().setAttribute("test", mess2);
request.getSession().setAttribute("aloMessage", message1);
并在jsp中得到它像
String getMessage = (String)session.getAttribute("aloMessage");
out.println(getMessage +&#34; abc&#34;);