如何在一个jsp页面中使用数据到另​​一个jsp页面 [英] How to use data in one jsp page to another jsp page

查看:98
本文介绍了如何在一个jsp页面中使用数据到另​​一个jsp页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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>

推荐答案

为提交"按钮指定一个固定的名称.因此,替换

Give the submit button a fixed name. So, replace

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

作者

<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");
// ...


与具体问题无关,老实说,您的JSP就最佳实践"而言完全是一场灾难.模型,视图和控制器逻辑全部混合在一个大的不可读且难以维护的JSP文件中.使用小脚本代替taglibs/EL来控制页面流和访问数据. JDBC代码正在泄漏资源.数据库连接保留在会话范围内,而不是由池管理. HTML使用90年代样式的大写标签/属性,并使用自1998年以来已弃用的<center>标签. Etcetera等.请确保您正在阅读有关该主题的正确且最新的书籍/教程:)


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.

这篇关于如何在一个jsp页面中使用数据到另​​一个jsp页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆