【问题标题】:Transferring Value through session in JSP通过 JSP 中的会话传递值
【发布时间】: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


【解决方案1】:

&lt;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>

【讨论】:

  • 它不起作用,因为第 4 页上的变量 name 正在获取空值。直到第 3 页传输值,但不能在第 4 页上传递它。我认为 'request.getRequestDispatcher("sample3.jsp").forward(request, response);'是正确的选择。但问题是它并没有停在第 3 页上,因为有一个锚标记为第 4 页提供 href。它应该停在那里,以便用户单击该 href '" value=""> ' 并移至第 4 页。
  • 您是否在 sample3.jsp 上添加了 ?它在我的系统中运行良好。
  • 同样的问题值 name 没有从第 2 页转移到第 3 页。我不这么认为 会将值从一个 jsp 页面转移到另一个页面.....!!!
  • 记住你正在使用会话。
【解决方案2】:

不要在 &lt;a&gt; 标记中使用 RequestDispatcher。即使在 Sample4.jsp 中,会话值也将保持不变,因此即使是指向它的基本超链接也可以使用 Session 中存在的值。

另外,如果您想了解何时使用 forward() 以及何时使用 sendRedirect() 方法,请查看此链接:http://javarevisited.blogspot.in/2011/09/sendredirect-forward-jsp-servlet.html

【讨论】:

  • 感谢您的回答,但通过在第 3 页获取普通超链接,然后移至第 4 页,然后在 name 变量中显示 NULL 值
  • @Salik47 是的,当您使用 JSP 页面时,将隐式创建会话对象,因此为了防止在调用特定页面时创建新会话对象,您可以在您的页面上使用&lt;%@ page session="false" %&gt; 声明
  • 所以在第 4 页声明它显示错误: String name =(String)session.getAttribute("name"); ^ 符号:可变会话位置:类 sample4_jsp 1 错误
  • @Salik47 不要使用&lt;%@ page session="false" %&gt; 简单的超链接可以正常工作,请评论我您所写的产生 NULL 值的内容
  • 我在第 3 页中使用了 和 在第 4 页通过这个我在第 4 页的 name 变量中得到 null
猜你喜欢
  • 1970-01-01
  • 2013-08-11
  • 1970-01-01
  • 2023-04-01
  • 2011-11-13
  • 2011-09-25
  • 2010-12-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多