Advertisement
MagnusArias

PS2 | Servlets - Cooperation

Apr 3rd, 2017
335
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.33 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.  
  7. import java.io.IOException;
  8. import java.io.PrintWriter;
  9. import javax.servlet.ServletException;
  10. import javax.servlet.http.HttpServlet;
  11. import javax.servlet.http.HttpServletRequest;
  12. import javax.servlet.http.HttpServletResponse;
  13. import java.text.DateFormat;
  14. import java.text.SimpleDateFormat;
  15. import java.util.Date;
  16. /**
  17.  *
  18.  * @author student_10
  19.  */
  20. public class NewServlet extends HttpServlet {
  21.  
  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.    
  33.     protected void processRequest(HttpServletRequest request, HttpServletResponse response)
  34.             throws ServletException, IOException {
  35.                
  36.         response.setContentType("text/html;charset=UTF-8");
  37.         PrintWriter out = response.getWriter();
  38.        
  39.         DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
  40.         Date date = new Date();
  41.         String dataLaunch = dateFormat.format(date);
  42.        
  43.         try {
  44.             out.println(dataLaunch);
  45.            
  46.             out.println("<form method=\"get\" action=\"second\">");
  47.                 out.println("imie: <input type=\"text\" name=\"user\">");
  48.                 out.println("<input type=\"submit\" value=\"zaloguj\">");
  49.             out.println("</form>");
  50.         }
  51.         finally {
  52.             out.close();
  53.         }
  54.     }
  55.  
  56.     // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
  57.     /**
  58.      * Handles the HTTP <code>GET</code> method.
  59.      *
  60.      * @param request servlet request
  61.      * @param response servlet response
  62.      * @throws ServletException if a servlet-specific error occurs
  63.      * @throws IOException if an I/O error occurs
  64.      */
  65.     @Override
  66.     protected void doGet(HttpServletRequest request, HttpServletResponse response)
  67.             throws ServletException, IOException {
  68.         processRequest(request, response);
  69.        
  70.        
  71.     }
  72.  
  73.     /**
  74.      * Handles the HTTP <code>POST</code> method.
  75.      *
  76.      * @param request servlet request
  77.      * @param response servlet response
  78.      * @throws ServletException if a servlet-specific error occurs
  79.      * @throws IOException if an I/O error occurs
  80.      */
  81.     @Override
  82.     protected void doPost(HttpServletRequest request, HttpServletResponse response)
  83.             throws ServletException, IOException {
  84.         processRequest(request, response);
  85.     }
  86.  
  87.     /**
  88.      * Returns a short description of the servlet.
  89.      *
  90.      * @return a String containing servlet description
  91.      */
  92.     @Override
  93.     public String getServletInfo() {
  94.         return "Short description";
  95.     }// </editor-fold>
  96.  
  97. }
  98.  
  99.  
  100. /*
  101.  * To change this license header, choose License Headers in Project Properties.
  102.  * To change this template file, choose Tools | Templates
  103.  * and open the template in the editor.
  104.  */
  105.  
  106. import java.io.IOException;
  107. import java.io.PrintWriter;
  108. import javax.servlet.ServletException;
  109. import javax.servlet.http.HttpServlet;
  110. import javax.servlet.http.HttpServletRequest;
  111. import javax.servlet.http.HttpServletResponse;
  112. import java.util.Collection;
  113. import java.util.Collections;
  114. import java.util.Vector;
  115. import java.util.Iterator;
  116.  
  117.  
  118. /**
  119.  *
  120.  * @author student_10
  121.  */
  122. public class Servlet2 extends HttpServlet {
  123.  
  124.     /**
  125.      * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
  126.      * methods.
  127.      *
  128.      * @param request servlet request
  129.      * @param response servlet response
  130.      * @throws ServletException if a servlet-specific error occurs
  131.      * @throws IOException if an I/O error occurs
  132.      */
  133.     protected void processRequest(HttpServletRequest request, HttpServletResponse response)
  134.             throws ServletException, IOException {
  135.        
  136.         response.setContentType("text/html;charset=UTF-8");
  137.         PrintWriter out = response.getWriter();
  138.         String name;
  139.        
  140.         try {
  141.             name = request.getParameter("user");
  142.            
  143.             /* TODO output your page here. You may use following sample code. */
  144.             out.println("<!DOCTYPE html>");
  145.             out.println("<html>");
  146.                 out.println("<head>");
  147.                     out.println("<title>Servlet Servlet2</title>");            
  148.                 out.println("</head>");
  149.                 out.println("<body>");
  150.                     out.println("<h1>yo " + name + "</h1>");
  151.                     out.println("<a href=\"sv\">Wdup coby cofnac</a>");
  152.                 out.println("</body>");
  153.             out.println("</html>");
  154.         }
  155.         finally {
  156.                 out.close();
  157.             }
  158.     }
  159.  
  160.     // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
  161.     /**
  162.      * Handles the HTTP <code>GET</code> method.
  163.      *
  164.      * @param request servlet request
  165.      * @param response servlet response
  166.      * @throws ServletException if a servlet-specific error occurs
  167.      * @throws IOException if an I/O error occurs
  168.      */
  169.     @Override
  170.     protected void doGet(HttpServletRequest request, HttpServletResponse response)
  171.             throws ServletException, IOException {
  172.         processRequest(request, response);
  173.     }
  174.  
  175.     /**
  176.      * Handles the HTTP <code>POST</code> method.
  177.      *
  178.      * @param request servlet request
  179.      * @param response servlet response
  180.      * @throws ServletException if a servlet-specific error occurs
  181.      * @throws IOException if an I/O error occurs
  182.      */
  183.     @Override
  184.     protected void doPost(HttpServletRequest request, HttpServletResponse response)
  185.             throws ServletException, IOException {
  186.         processRequest(request, response);
  187.     }
  188.  
  189.     /**
  190.      * Returns a short description of the servlet.
  191.      *
  192.      * @return a String containing servlet description
  193.      */
  194.     @Override
  195.     public String getServletInfo() {
  196.         return "Short description";
  197.     }// </editor-fold>
  198.  
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement