I am working on an online bus ticket booking project in jsp/servlet... using tomcat6 and mysql in netbeans... now i know i have done some really foolish and ancient coding .. but please help me in getting this done ... i am actually stuck in this session thing... look below in the view.jsp code, there i have provided link ( as input for the selected bus in which a user would prefer to travel) ... now the next sevlet code which is InfoServlet.java , this code takes the responsibility of inserting the booking of a ticket for the bus... The tables that i have made are...
bus (bus_id,bus_name,departure,arrival,departure_time,arrival_time,available_tickets,price)
customer ( cus_id,cus_name,address,email,ccno)
booking ( book_no,cus_id,book_date)
booking_list (book_no, bus_id, tickets)
except the booking_list table , data in other tables are geting inserted... because the booking_list table needs bus_id ( which is only available from the bus table , for which we will have to know the bus selected by the user i.e., previous data or session data is necessary ).. I am somewhere wrong in implementing this part.... so please help me in correcting the code here... As i said , i know i have done serious crimes by implementing foolish coding standards... apart from that please help me corecting this anyway... thanks in advance... the codes according to flow of the web app are given below.....
index.jsp-->>
<html>
<head>
<title> Nothing </title>
</head>
<body>
<h1>
Find your Bus -->>
</h1>
<form action="busServlet" method="post">
departure:
<select name="departure" size="1">
<option value="Guwahati"> Guwahati </option>
<option value="Dhemaji"> Dhemaji</option>
</select>
<br>
<br>
arrival:
<select name="arrival" size="1">
<option value="Guwahati"> Guwahati </option>
<option value="Dhemaji"> Dhemaji</option>
</select>
<br>
<br>
<br>
<input type="submit" name="submit" value=" Submit ">
<br>
<br>
<br>
</form>
</body>
</html>
=======================================
BusServlet.java-->>
import java.io.*;
import java.util.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class busServlet extends HttpServlet{
private ServletConfig config;
//Setting JSP page
String page="view.jsp";
public void init(ServletConfig config)
throws ServletException{
this.config=config;
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException,IOException
{
PrintWriter out = response.getWriter();
//Establish connection to MySQL database
HttpSession session = request.getSession();
String connectionURL = "jdbc:mysql://localhost:3306/busdb";
Connection connection = null;
ResultSet rs;
response.setContentType("text/html");
List dataList = new ArrayList();
String departure = request.getParameter("departure");
String arrival = request.getParameter("arrival");
session.setAttribute("dep","departure");
session.setAttribute("arr","arrival");
try {
// Load the database driver
Class.forName("com.mysql.jdbc.Driver");
// Get a Connection to the database
connection = DriverManager.getConnection(connectionURL, "root", "pass");
//Select the data from the database
String sql = "select bus_name from bus where departure='" + departure + "' and arrival= '" + arrival + "'";
Statement s = connection.createStatement();
s.executeQuery (sql);
rs = s.getResultSet();
while (rs.next ()){
//Add records into data list
dataList.add(rs.getString("bus_name"));
}
rs.close ();
s.close ();
}catch(Exception e){
System.out.println("Exception is ;"+e);
}
request.setAttribute("data",dataList);
//Disptching request
RequestDispatcher dispatcher = request.getRequestDispatcher(page);
if (dispatcher != null){
dispatcher.forward(request, response);
}
}
}
=======================================
view.jsp-->>
<%@page language="java" import="java.util.*" %>
<html>
<head>
<title>Data Page</title>
</head>
<body>
<form name="input" action="InfoServlet" method="post">
<table border="1" width="200" >
<tr>
<td width="200"><b>Bus_Name</b></td>
</tr>
<%Iterator itr;%>
<% List data= (List)request.getAttribute("data");
for (itr=data.iterator(); itr.hasNext(); )
{
%>
<% String item = (String)itr.next();%>
<tr>
<td width="200"> <a href="form.jsp?item={$item}"> <%=item%> </a></td>
</tr>
<%}%>
</table>
</form>
</body>
</html>
=======================================
form.jsp-->>
<html>
<head>
<title> Nothing </title>
</head>
<body>
<h1>
Fill the form -->>
</h1>
<form name="input" action="InfoServlet" method="post">
Id: <input type="text" name="id" />
<br><br>
Name : <input type="text" name="name" />
<br><br>
Address : <input type="text" name="address" />
<br><br>
E_mail : <input type="text" name="email" />
<br><br>
Credit Card Number : <input type="text" name="ccno" />
<br>
<br>
Tickets : <input type="text" name="tickets" />
<br>
<br>
Book Number : <input type="text" name="booknum" />
<br>
<br>
Date : <input type="text" name="date" />
<input type="submit" value="Submit" />
</form>
</body>
</html>
=============================================
InfoServlet.java -->>
import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class InfoServlet extends HttpServlet{
private ServletConfig config;
//Setting JSP page
String page="success.jsp";
@Override
public void init(ServletConfig config)
throws ServletException{
this.config=config;
}
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException,IOException
{
PrintWriter out = response.getWriter();
//Establish connection to MySQL database
HttpSession session = request.getSession();
String connectionURL = "jdbc:mysql://localhost:3306/busdb";
Connection connection = null;
ResultSet rs;
response.setContentType("text/html");
try {
String itm = request.getParameter("item");
session.setAttribute("buses", itm);
String id = request.getParameter("id");
String name = request.getParameter("name");
String address = request.getParameter("address");
String email = request.getParameter("email");
String ccno = request.getParameter("ccno");
String tickets = request.getParameter("tickets");
//int t = Integer.parseInt(tickets);
String bkno = request.getParameter("booknum");
String date = request.getParameter("date");
//session.setAttribute("ids",id);
session.setAttribute("nam",name);
session.setAttribute("add",address);
session.setAttribute("mail",email);
session.setAttribute("cc",ccno);
String d = (String)session.getAttribute("dep");
String a = (String)session.getAttribute("arr");
String b = (String)session.getAttribute("buses");
// Load the database driver
Class.forName("com.mysql.jdbc.Driver");
// Get a Connection to the database
connection = DriverManager.getConnection(connectionURL, "root", "pass");
PreparedStatement pst = connection.prepareStatement("insert into customer values(?,?,?,?,?)");
pst.setString(1,id);
pst.setString(2,name);
pst.setString(3,address);
pst.setString(4,email);
pst.setString(5,ccno);
int i = pst.executeUpdate();
PreparedStatement pst1 = connection.prepareStatement("insert into booking values(?,?,?)");
pst1.setString(1,bkno);
pst1.setString(2,id);
pst1.setString(3,date);
int j = pst1.executeUpdate();
String sql = "select bus_id from bus where bus_name ='" + b + "' and departure='" + d + "' and arrival= '" + a + "'";
Statement s = connection.createStatement();
s.executeQuery (sql);
rs = s.getResultSet();
String bid = rs.getString("bus_id");
PreparedStatement pst2 = connection.prepareStatement("insert into booking_list values(?,?,?)");
pst2.setString(1,bkno);
pst2.setString(2,bid);
pst2.setString(3,tickets);
int k = pst2.executeUpdate();
rs.close ();
s.close ();
}catch(Exception e){
System.out.println("Exception is ;"+e);
}
RequestDispatcher dispatcher = request.getRequestDispatcher(page);
if (dispatcher != null){
dispatcher.forward(request, response);
}
}
}