java—如何从一个jsp页面转换到另一个?

zazmityj  于 2021-06-25  发布在  Mysql
关注(0)|答案(1)|浏览(298)

我正在试着为在线巴士预订系统编码。但当我在索引页上单击submit之后,就不会转换到第二页了。我是jdbc和jsp的新手。
程序应该做什么:在index.jsp中单击“start booking”之后,它应该显示registration.jsp的内容。在那里输入的详细信息应该存储在数据库中,类似地,对于passenger.jsp,内容需要存储在同一个数据库中。但是,当我单击“start booking”时,registration.jsp不会显示。
下面是我的代码:index.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>
</title>
<body bgcolor="gray">
<center>
<h1>Welcome</h1><br/>
<h2>Road Transport System</h2>
<h3> <input id="sbutton" type="submit" value="start booking" onclick="registration.jsp"/><br/>
</h3>
</center>
</body>
</head>
</html>

注册.jsp

<%@page import="java.sql.*" %>
<%@page import="java.sql.DriverManager" %>
<%@page import="java.sql.Connection" %>

<% Class.forName("com.myssql.jdbc.Driver");%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
    <title>
</title>
<body bgcolor="gray">
<center>
<p> Enter details</p>
</scenter>
<%!
        public class Reservation{
  final String DB_URL = "jdbc:mysql://localhost/busrs?useSSL=false";
         final String USER = "root";
         final String PASS = "pwd";
         Connection con;

PreparedStatement ps;
     Statement st;
     public ResultSet rs;

public Reservation()
{
   try{
con = DriverManager.getConnection(DB_URL, USER, PASS);
}catch(SQLException e)
{
                System.out.println("Oops! Connection failed:" + e);

}
try {
                ps = con.prepareStatement ("insert into reservation values(?,?,?,?,?,?,?,?)");
                   }
catch(SQLException e)
{
e.printStackTrace();
}
}
public void setValues(String s1,String s2,String s3,String s4,
String s5,String s6,String s7, String s8)
{
try{
             ps.setString (1, s1);
             ps.setString (2, s2);
                    ps.setString (3, s3);
                    ps.setString (4,s4);
                    ps.setString (5, s5);
                    ps.setString (6, s6);
                    ps.setString (7, s7);
                    ps.setString (8, s8);
                    ps.executeUpdate ();
}
catch(SQLException e)
{
e.printStackTrace();
} 
}
}
 %>
 <%
 String s1=new String();
  String s2=new String();
 String s3=new String();
 String s4=new String();
 String s5=new String();
 String s6=new String();
 String s7=new String();
 String s8=new String();
 if(request.getParameter("next")!=null)
 {
 if(request.getParameter("pnrno")!=null)
 s1=request.getParameter("pnrno");
 if(request.getParameter("busno")!=null)
 s2=request.getParameter("busno");
 if(request.getParameter("busname")!=null)
 s3=request.getParameter("busname");
 if(request.getParameter("class")!=null)
 s4=request.getParameter("class");
 if(request.getParameter("dateof")!=null)
 s5 =request.getParameter("dateof");
 if(request.getParameter("from")!=null)
 s6 =request.getParameter("from");
 if(request.getParameter("to")!=null)
 s7 =request.getParameter("to");
 if(request.getParameter("boarding")!=null)
 s8=request.getParameter("boarding");
 Reservation r=new Reservation();
  r.setValues(s1, s2, s3, s4, s5, s6, s7, s8);
 }

 %>

    <P>
<center>

    <LABEL for="pnrno">PNR No</LABEL>
              <INPUT type="text" id="pnrno"><BR><BR/>
    <LABEL for="busno">Bus No </LABEL>
              <INPUT type="text" id="busno"><BR><BR/>
<LABEL for="busname">Bus Name </LABEL>
              <INPUT type="text" id="busname"><BR><BR/>
<LABEL for="class">Class(AC/Non-AC) </LABEL>
<INPUT type="text" id="class">
    <BR><BR/>
<LABEL for="dateof"> Date Of Journey</LABEL>
              <INPUT type="text" id="dateof"><BR><BR/>
<LABEL for="from">From </LABEL>
              <INPUT type="text" id="from"> 
              <LABEL for="to">To</LABEL><INPUT type="to" id="from"> 
              <BR><BR/>
<LABEL for="boarding">Boarding</LABEL>
              <INPUT type="text" id="boarding"><BR><BR/>

 <form name="f1" action="passenger.jsp" method="POST">
 <input type="button" value="next"/>
</h2> <form name="f2" action="index.jsp" method="POST">
</form> <h2>
        <input type="button" value="Main"/></h2>

<center>
    </P>
 </FORM>
</body>
</head>
</html>

最后是passenger.jsp

<%@page import="java.sql.*" %>
<%@page import="java.sql.DriverManager" %>
<%@page import="java.sql.Connection" %>
<% Class.forName("com.myssql.jdbc.Driver");%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<html>
<body bgcolor="gray">
    <%!
        public class Passenger{
     final String DB_URL = "jdbc:mysql://localhost/busrs?useSSL=false";
         final String USER = "root";
         final String PASS = "pwd";
         Connection con;
     PreparedStatement ps;
     Statement st;
     public ResultSet rs;
public Passenger()
{
 try{
con = DriverManager.getConnection(DB_URL, USER, PASS);
}catch(SQLException e)
{
                System.out.println("Oops! Connection failed:" + e);
}
try {
                ps=con.prepareStatement("insert into passenger values(?,?,?,?,?,?,?)");
                   }
catch(SQLException e)
{
e.printStackTrace();
}
}
public void setValues(String s1,String s2,String s3,String s4,
String s5,String s6,String s7)
{
try{
             ps.setString (1, s1);
             ps.setString (2, s2);
                    ps.setString (3, s3);
                    ps.setString (4,s4);
                    ps.setString (5, s5);
                    ps.setString (6, s6);
                    ps.setString (7, s7);  
                    ps.executeUpdate ();
}
catch(SQLException e)
{
e.printStackTrace();
} 
}
}
 %>
 <%
 String s1=new String();
  String s2=new String();
 String s3=new String();
 String s4=new String();
 String s5=new String();
 String s6=new String();
 String s7=new String();

 if(request.getParameter("save")!=null)
 {
 if(request.getParameter("pid")!=null)
 s1=request.getParameter("pid");
 if(request.getParameter("name")!=null)
 s2=request.getParameter("name");
 if(request.getParameter("age")!=null)
 s3=request.getParameter("age");
 if(request.getParameter("address")!=null)
 s5=request.getParameter("address");
 if(request.getParameter("category")!=null)
 s6 =request.getParameter("category");
 if(request.getParameter("gender")!=null)
 s4 =request.getParameter("gender");

 Passenger r=new Passenger();
  r.setValues(s1, s2, s3, s4, s5, s6, s7);
 }

 %>
    <P>
<center>
<LABEL for="pid">PID</LABEL>
              <INPUT type="text" id="pid"><BR><BR/>
<LABEL for="name">Name of Passenger</LABEL>
              <INPUT type="text" id="name"><BR><BR/>
<LABEL for="age">Age</LABEL>
              <INPUT type="text" id="age"><BR><BR/>
 <LABEL for="gender">Gender</LABEL><br>
           <INPUT type="text" id="gender"><BR><BR/>
<LABEL for="address">Address</LABEL>
              <INPUT type="textArea" id="address"><BR><BR/>
<LABEL for="category">Category(General/Senior Citizen/Ex-serviceman)</LABEL><br>
           <INPUT type="text" id="category"><BR><BR/>
     <form name="myform" action="registration.jsp" method="POST">
            </form>
 <input type="button" value="save"/> 
 <form name="form2" action="registration.jsp" method="POST">
 </form>
 <input type="button" value="Back"/>
<center>
    </P>
 </FORM>
</body>
</html>

我使用netbeans对jsp部分进行编码,使用mysql对数据库进行编码。

svdrlsy4

svdrlsy41#

首先,你需要让行动在你的生活中起作用 index.jsp ,添加 form 单击“提交”按钮并删除 onclick 属性

<form action="registration.jsp" method="get"> 
    <input id="sbutton" type="submit" value="start booking"/><br/>
</form>

另外,在 registration.jsp 页面,更改 mysql 驱动程序名称来自 Class.forName("com.myssql.jdbc.Driver");Class.forName("com.mysql.jdbc.Driver");

相关问题