java 从一个jsp到另一个jsp获取价值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15318986/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 19:13:24  来源:igfitidea点击:

Getting value from one jsp to another jsp

javajsp

提问by VijayKumar

I developed simeple web application. In one jsp i get values from preivous servlet file using getattribute and set attribute.I got the values. But after now i want that values from current jsp to another jsp file. Using getattribute and setattribute i used but the values should display as null.

我开发了简单的网络应用程序。在一个jsp中,我使用getattribute和set attribute从以前的servlet文件中获取值。我得到了这些值。但是现在我想要从当前jsp到另一个jsp文件的值。使用我使用的 getattribute 和 setattribute,但值应显示为空。

firstjsp file:

第一个jsp文件:

 <%@page contentType="text/html" pageEncoding="UTF-8"%>
          <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
             "http://www.w3.org/TR/html4/loose.dtd">
           <%@ page import="javax.servlet.http.*" %>

           <html>
           <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
        </head>
        <body>
    <form action="payment.jsp" method="POST">
        <h1>Confirmation</h1>
        <% 
                Integer amount=(Integer)request.getAttribute("amt");   
               String service=(String )request.getAttribute("service");
               String month=(String )request.getAttribute("month");
                Integer day=(Integer)request.getAttribute("day");   
                  String time=(String)request.getAttribute("time");
                  String city=(String)request.getAttribute("city");
                  out.println("<center>");
                  out.println("<table><tr>");
                  out.println("<td><h2>Service:"+service+"</td></tr>");
                  out.println("<tr><td><h2>Month:"+month+"</td></tr>");
                  out.println("<tr><td><h2>Date:"+day+"</td></tr>");
                  out.println("<tr><td><h2>Time:"+time+"</td></tr>");
                  out.println("<tr><td><h2>City:"+city+"</td></tr>");
                   out.println("<tr><td><h2>Total Amount:Rs."+amount+"</td></tr>");                      
                  out.println("</center>");
                  request.setAttribute("amt",amount);







        %>


        <center>
            <input type="submit" value="Confirm"></input>
        </center>

    </form>
         </body>
       </html>

payment.jsp:

付款.jsp:

            <html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
</head>
<body>
    <h2>Select bank</h2>
    <%!Integer money;%>
    <%
    if(request.getAttribute("amt")!=null)
        {
    money=(Integer) request.getAttribute("amt");
    out.println(""+money);
    }
    %>
        </body>
         </html>

回答by verisimilitude

Set whatever values you want to access in payment.jspin HTML hidden control as below :-

payment.jsp在 HTML 隐藏控件中设置要访问的任何值,如下所示:-

<form action="payment.jsp" method="POST">
    <input type="hidden" name="amt" value="<%= amount%>" />
</form>

Request object in JSP spans only a single HTTP request. So when you forward the request to your first JSP file it is a single request. But when you submit the form and payment.jsploads, the request object is cleared since it is a new HTTP request to the server.

JSP 中的请求对象仅跨越单个 HTTP 请求。因此,当您将请求转发到您的第一个 JSP 文件时,它是一个单一的请求。但是当您提交表单并payment.jsp加载时,请求对象将被清除,因为它是对服务器的新 HTTP 请求。

回答by Visruth

You may use sessioninstead of requestimplicit object.

您可以使用session代替request隐式对象。

<html>
               <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
            </head>
            <body>
        <form action="payment.jsp" method="POST">
            <h1>Confirmation</h1>
            <% 
                    Integer amount=(Integer)request.getAttribute("amt");   
                   String service=(String )request.getAttribute("service");
                   String month=(String )request.getAttribute("month");
                    Integer day=(Integer)request.getAttribute("day");   
                      String time=(String)request.getAttribute("time");
                      String city=(String)request.getAttribute("city");
                      out.println("<center>");
                      out.println("<table><tr>");
                      out.println("<td><h2>Service:"+service+"</td></tr>");
                      out.println("<tr><td><h2>Month:"+month+"</td></tr>");
                      out.println("<tr><td><h2>Date:"+day+"</td></tr>");
                      out.println("<tr><td><h2>Time:"+time+"</td></tr>");
                      out.println("<tr><td><h2>City:"+city+"</td></tr>");
                       out.println("<tr><td><h2>Total Amount:Rs."+amount+"</td></tr>");                      
                      out.println("</center>");
                      session.setAttribute("amt",amount);//Changed to session







            %>


            <center>
                <input type="submit" value="Confirm"></input>
            </center>

        </form>
             </body>
           </html>

payment.jsp:

付款.jsp:

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
</head>
<body>
    <h2>Select bank</h2>
    <%!Integer money;%>
    <%
    if(session.getAttribute("amt")!=null)//changed to session
        {
    money=(Integer) session.getAttribute("amt");//changed to session
    out.println(""+money);
     session.removeAttribute("amt");
    }
    %>
        </body>
         </html>

回答by Birhan Nega

in order to access the value you set in the first jsp, you have better to put the value using session in this way.request.getSeession.setAttribute("amount",amount);then in the second jsp access it like this

为了访问您在第一个jsp中设置的值,您最好以这种方式使用session来放置该值。request.getSeession.setAttribute("amount",amount);然后在第二个jsp中像这样访问它

<form>
 <input type="hidden" value="<%=session.getAttribute("amount")%>"/>
 <form>

i hope it solves your problem

我希望它能解决你的问题