首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在JSP中通过会话传递值

问在JSP中通过会话传递值
EN

Stack Overflow用户
提问于 2017-04-02 12:52:01
回答 2查看 167关注 0票数 0

我有4个jsp表单名-- Sample1.jsp、Sample2.jsp、Sample3.jsp、Sample4.jsp--我想通过会话将值从一个页面传递到另一个页面,我使用了以下方法

Sample1.jsp

代码语言:javascript
复制
<%@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

代码语言:javascript
复制
<%@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

代码语言:javascript
复制
<%@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,而不允许用户单击表单Sample3.jsp中锚标记中的超链接。想要停止页面并允许用户单击并向前移动。

Sample4.jsp

代码语言:javascript
复制
<%@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>
EN

回答 2

Stack Overflow用户

发布于 2017-04-02 15:52:53

你的<a href中的问题.标记,其中您将请求转发到sample2.jsp,在sample3.jsp页面中,您再次转发请求--因此,最终将到达的请求请求到sample4.jsp。在这里阅读更多关于RequestDispatcher的信息。

这个问题的解决办法:

尝尝这个

Web.xml :

代码语言:javascript
复制
<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

代码语言:javascript
复制
<body>
    <form action="sample2.jsp" method="post">
        <h1>Page1</h1>
        <input type="text" name="name">
        <input type="submit" value="Go">
    </form>
</body>

Sample2.jsp

代码语言:javascript
复制
<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

代码语言:javascript
复制
<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

代码语言:javascript
复制
<body>
    <h1>page 4</h1>
    <%String name =(String)session.getAttribute("name");
    out.println(name);%>

</body>
票数 0
EN

Stack Overflow用户

发布于 2017-04-03 05:17:20

不要在您的RequestDispatcher标记中使用<a>。即使在Sample4.jsp中,会话值也将保持持久化,因此即使是一个基本的超链接也可以使用会话中的值。

此外,如果您希望签出何时使用sendRedirect() ()和何时使用方法,请查看以下链接:http://javarevisited.blogspot.in/2011/09/sendredirect-forward-jsp-servlet.html

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43168557

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档