Advertisement
sergAccount

Untitled

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