0

I have 2 jsp files inwhich first file has a series of buttons. My problem is to use the button value that I clicked in first file in the second JSP file.

ApproveBusReq.jsp:

<%@ page language="java" %>
<%@ page session="true" %>
<%@ page import="java.sql.*,java.io.*,java.util.Random"%>
<% String bus = request.getParameter("busno");%>
<% String user = request.getParameter("user");%>
<HEAD>

    <LINK href="styles.css" type="text/css" rel="stylesheet">


<script LANGUAGE="Javascript" SRC="Images/validate.js"></script>
<script LANGUAGE="Javascript">
<!--
function fnShowDetails()
            {
                var frm = document.forms(0);
                var RouteCode = frm.RouteCode.value;

                if (RouteCode=='')
                {
                    alert("Please select RouteCode to Proceed");
                }else{
                    var URL = "ShowRoutes01.jsp?RouteCode="+RouteCode;
                    fnEmpPopUp(URL,300,400);
                }
            }
    function validate()
        {
            var frm = document.acc;
            var RouteCode = frm.RouteCode.value;
                        if (RouteCode=='')
                {
                    alert("Please select RouteCode to Proceed");
                }


        }
//-->

</script>
</HEAD>
<jsp:include page="MultiLevelmenu.htm"/><br><br><br>

<BODY class=Grad>
<%
    Integer IAuth =(Integer)session.getAttribute("auth");
    int auth= IAuth.intValue();
    System.out.println("===Authentication=="+auth);
if(auth==2){
    %><H3 align=center><IMG SRC="Images/error.gif" WIDTH="17" HEIGHT="13" BORDER=0 ALT="">You are not authorized to access this page</H3><%
}
else{
%>  
<h2 align=center>Assign Seat Number</h2>
<%

    Connection con;
    ResultSet rs=null;
    Statement stmt=null;
    int[] ConValues = new int[100];
        String[] ConValues2=new String[100];
    int ConCount=0,i=0;
    String Query1="";

    try{

            con=(Connection)session.getAttribute("connection");
            stmt =  con.createStatement();
            String Query = "Select seatno from approve where studentid = "+user;
            System.out.println(Query);
            rs = stmt.executeQuery(Query);
            int count=0;
            while(rs.next())
            {
                    System.out.println("Seat Already");
                    int x = rs.getInt(1);
                    count++;
            }
            if(count!=0)
            {
            %>
            <center><h3 align=center>Seat Already Assigned</h3>
            <br><A href="Home.html"> Back To Home </A></center>
            <%
            }
            else
            {
            %>
    <FORM Name='acc' METHOD=POST ACTION="ApproveBusReq1.jsp?bus=<%=bus%>&user=<%=user%>" onClick="return validate()">
    <center> <%

                         Query1 = "Select seatno,studentid from approve where busno="+bus+" order by seatno";
            System.out.println(Query1);
            rs = stmt.executeQuery(Query1);
                        int seat=0;
                        String id;
                        i=0;
                        while(rs.next()){
                            seat=rs.getInt(1);
                            ConValues[i]=seat;
                            id=rs.getString(2);
                            ConValues2[i++]=id;
                          }
        if(i==40||i==60){
        %> <table bgcolor="gray" align="center" rules="none" hspace="50" border="1"><%
                int k=0;
                        for(int j=0;j<i;j++){
                            if(k==0)
                                %><tr><%
                             if(k==4){
                                k=0;
                                %></tr><%
                             }
                             if(k==2){
                                 for(int t=0;t<50;t++){
                             %><th> </th><%}}
                             if(ConValues2[j]!=null){
                             %><th><input type="submit" name="<%=ConValues[j]%>" value="<%=ConValues[j]%>" style="color:red"/></th><%}
                             else{
                              %><th><input type="submit" value="<%=ConValues[j]%>" name="<%=ConValues[j]%>" style="color:green"/></th><%}
                             k++;
                        }
                %></table><% }
         else if(i==80){
             %> <table bgcolor="gray" align="center" rules="none" border="0"><%
                int k=0;
                        for(int j=0;j<i;j++){
                            if(k==0)
                                %><tr><%
                             if(k==5){
                                k=0;
                                %></tr><%
                             }
                             %><th><input type="button" value="<%=ConValues[j]%>" style="color:red"/></th><%
                             k++;
                        }
                %></table><% }

             }}catch(Exception e){
                            %><%=e%><%
                     }
            %>
</center>
</FORM>
</BODY>
<%
}
%>

approveBusReq1.jsp:

<%@ page language="java" %>
<%@ page session="true" %>
<%@ page import="java.sql.*,java.io.*,java.util.Random"%>
<% String bus = request.getParameter("bus");%>
<% String user = request.getParameter("user");%>
<% String seatno = request.getParameter("seatno");%>
<%
System.out.println("bus "+bus);
System.out.println("user "+user);
System.out.println("seatno "+seatno);
%>


<HEAD>

    <LINK href="styles.css" type="text/css" rel="stylesheet">


    <script LANGUAGE="Javascript" SRC="Images/validate.js"></script>

</HEAD>
<jsp:include page="MultiLevelmenu.htm"/><br><br>

<BODY class=Grad>
<h2 align=center>Cancel Route</h2>

<%

    Connection con;
    ResultSet rs=null;
    Statement stmt=null,stmt1=null;
    try{

            con=(Connection)session.getAttribute("connection");
            stmt =  con.createStatement();
            String rcode="null";
                        %><%=seatno%><%
            String Query2="update approve SET approve.studentid='" + user + "' where busno="+bus+" AND seatno=" + seatno;
            int result1 = stmt.executeUpdate(Query2);
            System.out.println(Query2);
            String Query3="update busrequest SET busrequest.status=1 where studentid='"+user+"'";
            int result2 = stmt.executeUpdate(Query3);
            System.out.println(Query3);

            if(result1!=0)
            {
            System.out.println(result1);
                        %>
            <center><h3 align=center> Bus Approved Successfully </h3>

            <A href="Home.html"> Back To Home </A> </center>
            <%
            }
            else
            {
            %>
            <center><h3 align=center>Already Approved</h3>
            <A href="Home.html"> Back To Home </A>
            </center>
                    <%
            }
        }
        catch(Exception e)
        {%>
            <h3 align=center>Error: Connection Error</h3>
            <center>
            <A href="DeleteRoute0.jsp"> Back </A>
            </center><br>
        <%=e%><%}
%>

</BODY>
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
RAVITEJA SATYAVADA
  • 2,503
  • 23
  • 56
  • 88

1 Answers1

0

Give the submit button a fixed name. So, replace

<input type="submit" name="<%=ConValues[j]%>" value="<%=ConValues[j]%>">

by

<input type="submit" name="conValue" value="<%=ConValues[j]%>">

Its value will be available as a request parameter with the given name

String conValue = request.getParameter("conValue");
// ...

Unrelated to the concrete problem, your JSPs are honestly said complete disasters with regard to "best practices". The model, view and controller logic is all mingled in one big unreadable and hard-to-maintain JSP file. Scriptlets are been used instead of taglibs/EL to control the page flow and access the data. The JDBC code is leaking resources. The DB connection is kept in the session scope instead of managed by a pool. The HTML is using 90's style uppercased tags/attributes and using the since 1998 deprecated <center> tag. Etcetera, etcetera. Please ensure that you're reading the right and up-to-date books/tutorials on the subject :) Start here.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555