Advertisement
CYB3Rhuman

Untitled

Aug 23rd, 2012
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.69 KB | None | 0 0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package org.golovin.vr;
  6.  
  7. import java.io.IOException;
  8. import java.io.PrintWriter;
  9. import java.util.Enumeration;
  10. import javax.servlet.ServletException;
  11. import javax.servlet.http.HttpServlet;
  12. import javax.servlet.http.HttpServletRequest;
  13. import javax.servlet.http.HttpServletResponse;
  14.  
  15. /**
  16.  *
  17.  * @author golovin
  18.  */
  19. public class RegistrationForm extends HttpServlet {
  20.  
  21.     /**
  22.      * Processes requests for both HTTP
  23.      * <code>GET</code> and
  24.      * <code>POST</code> methods.
  25.      *
  26.      * @param request servlet request
  27.      * @param response servlet response
  28.      * @throws ServletException if a servlet-specific error occurs
  29.      * @throws IOException if an I/O error occurs
  30.      */
  31.     protected void processRequest(HttpServletRequest request, HttpServletResponse response)
  32.             throws ServletException, IOException {
  33.         response.setContentType("text/html;charset=UTF-8");
  34.         request.setCharacterEncoding("UTF-8");
  35.         response.setCharacterEncoding("UTF-8");
  36.         PrintWriter out = response.getWriter();
  37.         try {
  38.             /* TODO output your page here. You may use following sample code. */
  39.             out.println("<!doctype html>");
  40.             out.println("<html>");
  41.  
  42.             out.println("<head>");
  43.             out.println("<title>Registration Form</title>");
  44.             out.println("</head>");
  45.  
  46.             out.println("<body>");
  47.             out.println("<h2>Пожалуйста, заполните следующую форму:</h2>");
  48.  
  49.             out.println("<form method='post' accept-charset='UTF-8' action='" +
  50.                     request.getContextPath()+ "/srv/regform'>");
  51.             out.println("<table border='0'>");
  52.  
  53.             out.println("<tr>");
  54.             out.println("<td valign='top'>Имя:</td>");
  55.             out.println("<td valign='top'><input type='text' name='firstname' size='20'></td>");
  56.             out.println("</tr>");
  57.  
  58.             out.println("<tr>");
  59.             out.println("<td valign='top'>Фамилия:</td>");
  60.             out.println("<td valign='top'><input type='text' name='lastname' size='20'></td>");
  61.             out.println("</tr>");
  62.  
  63.             out.println("<tr>");
  64.             out.println("<td valign='top'>Email:</td>");
  65.             out.println("<td valign='top'><input type='text' name='email' size='20'></td>");
  66.             out.println("</tr>");
  67.  
  68.             out.println("<tr>");
  69.             out.println("<td valign='top'><input type='submit' value='Зарегистрироваться'></td>");
  70.             out.println("</tr>");
  71.  
  72.             out.println("</table>");
  73.             out.println("</form>");
  74.  
  75.             out.println("</body>");
  76.  
  77.             out.println("</html>");
  78.         } finally {
  79.             out.close();
  80.         }
  81.     }
  82.  
  83.     protected void processResponse(HttpServletRequest request, HttpServletResponse response)
  84.             throws ServletException, IOException {
  85.         Enumeration paramNames = request.getParameterNames();
  86.  
  87.         response.setContentType("text/html;charset=UTF-8");
  88.         request.setCharacterEncoding("UTF-8");
  89.         response.setCharacterEncoding("UTF-8");
  90.         PrintWriter out = response.getWriter();
  91.  
  92.         try {
  93.             /* TODO output your page here. You may use following sample code. */
  94.             out.println("<!doctype html>");
  95.             out.println("<html>");
  96.             out.println("<head>");
  97.             out.println("<title>Registration Form</title>");
  98.             out.println("</head>");
  99.             out.println("<body>");
  100.  
  101.             if (!paramNames.hasMoreElements()) {
  102.                 out.println("<h2>Не было передано ни одного параметра!</h2>");
  103.             } else {
  104.                 out.println("<h2>Были переданы следующие параметры:</h2>");
  105.  
  106.                 while (paramNames.hasMoreElements()) {
  107.                     String parName = (String) paramNames.nextElement();
  108.                     out.println("<strong>" + parName + "</strong> : "
  109.                             + request.getParameter(parName));
  110.                     out.println("<br>");
  111.                 }
  112.             }
  113.  
  114.             out.println("</body>");
  115.             out.println("</html>");
  116.         } finally {
  117.             out.close();
  118.         }
  119.     }
  120.  
  121.     // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
  122.     /**
  123.      * Handles the HTTP
  124.      * <code>GET</code> method.
  125.      *
  126.      * @param request servlet request
  127.      * @param response servlet response
  128.      * @throws ServletException if a servlet-specific error occurs
  129.      * @throws IOException if an I/O error occurs
  130.      */
  131.     @Override
  132.     protected void doGet(HttpServletRequest request, HttpServletResponse response)
  133.             throws ServletException, IOException {
  134.         processRequest(request, response);
  135.     }
  136.  
  137.     /**
  138.      * Handles the HTTP
  139.      * <code>POST</code> method.
  140.      *
  141.      * @param request servlet request
  142.      * @param response servlet response
  143.      * @throws ServletException if a servlet-specific error occurs
  144.      * @throws IOException if an I/O error occurs
  145.      */
  146.     @Override
  147.     protected void doPost(HttpServletRequest request, HttpServletResponse response)
  148.             throws ServletException, IOException {
  149.         processResponse(request, response);
  150.     }
  151.  
  152.     /**
  153.      * Returns a short description of the servlet.
  154.      *
  155.      * @return a String containing servlet description
  156.      */
  157.     @Override
  158.     public String getServletInfo() {
  159.         return "Registration Form";
  160.     }// </editor-fold>
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement