如何在一个jsp页面中使用数据到另​​一个jsp页面

时间:2011-05-09 19:20:42

标签: jsp

我有2个jsp文件,其中第一个文件有一系列按钮。我的问题是使用我在第二个JSP文件中的第一个文件中单击的按钮值。

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>

1 个答案:

答案 0 :(得分:0)

为提交按钮指定固定名称。所以,替换

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

通过

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

其值将作为具有给定名称

的请求参数提供
String conValue = request.getParameter("conValue");
// ...

对于具体问题

无关,您的JSP诚实地说是关于“最佳实践”的完整灾难。模型,视图和控制器逻辑混合在一个难以理解且难以维护的JSP文件中。已使用 Scriptlets 代替taglibs / EL来控制页面流并访问数据。 JDBC代码正在泄漏资源。数据库连接保留在会话范围内,而不是由池管理。 HTML使用90年代风格的大写标签/属性,并使用自1998年以来弃用的<center>标签。 Etcetera,etcetera。请确保您正在阅读有关该主题的正确和最新的书籍/教程:) Start here