我有4个jsp表单名称 Sample1.jsp,Sample2.jsp,Sample3.jsp,Sample4.jsp 我想通过Session将值从一个页面传输到另一个页面 我用这个方法
Sample1.jsp
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<form action="sample2.jsp" method="post">
<h1>Page1</h1>
<input type="text" name="name">
<input type="submit" value="Go">
</form>
</body>
Sample2.jsp 从Sample1.jsp获取值名称并重定向到sample3.jsp
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>page2</h1>
<%
String name = request.getParameter("name");
session.setAttribute("name",name);
out.println(name);
//response.sendRedirect("sample3.jsp");
request.getRequestDispatcher("sample3.jsp").forward(request, response);
%>
</body>
Sample3.jsp 从Sample2.jsp获取值名称,并且不在Sample3.jsp中显示锚标记超链接并直接跳转到Sample4.jsp
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Page 3</h1>
<%
String lname = (String)session.getAttribute("name");
out.println(lname);
//session.setAttribute("lname",lname);
%>
<a href="<%request.getRequestDispatcher("sample4.jsp").forward(request, response);%>" value=""><%out.println(lname);%> </a>
</body>
Code dirctly跳转到Sample4.jsp,但不允许用户单击Form Sample3.jsp中锚标签中的超链接。想要停止页面并允许用户点击并继续前进。
Sample4.jsp
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>page 4</h1>
<%String name =(String)session.getAttribute("name");
out.println(name);%>
</body>
Plz为此提供任何解决方案。
答案 0 :(得分:0)
<a href
.....标记中您将请求转发到sample2.jsp
和sample3.jsp
页面的问题,您再次转发请求,最终请求sample4.jsp
。
从这里了解更多信息RequestDispatcher。
这个问题的解决方案:
试试这个
Web.xml:
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>sample1.jsp</welcome-file>
</welcome-file-list>
</web-app>
<强> Sample1.jsp 强>
<body>
<form action="sample2.jsp" method="post">
<h1>Page1</h1>
<input type="text" name="name">
<input type="submit" value="Go">
</form>
</body>
<强> Sample2.jsp 强>
<body>
<h1>page2</h1>
<%
String name = request.getParameter("name");
session.setAttribute("name",name);
//out.println(name);
//response.sendRedirect("sample3.jsp");
%>
<a href="sample3.jsp"><%out.println(name);%></a>
</body>
<强> Sample3.jsp 强>
<body>
<h1>Page 3</h1>
<%
String lname = (String)session.getAttribute("name");
//out.println(lname+"\n");
//session.setAttribute("lname",lname);
%>
<a href="sample4.jsp"><%out.println(lname);%></a>
</body>
<强> Sample4.jsp 强>
<body>
<h1>page 4</h1>
<%String name =(String)session.getAttribute("name");
out.println(name);%>
</body>
答案 1 :(得分:0)
请勿在{{1}}标记中使用RequestDispatcher。即使在 Sample4.jsp 中,会话值仍将保持不变,因此即使是基本的超链接也可以使用会话中的值。
此外,如果您想查看何时使用转发()以及何时使用 sendRedirect()方法,请查看以下链接:http://javarevisited.blogspot.in/2011/09/sendredirect-forward-jsp-servlet.html