Javascript 在JSP中通过会话传递值
我有4个jsp表单名Sample1.jsp、Sample2.jsp、Sample3.jsp、Sample4.jsp 我想通过会话将值从一个页面转移到另一个页面 我用了这个方法 Sample1.jspJavascript 在JSP中通过会话传递值,javascript,java,jquery,jsp,Javascript,Java,Jquery,Jsp,我有4个jsp表单名Sample1.jsp、Sample2.jsp、Sample3.jsp、Sample4.jsp 我想通过会话将值从一个页面转移到另一个页面 我用了这个方法 Sample1.jsp <%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta http-equiv="Conte
<%@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>
<%@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>
<body>
<form action="sample2.jsp" method="post">
<h1>Page1</h1>
<input type="text" name="name">
<input type="submit" value="Go">
</form>
</body>
<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>
<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>
<body>
<h1>page 4</h1>
<%String name =(String)session.getAttribute("name");
out.println(name);%>
</body>
JSP页面
第1页
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>
JSP页面
第2页
Sample3.jsp
从Sample2.jsp获取了值名,并且没有在Sample3.jsp中显示锚定标记超链接,直接跳转到Sample4.jsp
JSP页面
第3页
代码直接跳转到Sample4.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>
<form action="sample2.jsp" method="post">
<h1>Page1</h1>
<input type="text" name="name">
<input type="submit" value="Go">
</form>
</body>
</html>
<%@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>
<body>
<form action="sample2.jsp" method="post">
<h1>Page1</h1>
<input type="text" name="name">
<input type="submit" value="Go">
</form>
</body>
<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>
<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>
<body>
<h1>page 4</h1>
<%String name =(String)session.getAttribute("name");
out.println(name);%>
</body>
JSP页面
第4页
您的中的问题
这个问题的解决办法是:
试试这个
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>
30
sample1.jsp
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>
<%@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>
<body>
<form action="sample2.jsp" method="post">
<h1>Page1</h1>
<input type="text" name="name">
<input type="submit" value="Go">
</form>
</body>
<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>
<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>
<body>
<h1>page 4</h1>
<%String name =(String)session.getAttribute("name");
out.println(name);%>
</body>
第1页
Sample2.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>
<%@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>
<body>
<form action="sample2.jsp" method="post">
<h1>Page1</h1>
<input type="text" name="name">
<input type="submit" value="Go">
</form>
</body>
<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>
<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>
<body>
<h1>page 4</h1>
<%String name =(String)session.getAttribute("name");
out.println(name);%>
</body>
第2页
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>
<form action="sample2.jsp" method="post">
<h1>Page1</h1>
<input type="text" name="name">
<input type="submit" value="Go">
</form>
</body>
</html>
<%@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>
<body>
<form action="sample2.jsp" method="post">
<h1>Page1</h1>
<input type="text" name="name">
<input type="submit" value="Go">
</form>
</body>
<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>
<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>
<body>
<h1>page 4</h1>
<%String name =(String)session.getAttribute("name");
out.println(name);%>
</body>
第3页
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>
<form action="sample2.jsp" method="post">
<h1>Page1</h1>
<input type="text" name="name">
<input type="submit" value="Go">
</form>
</body>
</html>
<%@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>
<body>
<form action="sample2.jsp" method="post">
<h1>Page1</h1>
<input type="text" name="name">
<input type="submit" value="Go">
</form>
</body>
<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>
<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>
<body>
<h1>page 4</h1>
<%String name =(String)session.getAttribute("name");
out.println(name);%>
</body>
第4页
不要在您的中使用RequestDispatcher将其添加到sample3.jsp request.getRequestDispatcher(“sample4.jsp”).forward(请求,响应);我应用了request.getRequestDispatcher,但它跳到sample4.jsp,而没有显示或单击Sample3.jsp中锚定标记中的lname值。我应用了此功能,但它跳到sample4.jsp,而没有显示或单击Sample3.jsp中锚定标记中的lname值。我还希望sample3.jsp显示输出,当我在sample3.jsp中单击锚定标记中的超链接时,它应该转到sample4.jspI。是的,谢谢,它成功了,但从第一页直接跳到最后一页(第四页),而不在第三页停止。有没有办法处理它。它不起作用,因为第4页上的变量名得到空值。直到第3页它才传递值,但无法在第4页传递。我认为'request.getRequestDispatcher(“sample3.jsp”).forward(请求,响应);'这是正确的选择。但问题是它并没有停在第3页上,因为有一个锚定标签为第4页提供href。它应该停在那里,让用户单击该href''并移动到第4页。您是否添加了sample3.jsp?它在我的系统中运行良好。仍然是相同的问题名称值没有从第2页转移到第3页。我认为这样做不会将值从一个jsp页面转移到另一个页面。。。。。!!!请记住,您正在使用会话。谢谢您的回答,但通过在第3页上使用普通超链接,然后移动到第4页,然后在名称中显示空值variable@Salik47是的,当您使用JSP页面时,会话对象将被隐式创建,因此,为了防止在调用特定页面时创建新的会话对象,您可以对页面使用
声明,以便在第4页中声明其显示错误:String name=(String)session.getAttribute(“name”);^符号:变量会话位置:类sample4\u jsp 1error@Salik47不要使用
简单的超链接可以很好地工作,请评论我您所写的产生空值的内容我在第3页和第4页中使用了这个,我在第4页的name变量中得到了空值