【发布时间】:2017-04-02 12:52:01
【问题描述】:
我有 4 个 jsp 表单名称 Sample1.jsp,Sample2.jsp,Sample3.jsp,Sample4.jsp 我想通过 Session 将价值从一个页面转移到另一个页面 我用了这个方法
Sample1.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<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>
</html>
Sample2.jsp 从 Sample1.jsp 中获取值名称并重定向到 sample3.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<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>
</html>
Sample3.jsp 从 Sample2.jsp 中获取值名称,在 Sample3.jsp 中不显示锚标记超链接,直接跳转到 Sample4.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<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>
</html>
代码直接跳转到Sample4.jsp,不允许用户点击Form Sample3.jsp中锚标签中的超链接。想要停止页面并允许用户单击并继续前进。
Sample4.jsp
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<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>
</html>
【问题讨论】:
-
将此添加到 sample3.jsp request.getRequestDispatcher("sample4.jsp").forward(request, response);
-
我应用了 request.getRequestDispatcher 但它被跳转到 sample4.jsp 而不显示或单击 Sample3.jsp 中锚标记中 lname 的值。
-
我应用了这个,但它被跳转到 sample4.jsp,而没有显示或单击 Sample3.jsp 中锚标记中的 lname 的值。我还希望 sample3.jsp 显示输出,当我单击 sample3.jsp 中锚标记中的超链接时,它应该转到 sample4.jsp
-
我解决了你的问题。
-
是的,谢谢它的工作,但是,从第一页它直接跳到最后一页(第四页),而不会在第三页停止。有什么办法可以处理。
标签: javascript java jquery jsp