Advertisement
Guest User

ServletLevel

a guest
Apr 17th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.96 KB | None | 0 0
  1. public class ServletLevel extends HttpServlet {
  2. private static final long serialVersionUID = 1L;
  3. private LevelDAOImpl levelDAOImpl;
  4.  
  5. public void init() {
  6. String jdbcURL = getServletContext().getInitParameter("jdbcURL");
  7. String jdbcUsername = getServletContext().getInitParameter("jdbcUsername");
  8. String jdbcPassword = getServletContext().getInitParameter("jdbcPassword");
  9.  
  10. DbConnection dbConnection = new DbConnection(jdbcURL, jdbcUsername, jdbcPassword);
  11. levelDAOImpl= new LevelDAOImpl(dbConnection);
  12.  
  13. }
  14. protected void doPost(HttpServletRequest request, HttpServletResponse response)
  15. throws ServletException, IOException {
  16. doGet(request, response);
  17. }
  18.  
  19. protected void doGet(HttpServletRequest request, HttpServletResponse response)
  20. throws ServletException, IOException {
  21. String action = request.getServletPath();
  22.  
  23. try {
  24. switch (action) {
  25. case "/newL":
  26. showNewForm(request, response);
  27. break;
  28. case "/insertL":
  29. insertLevel(request, response);
  30. break;
  31. case "/deleteL":
  32. deleteLevel(request, response);
  33. break;
  34. case "/editL":
  35. showEditForm(request, response);
  36. break;
  37. case "/updateL":
  38. updateLevel(request, response);
  39. break;
  40. case "/listL":
  41. listLevel(request, response);
  42. break;
  43. default:
  44. listLevel(request, response);
  45. break;
  46. }
  47. } catch (SQLException ex) {
  48. throw new ServletException(ex);
  49. }
  50. }
  51. private void listLevel(HttpServletRequest request, HttpServletResponse response)
  52. throws SQLException, IOException, ServletException {
  53. List<Level> listLevel = levelDAOImpl.GetAll();
  54. request.getSession().setAttribute("listLevel", listLevel);
  55. request.getRequestDispatcher("Level.jsp").forward(request, response);
  56.  
  57. }
  58.  
  59. private void showNewForm(HttpServletRequest request, HttpServletResponse response)
  60. throws ServletException, IOException {
  61. RequestDispatcher dispatcher = request.getRequestDispatcher("AddLevel.jsp");
  62. dispatcher.forward(request, response);
  63. }
  64.  
  65. private void showEditForm(HttpServletRequest request, HttpServletResponse response)
  66. throws SQLException, ServletException, IOException {
  67. int id_level = Integer.parseInt(request.getParameter("id_level"));
  68. Level existingLevel = levelDAOImpl.getId(id_level);
  69. RequestDispatcher dispatcher = request.getRequestDispatcher("AddLevel.jsp");
  70. request.setAttribute("level", existingLevel);
  71. dispatcher.forward(request, response);
  72.  
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement