java中request.setAttribute???

来源:百度知道 编辑:UC知道 时间:2024/05/27 20:17:37
a.jsp
<%@ page contentType="text/html; charset=GBK" %>
<html>
<body bgcolor="#ffffff">
<%
request.setAttribute("name","Func Real");
out.println("name=" + request.getAttribute("name"));
%>
<a href="b.jsp">指向b.jsp</a>
</body>
</html>

b.jsp
<%@ page contentType="text/html; charset=GBK" %>
<html>
<body bgcolor="#ffffff">
<%
out.println("name=" + request.getAttribute("name"));
%>
</body>
</html>
为什么b.jsp上打印name为空???

你a页面没有把name设置到request作用域,所以在b页面取不到name的值...如果设置到了request作用域那么在b页面想要获取到值,必须a页面要转发过去,如果用<a href="b.jsp">指向b.jsp</a> 这样链接的话,就是url重写了,只能用request.getParameter("name");来获取了.
所以你要在a.jsp里把<a href="b.jsp">指向b.jsp</a>
改成一个表单提交,如:
a.jsp
<%@ page contentType="text/html; charset=GBK" %>
<html>
<body bgcolor="#ffffff">
<%
request.setAttribute("name","Func Real");
out.println("name=" + request.getAttribute("name"));
%>
<form ation="b.jsp"><input type="submit" value="提交"/></form>
</body>
</html>

如果没有的话,就在a.jsp里加上一段
<%
request.setAttribute("name","Func Real");
out.println("name=" + request.getAttribute("name"));
request.getRequestDispatcher("b.js