简体   繁体   English

从一个jsp获得价值到另一个jsp

[英]Getting value from one jsp to another jsp

I developed simeple web application. 我开发了Simeple Web应用程序。 In one jsp i get values from preivous servlet file using getattribute and set attribute.I got the values. 在一个jsp中,我使用getattribute和set attribute从先前的servlet文件中获取了值。 But after now i want that values from current jsp to another jsp file. 但是现在以后,我希望该值从当前的jsp到另一个jsp文件。 Using getattribute and setattribute i used but the values should display as null. 我使用了getattribute和setattribute,但是值应显示为null。

firstjsp file: firstjsp文件:

 <%@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: payment.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>

Set whatever values you want to access in payment.jsp in HTML hidden control as below :- 设置您想要在HTML隐藏控件中的payment.jsp中访问的任何值,如下所示:-

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

Request object in JSP spans only a single HTTP request. JSP中的请求对象仅跨越一个HTTP请求。 So when you forward the request to your first JSP file it is a single request. 因此,当您将请求转发到第一个JSP文件时,它是一个请求。 But when you submit the form and payment.jsp loads, the request object is cleared since it is a new HTTP request to the server. 但是,当您提交表单并加载payment.jsp时,由于该对象是对服务器的新HTTP请求,因此该请求对象被清除。

You may use session instead of request implicit 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: payment.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>

in order to access the value you set in the first jsp, you have better to put the value using session in this way. 为了访问您在第一个jsp中设置的值,最好以这种方式使用session来放置值。 request.getSeession.setAttribute("amount",amount); then in the second jsp access it like this 然后在第二个jsp中像这样访问它

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

i hope it solves your problem 我希望它能解决您的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM