VijayKumar
VijayKumar

Reputation: 11

Getting value from one jsp to another jsp

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.

firstjsp file:

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

            <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>

Upvotes: 1

Views: 15180

Answers (3)

Birhan Nega
Birhan Nega

Reputation: 681

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

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

i hope it solves your problem

Upvotes: 0

verisimilitude
verisimilitude

Reputation: 5108

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

<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.jsp loads, the request object is cleared since it is a new HTTP request to the server.

Upvotes: 1

Visruth
Visruth

Reputation: 3450

You may use session instead of request implicit object.

<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:

<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>

Upvotes: 1

Related Questions