Guest User

MyServlet

a guest
Feb 29th, 2016
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.08 KB | None | 0 0
  1. package servlet;
  2.  
  3. import java.io.IOException;
  4. import java.io.PrintWriter;
  5. import java.lang.reflect.Type;
  6. import java.util.List;
  7.  
  8. import javax.servlet.ServletConfig;
  9. import javax.servlet.ServletException;
  10. import javax.servlet.annotation.WebServlet;
  11. import javax.servlet.http.HttpServlet;
  12. import javax.servlet.http.HttpServletRequest;
  13. import javax.servlet.http.HttpServletResponse;
  14.  
  15. import com.google.gson.Gson;
  16. import com.google.gson.reflect.TypeToken;
  17.  
  18. import bean.BeanArticle;
  19. import bean.BeanAuthentification;
  20. import bean.BeanEquipement;
  21. import entite.Article;
  22.  
  23. /**
  24.  * Servlet implementation class Control
  25.  */
  26. //@WebServlet("/control")
  27. public class Control extends HttpServlet {
  28.     private static final long serialVersionUID = 1L;
  29.  
  30.  
  31.     protected void processRequest(HttpServletRequest req, HttpServletResponse rep)
  32.     throws ServletException, IOException {
  33.  
  34.             rep.setContentType("application/json");
  35.             req.getRequestDispatcher("/WEB-INF/index.html").forward(req, rep);
  36.            
  37.            
  38.             BeanArticle a = new BeanArticle();                 
  39.             Gson gson = new Gson();
  40.             Type type = new TypeToken<List<Article>>() {}.getType();
  41.             String json = gson.toJson(a.findArticle(), type);    
  42.            
  43.             PrintWriter out = rep.getWriter();
  44.             out.print(json);
  45.             out.flush();
  46.            
  47.     }
  48.    
  49.     /**
  50.      * @see HttpServlet#HttpServlet()
  51.      */
  52.     public Control() {
  53.         super();
  54.     }
  55.  
  56.     /**
  57.      * @see Servlet#init(ServletConfig)
  58.      */
  59.     public void init(ServletConfig config) throws ServletException {
  60.         // TODO Auto-generated method stub
  61.     }
  62.  
  63.     /**
  64.      * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
  65.      *      response)
  66.      */
  67.     protected void doGet(HttpServletRequest request, HttpServletResponse response)
  68.             throws ServletException, IOException {
  69.         processRequest(request, response);
  70.  
  71.         }
  72.        
  73.    
  74.  
  75.     /**
  76.      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
  77.      *      response)
  78.      */
  79.     protected void doPost(HttpServletRequest request, HttpServletResponse response)
  80.             throws ServletException, IOException {
  81.         processRequest(request, response);
  82.  
  83.     }
  84.  
  85. }
Advertisement
Add Comment
Please, Sign In to add comment