github.com/ismailbayram/bigpicture@v0.0.0-20231225173155-e4b21f5efcff/internal/browser/javaproject/src/main/com/shashi/servlets/AdminSearchTrain.java (about) 1 package com.shashi.servlets; 2 3 import java.io.IOException; 4 import java.io.PrintWriter; 5 6 import javax.servlet.RequestDispatcher; 7 import javax.servlet.ServletException; 8 import javax.servlet.annotation.WebServlet; 9 import javax.servlet.http.HttpServlet; 10 import javax.servlet.http.HttpServletRequest; 11 import javax.servlet.http.HttpServletResponse; 12 13 import com.shashi.beans.TrainBean; 14 import com.shashi.beans.TrainException; 15 import com.shashi.constant.UserRole; 16 import com.shashi.service.TrainService; 17 import com.shashi.service.impl.TrainServiceImpl; 18 import com.shashi.utility.TrainUtil; 19 20 @SuppressWarnings("serial") 21 @WebServlet("/adminsearchtrain") 22 public class AdminSearchTrain extends HttpServlet { 23 24 private TrainService trainService = new TrainServiceImpl(); 25 26 /** 27 * 28 * @param req 29 * @param res 30 * @throws IOException 31 * @throws ServletException 32 */ 33 protected void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException { 34 res.setContentType("text/html"); 35 PrintWriter pw = res.getWriter(); 36 TrainUtil.validateUserAuthorization(req, UserRole.ADMIN); 37 try { 38 String trainNo = req.getParameter("trainnumber"); 39 TrainBean train = trainService.getTrainById(trainNo); 40 if (train != null) { 41 RequestDispatcher rd = req.getRequestDispatcher("AdminSearchTrain.html"); 42 rd.include(req, res); 43 pw.println("<div class='main'><p1 class='menu'>Searched Train Detail</p1></div>"); 44 pw.println("<div class='tab'>" + "<table>" + "<tr><td class='blue'>Train Name :</td><td>" 45 + train.getTr_name() + "</td></tr>" + "<tr><td class='blue'>Train Number :</td><td>" 46 + train.getTr_no() + "</td></tr>" + "<tr><td class='blue'>From Station :</td><td>" 47 + train.getFrom_stn() + "</td></tr>" + "<tr><td class='blue'>To Station :</td><td>" 48 + train.getTo_stn() + "</td></tr>" + "<tr><td class='blue'>Available Seats:</td><td>" 49 + train.getSeats() + "</td></tr>" + "<tr><td class='blue'>Fare (INR) :</td><td>" 50 + train.getFare() + " RS</td></tr>" + "</table>" + "</div>"); 51 } else { 52 RequestDispatcher rd = req.getRequestDispatcher("AdminSearchTrain.html"); 53 rd.include(req, res); 54 pw.println("<div class='tab'><p1 class='menu'>Train No." + trainNo + " is Not Available !</p1></div>"); 55 } 56 } catch (Exception e) { 57 throw new TrainException(422, this.getClass().getName() + "_FAILED", e.getMessage()); 58 } 59 60 } 61 62 }