Write a web application using servlet and database to reserve bus tickets and generate passengers tickets

Spread the love
This is The first page which has only one Submit button which when clicked leads to display.java servlet

1
2
3
4
5
6
7
8
9
10
11
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <form action="display" method="get">
            <input type="submit" value="Submit">
        </form>       
    </body>
</html>

display.java thisfile shows the status of seats available in a bus

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package pack1;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import javax.sql.*;
import java.sql.*;
import java.lang.*;
public class display extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        response.setContentType("text/html");
        try {
            out.println("<html>");
            out.println("<body>");
            LinkedList l = new LinkedList();
            l.add("1");
            l.add("2");
            l.add("3");
            l.add("4");
            l.add("5");
            l.add("6");
            l.add("7");
            l.add("8");
            l.add("9");
            l.add("10");
            out.println("<center><h1>" + l + "</h1>");
            out.println("<a href = 'seat'>Book A Ticket</a>");           
        } catch (Exception e) {
            out.println(e);
        }
    }
}

the hyperlink leads to the seat.java file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package pack1;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.sql.*;
import javax.sql.*;
public class seat extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        response.setContentType("text/html");
        String cust_name = request.getParameter("name");
        String dest1 = request.getParameter("dest1");
        String dest2 = request.getParameter("dest2");
        String seat = request.getParameter("seat");
        try {
            LinkedList l = new LinkedList();
            out.println("<html>");
            out.println("<body>");
            out.println("<center>");
            out.println("<form method = 'get' action = 'booked'>");
            out.println("<h1><u>Fill The Form Below To Book A Ticket<u></h1>");
            out.println("<table><tr>");
            out.println("<th>Name :</th>");
            out.println("<td><input type = 'text' name = 'cust_name'></td>");
            out.println("<th>Destination :</th>");
            out.println("<td>From :</td>");
            out.println("<td><input type = 'text' name = 'dest1'></td>");
            out.println("<td>TO :</td>");
            out.println("<td><input type = 'text' name = 'dest2'></td></th>");
            out.println("<th>Seat No :</th>");
            out.println("<td><input type = 'text' name = 'seat'></td></th></tr></table><br>");
            out.println("<input type = 'submit' value = 'Book'>");
            out.println("</form>");
            out.println("</center>");
            out.println("</body>");
            out.println("</html>");
        } catch (Exception e) {
            out.println(e);
        }
    }
}

this will allow user to fill up the details form and book a ticket and will lead to booked.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package pack1;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
import javax.sql.*;
import java.util.*;
/**
 *
 * @author Pranit
 */
public class booked extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        response.setContentType("text/html");
        String cust_name = request.getParameter("cust_name");
        String dest1 = request.getParameter("dest1");
        String dest2 = request.getParameter("dest2");
        String seat = request.getParameter("seat");
        LinkedList l = new LinkedList();
        try{
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection cn = DriverManager.getConnection("jdbc:odbc:travel","system","tiger");
            PreparedStatement s = cn.prepareStatement("insert into travel values(?,?,?,?)");
            s.setString(1, cust_name);
            s.setString(2, dest1);
            s.setString(3,dest2);
            s.setString(4,seat);
            s.executeUpdate();
            out.println("Ticket has been booked");
            l.remove(request.getParameter("seat"));
            out.println("<h1>"+l+"</h1>");
        }catch(Exception e){
            out.println(e);
        }
    }
}

What I Want to show is that after a ticket is booked it should reduce that no from the array list which was chosen at the time of booking wherein it shows a blank array list!

Ranch Hand
Posts: 43
Android
 C++ Java

  • Mark post as helpful
  • send pies 
  • Quote
  • Report post to moderator
There was a mistake you just created a new LinkedList in Booked.java but you didn’t initialized it. One more thing you were passing a String to remove() method which is not correct. you should first convet it to Integeras I did . I have used session to initialize it and it is working. 
The Edited portion is highlighted

Display.java

import java.io.IOException;
import java.io.PrintWriter;

import javax.jms.Session;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import java.util.*;
import javax.sql.*;
import java.sql.*;
import java.lang.*;

public class Display extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
response.setContentType(“text/html”);
try {
out.println(“<html>”);
out.println(“<body>”);
LinkedList l = new LinkedList();
l.add(“1”);
l.add(“2”);
l.add(“3”);
l.add(“4”);
l.add(“5”);
l.add(“6”);
l.add(“7”);
l.add(“8”);
l.add(“9”);
l.add(“10”);
HttpSession session = request.getSession(true);
session.setAttribute(“linked”, l);

out.println(“<center><h1>” + l + “</h1>”);
out.println(“<a href = ‘Seat’>Book A Ticket</a>”);
} catch (Exception e) {
out.println(e);
}
}
}

Book.java

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import java.sql.*;
import javax.sql.*;
import java.util.*;

/**
*
* @author Pranit
*/
public class Booked extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
response.setContentType(“text/html”);
String cust_name = request.getParameter(“cust_name”);
String dest1 = request.getParameter(“dest1”);
String dest2 = request.getParameter(“dest2”);
String seat = request.getParameter(“seat”);
int n=0;
HttpSession session = request.getSession(true);
try{n=Integer.parseInt(seat);}catch(Exception e){}
LinkedList l =(LinkedList) session.getAttribute(“linked”);

try{
Class.forName(“com.mysql.jdbc.Driver”);
Connection cn = DriverManager.getConnection(“jdbc:mysql:///bus”,”root”,”admin”);
PreparedStatement s = cn.prepareStatement(“insert into travel values(?,?,?,?)”);
s.setString(1, cust_name);
s.setString(2, dest1);
s.setString(3,dest2);
s.setString(4,seat);
s.executeUpdate();
out.println(“Ticket has been booked”);
try{
l.remove(n-1);}catch(Exception e){out.print(e);}

out.println(“<h1>”+l+”</h1>”);
}catch(Exception e){
out.println(e);
}
}
}

Rest of your code is correct. All the best

Leave a Reply