github.com/ismailbayram/bigpicture@v0.0.0-20231225173155-e4b21f5efcff/internal/browser/javaproject/src/main/com/shashi/servlets/AdminAddTrain.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.ResponseCode;
    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  @WebServlet("/adminaddtrain")
    22  public class AdminAddTrain extends HttpServlet {
    23  
    24  	/**
    25  	 * 
    26  	 */
    27  	private static final long serialVersionUID = 1L;
    28  
    29  	private TrainService trainService = new TrainServiceImpl();
    30  
    31  	/**
    32  	 * 
    33  	 * @param req
    34  	 * @param res
    35  	 * @throws IOException
    36  	 * @throws ServletException
    37  	 */
    38  	protected void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
    39  		res.setContentType("text/html");
    40  		PrintWriter pw = res.getWriter();
    41  		TrainUtil.validateUserAuthorization(req, UserRole.ADMIN);
    42  		try {
    43  			TrainBean train = new TrainBean();
    44  			train.setTr_no(Long.parseLong(req.getParameter("trainno")));
    45  			train.setTr_name(req.getParameter("trainname").toUpperCase());
    46  			train.setFrom_stn(req.getParameter("fromstation").toUpperCase());
    47  			train.setTo_stn(req.getParameter("tostation").toUpperCase());
    48  			train.setSeats(Integer.parseInt(req.getParameter("available")));
    49  			train.setFare(Double.parseDouble(req.getParameter("fare")));
    50  			String message = trainService.addTrain(train);
    51  			if (ResponseCode.SUCCESS.toString().equalsIgnoreCase(message)) {
    52  				RequestDispatcher rd = req.getRequestDispatcher("AddTrains.html");
    53  				rd.include(req, res);
    54  				pw.println("<div class='tab'><p1 class='menu'>Train Added Successfully!</p1></div>");
    55  			} else {
    56  				RequestDispatcher rd = req.getRequestDispatcher("AddTrains.html");
    57  				rd.include(req, res);
    58  				pw.println("<div class='tab'><p1 class='menu'>Error in filling the train Detail</p1></div>");
    59  			}
    60  		} catch (Exception e) {
    61  			throw new TrainException(422, this.getClass().getName() + "_FAILED", e.getMessage());
    62  		}
    63  
    64  	}
    65  
    66  }