Advertisement
sergAccount

Untitled

Apr 11th, 2021
714
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.47 KB | None | 0 0
  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 com.mycompany.service;
  7.  
  8. import java.io.IOException;
  9. import java.io.PrintWriter;
  10. import java.util.Enumeration;
  11. import javax.servlet.ServletException;
  12. import javax.servlet.annotation.WebServlet;
  13. import javax.servlet.http.HttpServlet;
  14. import javax.servlet.http.HttpServletRequest;
  15. import javax.servlet.http.HttpServletResponse;
  16.  
  17. /**
  18.  *
  19.  * @author Admin
  20.  */
  21. @WebServlet(name = "MyServlet", urlPatterns = {"/MyServlet"})
  22. public class MyServlet extends HttpServlet {
  23.  
  24.     /**
  25.      * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
  26.      * methods.
  27.      *
  28.      * @param request servlet request
  29.      * @param response servlet response
  30.      * @throws ServletException if a servlet-specific error occurs
  31.      * @throws IOException if an I/O error occurs
  32.      */
  33.     protected void processRequest(HttpServletRequest request, HttpServletResponse response)
  34.             throws ServletException, IOException {
  35.         // 1)
  36.         // получаем значение параметра по имени
  37.         String value = request.getParameter("param1");
  38.         System.out.println("MyServlet.value=" + value);        
  39.         // 2)
  40.         Enumeration<String> en = request.getParameterNames();
  41.         while(en.hasMoreElements()){
  42.             System.out.println("en.name=" + en.nextElement());
  43.         }
  44.        
  45.         response.setContentType("text/html;charset=UTF-8");
  46.         try (PrintWriter out = response.getWriter()) {
  47.             /* TODO output your page here. You may use following sample code. */
  48.             out.println("<!DOCTYPE html>");
  49.             out.println("<html>");
  50.             out.println("<head>");
  51.             out.println("<title>Servlet MyServlet</title>");            
  52.             out.println("</head>");
  53.             out.println("<body>");
  54.             out.println("<h1>Servlet MyServlet at " + request.getContextPath() + "</h1>");
  55.             out.println("</body>");
  56.             out.println("</html>");
  57.         }
  58.     }
  59.  
  60.     // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
  61.     /**
  62.      * Handles the HTTP <code>GET</code> method.
  63.      *
  64.      * @param request servlet request
  65.      * @param response servlet response
  66.      * @throws ServletException if a servlet-specific error occurs
  67.      * @throws IOException if an I/O error occurs
  68.      */
  69.     @Override
  70.     protected void doGet(HttpServletRequest request, HttpServletResponse response)
  71.             throws ServletException, IOException {
  72.         processRequest(request, response);
  73.     }
  74.  
  75.     /**
  76.      * Handles the HTTP <code>POST</code> method.
  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.     @Override
  84.     protected void doPost(HttpServletRequest request, HttpServletResponse response)
  85.             throws ServletException, IOException {
  86.         processRequest(request, response);
  87.     }
  88.  
  89.     /**
  90.      * Returns a short description of the servlet.
  91.      *
  92.      * @return a String containing servlet description
  93.      */
  94.     @Override
  95.     public String getServletInfo() {
  96.         return "Short description";
  97.     }// </editor-fold>
  98.  
  99. }
  100.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement