github.com/ismailbayram/bigpicture@v0.0.0-20231225173155-e4b21f5efcff/internal/browser/javaproject/src/main/com/shashi/servlets/TrainBwStn.java (about) 1 package com.shashi.servlets; 2 3 import java.io.IOException; 4 import java.io.PrintWriter; 5 import java.util.List; 6 7 import javax.servlet.RequestDispatcher; 8 import javax.servlet.ServletException; 9 import javax.servlet.annotation.WebServlet; 10 import javax.servlet.http.HttpServlet; 11 import javax.servlet.http.HttpServletRequest; 12 import javax.servlet.http.HttpServletResponse; 13 14 import com.shashi.beans.TrainBean; 15 import com.shashi.beans.TrainException; 16 import com.shashi.constant.UserRole; 17 import com.shashi.service.TrainService; 18 import com.shashi.service.impl.TrainServiceImpl; 19 import com.shashi.utility.TrainUtil; 20 21 @SuppressWarnings("serial") 22 @WebServlet("/trainbwstn") 23 public class TrainBwStn extends HttpServlet { 24 private TrainService trainService = new TrainServiceImpl(); 25 26 protected void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException { 27 res.setContentType("text/html"); 28 PrintWriter pw = res.getWriter(); 29 TrainUtil.validateUserAuthorization(req, UserRole.CUSTOMER); 30 try { 31 String fromStation = req.getParameter("fromstation"); 32 String toStation = req.getParameter("tostation"); 33 List<TrainBean> trains = trainService.getTrainsBetweenStations(fromStation, toStation); 34 if (trains != null && !trains.isEmpty()) { 35 RequestDispatcher rd = req.getRequestDispatcher("UserHome.html"); 36 rd.include(req, res); 37 pw.println("<div class='main'><p1 class='menu'>Trains BetWeen Station " 38 + req.getParameter("fromstation") + " and " + req.getParameter("tostation") + "</p1></div>"); 39 pw.println("<div class='tab'><table><tr><th>Train Name</th><th>Train No</th>" 40 + "<th>From Stn</th><th>To Stn</th><th>Time</th><th>Seats</th><th>Fare (INR)</th><th>Action</th></tr>"); 41 for (TrainBean train : trains) { 42 int hr = (int) (Math.random() * 24); 43 int min = (int) (Math.random() * 60); 44 String time = (hr < 10 ? ("0" + hr) : hr) + ":" + ((min < 10) ? "0" + min : min); 45 46 pw.println("" + "<tr><td>" + train.getTr_name() + "</td>" + "<td>" + train.getTr_no() + "</td>" 47 + "<td>" + train.getFrom_stn() + "</td>" + "<td>" + train.getTo_stn() + "</td>" + "<td>" 48 + time + "</td>" + "<td>" + train.getSeats() + "</td>" + "<td>" + train.getFare() 49 + " RS</td><td><a href='booktrainbyref?trainNo=" + train.getTr_no() + "&fromStn=" 50 + train.getFrom_stn() + "&toStn=" + train.getTo_stn() 51 + "'><div class='red'>Book Now</div></a></td>" + "</tr>"); 52 } 53 pw.println("</table></div>"); 54 } else { 55 RequestDispatcher rd = req.getRequestDispatcher("TrainBwStn.html"); 56 rd.include(req, res); 57 pw.println("<div class='tab'><p1 class='menu'>There are no trains Between " 58 + req.getParameter("fromstation") + " and " + req.getParameter("tostation") + "</p1></div>"); 59 } 60 } catch (Exception e) { 61 throw new TrainException(422, this.getClass().getName() + "_FAILED", e.getMessage()); 62 } 63 64 } 65 }