SHARE
TWEET

Untitled

a guest Jun 12th, 2019 57 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 be.vives.prog3.web;
  7.  
  8. import be.vives.prog3.control.BlogJpaController;
  9. import be.vives.prog3.control.exceptions.NonexistentEntityException;
  10. import be.vives.prog3.model.Blog;
  11. import com.google.gson.Gson;
  12. import java.io.IOException;
  13. import java.io.PrintWriter;
  14. import java.util.List;
  15. import javax.persistence.EntityManagerFactory;
  16. import javax.servlet.ServletContext;
  17. import javax.servlet.ServletException;
  18. import javax.servlet.annotation.WebServlet;
  19. import javax.servlet.http.HttpServlet;
  20. import javax.servlet.http.HttpServletRequest;
  21. import javax.servlet.http.HttpServletResponse;
  22.  
  23. /**
  24.  *
  25.  * @author Jean-Pierre
  26.  */
  27. @WebServlet(name = "BlogServlet", urlPatterns = {"/BlogServlet"})
  28. public class BlogServlet extends HttpServlet {
  29.  
  30.     private BlogJpaController blogJpaController;
  31.    
  32.     @Override
  33.     public void init() throws ServletException {
  34.         super.init(); //To change body of generated methods, choose Tools | Templates.
  35.         ServletContext context = this.getServletContext();
  36.         EntityManagerFactory emf =
  37.                 (EntityManagerFactory) context.getAttribute("EntityManagerFactory");
  38.         blogJpaController = new BlogJpaController(emf);
  39.     }
  40.  
  41.     /**
  42.      * Als de request een parameter id heeft : deze Blog ophalen, anders alle Blogs
  43.      * @param request
  44.      * @param response
  45.      * @throws ServletException
  46.      * @throws IOException
  47.      */
  48.     protected void doGet(HttpServletRequest request, HttpServletResponse response)
  49.             throws ServletException, IOException {
  50.         // 1. responseType instellen.
  51.         response.setContentType("application/json;charset=UTF-8");
  52.  
  53.         // 2. Parameters uit de request halen (indien nodig)
  54.         //Als de request een parameter id heeft : deze Blog ophalen, anders alle Blogs
  55.         Integer id;
  56.         try {
  57.             id = Integer.parseInt(request.getParameter("id"));
  58.         } catch (NumberFormatException e) {
  59.             id = null;
  60.         }
  61.  
  62.         Gson gson = new Gson();
  63.         String jsonString;
  64.         // 3. Instantie van het model ophalen en raadplegen.
  65.         // 4. resultaat converteren naar een JSON String.
  66.         if (id == null) {
  67.             List<Blog> blogList = blogJpaController.findBlogEntities();
  68.             jsonString = gson.toJson(blogList);
  69.         } else {
  70.             Blog blog = blogJpaController.findBlog(id);
  71.             jsonString = gson.toJson(blog);
  72.         }
  73.  
  74.         // 5. Output genereren
  75.         try (PrintWriter out = response.getWriter()) {
  76.             out.println(jsonString);
  77.         }
  78.     }
  79.    
  80.     //New Post
  81.     @Override
  82.     protected void doPost(HttpServletRequest request, HttpServletResponse response)
  83.             throws ServletException, IOException {
  84.         // 1. responseType instellen.
  85.         response.setContentType("application/json;charset=UTF-8");
  86.  
  87.         // 2. Parameters uit de request halen (indien nodig)
  88.         String title   = request.getParameter("title");
  89.         String content = request.getParameter("content");
  90.         //Map<String, String[]> params = request.getParameterMap();
  91.  
  92.         // 3. Instantie van het model ophalen en raadplegen.
  93.         Blog blog = new Blog(title, content);
  94.         blogJpaController.create(blog);
  95.  
  96.         // 4. resultaat converteren naar een JSON String.
  97.         Gson gson = new Gson();
  98.         String jsonString = gson.toJson(blog);
  99.  
  100.         // 5. Output genereren
  101.         try (PrintWriter out = response.getWriter()) {
  102.             out.println(jsonString);
  103.         }
  104.     }
  105.  
  106.     @Override
  107.     protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  108.         // 1. responseType instellen.
  109.         resp.setContentType("application/json;charset=UTF-8");
  110.  
  111.         // 2. Parameters uit de request halen (indien nodig)
  112.         Integer id;
  113.         try {
  114.             id = Integer.parseInt(req.getParameter("id"));
  115.         } catch (NumberFormatException e) {
  116.             id = -1;
  117.         }
  118.  
  119.         // 3. Instantie van het model ophalen en raadplegen.
  120.         int aantal = 1;
  121.         try {
  122.             blogJpaController.destroy(id);
  123.         } catch (NonexistentEntityException ex) {
  124.             aantal = 0;
  125.         }
  126.  
  127.         // 4. resultaat converteren naar een JSON String.
  128.         Gson gson = new Gson();
  129.         String jsonString = gson.toJson(aantal);
  130.  
  131.         // 5. Output genereren
  132.         try (PrintWriter out = resp.getWriter()) {
  133.             out.println(jsonString);
  134.         }
  135.     }
  136.  
  137.     //Update Post
  138.     @Override
  139.     protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  140.         // 1. responseType instellen.
  141.         resp.setContentType("application/json;charset=UTF-8");
  142.  
  143.         // 2. Parameters uit de request halen (indien nodig)
  144.         Integer id;
  145.         try {
  146.             id = Integer.parseInt(req.getParameter("id"));
  147.         } catch (NumberFormatException e) {
  148.             id = -1;
  149.         }
  150.         String title   = req.getParameter("title");
  151.         String content = req.getParameter("content");
  152.  
  153.         // 3. Instantie van het model ophalen en raadplegen.
  154.         Blog blog = new Blog(id, title, content);
  155.         int aantal = 1;
  156.         try {
  157.             blogJpaController.edit(blog);
  158.         } catch (Exception ex) {
  159.             aantal = 0;
  160.         }
  161.  
  162.         // 4. resultaat converteren naar een JSON String.
  163.         Gson gson = new Gson();
  164.         String jsonString = gson.toJson(aantal);
  165.  
  166.         // 5. Output genereren
  167.         try (PrintWriter out = resp.getWriter()) {
  168.             out.println(jsonString);
  169.         }
  170.     }
  171.  
  172.     /**
  173.      * Returns a short description of the servlet.
  174.      *
  175.      * @return a String containing servlet description
  176.      */
  177.     @Override
  178.     public String getServletInfo() {
  179.         return "Short description";
  180.     }// </editor-fold>
  181.  
  182.  
  183. }
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