SHARE
TWEET

AdminServlet

a guest Jun 8th, 2017 92 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package hbo.it.www;
  7.  
  8. import hbo5.it.www.beans.Airport;
  9. import hbo5.it.www.beans.Flight;
  10. import hbo5.it.www.dataaccess.DAAirport;
  11. import hbo5.it.www.dataaccess.DAFlight;
  12. import java.io.IOException;
  13. import java.io.PrintWriter;
  14. import java.sql.SQLException;
  15. import java.util.ArrayList;
  16. import javax.servlet.RequestDispatcher;
  17. import javax.servlet.ServletException;
  18. import javax.servlet.annotation.WebInitParam;
  19. import javax.servlet.annotation.WebServlet;
  20. import javax.servlet.http.HttpServlet;
  21. import javax.servlet.http.HttpServletRequest;
  22. import javax.servlet.http.HttpServletResponse;
  23.  
  24. /**
  25.  *
  26.  * @author Sam
  27.  */
  28. @WebServlet(name = "AdminServlet", urlPatterns = {"/AdminServlet"}, initParams = {
  29.     @WebInitParam(name = "url", value = "jdbc:oracle:thin:@ti-oracledb06.thomasmore.be:1521:XE")
  30.     , @WebInitParam(name = "login", value = "c1039607")
  31.     , @WebInitParam(name = "password", value = "6125")
  32.     , @WebInitParam(name = "driver", value = "oracle.jdbc.driver.OracleDriver")})
  33. public class AdminServlet extends HttpServlet {
  34.  
  35.    
  36.  
  37.     private DAFlight daflight = null;
  38.     private DAAirport daairport = null;
  39.  
  40.     @Override
  41.     public void init() throws ServletException {
  42.         try {
  43.             String url = getInitParameter("url");
  44.             String password = getInitParameter("password");
  45.             String login = getInitParameter("login");
  46.             String driver = getInitParameter("driver");
  47.             if (daflight == null) {
  48.                 daflight = new DAFlight(url, login, password, driver);
  49.             }
  50.             if (daairport == null) {
  51.                 daairport = new DAAirport(url, login, password, driver);
  52.             }
  53.  
  54.         } catch (ClassNotFoundException | SQLException e) {
  55.             throw new ServletException(e);
  56.         }
  57.     }
  58.  
  59.     @Override
  60.     public void destroy() {
  61.         try {
  62.             if (daflight != null) {
  63.                 daflight.close();
  64.             }
  65.             if (daairport != null) {
  66.                 daairport.close();
  67.             }
  68.  
  69.         } catch (SQLException ex) {
  70.  
  71.         }
  72.     }
  73.    
  74.     /**
  75.      * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
  76.      * methods.
  77.      *
  78.      * @param request servlet request
  79.      * @param response servlet response
  80.      * @throws ServletException if a servlet-specific error occurs
  81.      * @throws IOException if an I/O error occurs
  82.      */
  83.     protected void processRequest(HttpServletRequest request, HttpServletResponse response)
  84.             throws ServletException, IOException {
  85.  
  86.         RequestDispatcher rd = null;
  87.        
  88.         if (request.getParameter("toSearch") != null) {
  89.  
  90.             //luchthavens ophalen
  91.             ArrayList<Airport> airports = daairport.getAirports();
  92.             request.setAttribute("airports", airports);
  93.  
  94.             rd = request.getRequestDispatcher("adminAirports.jsp");
  95.         }
  96.  
  97.         rd.forward(request, response);
  98.  
  99.     }
  100.  
  101. // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
  102.     /**
  103.      * Handles the HTTP <code>GET</code> method.
  104.      *
  105.      * @param request servlet request
  106.      * @param response servlet response
  107.      * @throws ServletException if a servlet-specific error occurs
  108.      * @throws IOException if an I/O error occurs
  109.      */
  110.     @Override
  111.     protected void doGet(HttpServletRequest request, HttpServletResponse response)
  112.             throws ServletException, IOException {
  113.         processRequest(request, response);
  114.     }
  115.  
  116.     /**
  117.      * Handles the HTTP <code>POST</code> method.
  118.      *
  119.      * @param request servlet request
  120.      * @param response servlet response
  121.      * @throws ServletException if a servlet-specific error occurs
  122.      * @throws IOException if an I/O error occurs
  123.      */
  124.     @Override
  125.     protected void doPost(HttpServletRequest request, HttpServletResponse response)
  126.             throws ServletException, IOException {
  127.         processRequest(request, response);
  128.     }
  129.  
  130.     /**
  131.      * Returns a short description of the servlet.
  132.      *
  133.      * @return a String containing servlet description
  134.      */
  135.     @Override
  136.     public String getServletInfo() {
  137.         return "Short description";
  138.     }// </editor-fold>
  139.  
  140. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top