github.com/ismailbayram/bigpicture@v0.0.0-20231225173155-e4b21f5efcff/internal/browser/javaproject/src/main/com/shashi/servlets/AdminViewTrainFwd.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("/adminviewtrainfwd") 23 public class AdminViewTrainFwd extends HttpServlet { 24 25 private TrainService trainService = new TrainServiceImpl(); 26 27 protected void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException { 28 res.setContentType("text/html"); 29 PrintWriter pw = res.getWriter(); 30 TrainUtil.validateUserAuthorization(req, UserRole.ADMIN); 31 try { 32 List<TrainBean> trains = trainService.getAllTrains(); 33 if (trains != null && !trains.isEmpty()) { 34 RequestDispatcher rd = req.getRequestDispatcher("ViewTrains.html"); 35 rd.include(req, res); 36 pw.println("<div class='main'><p1 class='menu'>Running Trains</p1></div>"); 37 pw.println("<div class='tab'><table><tr><th>Train Name</th><th>Train Number</th>" 38 + "<th>From Station</th><th>To Station</th><th>Seats Available</th><th>Fare (INR)</th><th>Action</th></tr>"); 39 40 for (TrainBean train : trains) { 41 42 pw.println("" + "<tr> " + "" + "<td><a href='viewadmin?trainNo=" + train.getTr_no() + "&fromStn=" 43 + train.getFrom_stn() + "&toStn=" + train.getTo_stn() + "'>" + train.getTr_name() 44 + "</a></td>" + "<td>" + train.getTr_no() + "</td>" + "<td>" + train.getFrom_stn() + "</td>" 45 + "<td>" + train.getTo_stn() + "</td>" + "<td>" + train.getSeats() + "</td>" + "<td>" 46 + train.getFare() + " RS</td>" + "<td><a href='adminupdatetrain?trainnumber=" 47 + train.getTr_no() + "'>Update</a></td>" + "</tr>"); 48 } 49 pw.println("</table></div>"); 50 } else { 51 RequestDispatcher rd = req.getRequestDispatcher("ViewTrains.html"); 52 rd.include(req, res); 53 pw.println("<div class='main'><p1 class='menu red'> No Running Trains</p1></div>"); 54 } 55 } catch (Exception e) { 56 throw new TrainException(422, this.getClass().getName() + "_FAILED", e.getMessage()); 57 58 } 59 60 } 61 62 }