Results 1 to 2 of 2

Thread: [RESOLVED] Using a Java Bean to process Form parameters

  1. #1
    Web developer Nightwalker83's Avatar
    Join Date
    Dec 01
    Location
    Adelaide, Australia
    Posts
    9,723

    Resolved [RESOLVED] Using a Java Bean to process Form parameters

    Hi,

    I need to modify the code below so that the form posts the information to the student class then the student class returns the data.

    html Code:
    1. <!--
    2. To change this template, choose Tools | Templates
    3. and open the template in the editor.
    4. -->
    5. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    6. <html>
    7.     <head>
    8.         <title>Course Registration</title>
    9.         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    10.     </head>
    11.     <body>
    12.         <form name="RegForm" action="RegController" method="POST">
    13.             <table border="1">
    14.                 <tbody>
    15.                     <tr>
    16.                         <td>Student ID</td>
    17.                         <td><input type="text" name="txtID" value="" /></td>
    18.                     </tr>
    19.                     <tr>
    20.                         <td>Full Name</td>
    21.                         <td><input type="text" name="txtName" value="" /></td>
    22.                     </tr>
    23.                     <tr>
    24.                         <td>Address</td>
    25.                         <td><input type="text" name="txtAddress" value="" /></td>
    26.                     </tr>
    27.                     <tr>
    28.                         <td>Telephone</td>
    29.                         <td><input type="text" name="txtTelphone" value="" /></td>
    30.                     </tr>
    31.                     <tr>
    32.                         <td>Email</td>
    33.                         <td><input type="text" name="txtEmail" value="" /></td>
    34.                     </tr>
    35.                     <tr>
    36.                         <td><input type="submit" value="Submit" name="btnSubmit" /></td>
    37.                         <td></td>
    38.                     </tr>
    39.                 </tbody>
    40.             </table>
    41.         </form>
    42.     </body>
    43. </html>

    Servlet:

    java Code:
    1. /*
    2.  * To change this template, choose Tools | Templates
    3.  * and open the template in the editor.
    4.  */
    5. package s2;
    6.  
    7. import java.io.IOException;
    8. import java.io.PrintWriter;
    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. /**
    16.  *
    17.  * @author
    18.  */
    19. @WebServlet(name = "RegistrationControllerServlet", urlPatterns = {"/RegController"})
    20. public class RegistrationControllerServlet extends HttpServlet {
    21.  
    22.     /**
    23.      * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
    24.      * @param request servlet request
    25.      * @param response servlet response
    26.      * @throws ServletException if a servlet-specific error occurs
    27.      * @throws IOException if an I/O error occurs
    28.      */
    29.     protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    30.             throws ServletException, IOException {
    31.         response.setContentType("text/html;charset=UTF-8");
    32.         PrintWriter out = response.getWriter();
    33.         try {
    34.             String id = request.getParameter("txtID");
    35.             String name = request.getParameter("txtName");
    36.             String address = request.getParameter("txtAddress");
    37.             String phone = request.getParameter("txtTelphone");
    38.             String email = request.getParameter("txtEmail");
    39.             out.println("<html>");
    40.             out.println("<head>");
    41.             out.println("<title>Student Registration Information</title>");
    42.             out.println("</head>");
    43.             out.println("<body>");
    44.             out.println("<h1>Your details are as follows:" + "</h1>");
    45.             out.println("<h1>Your ID is " + id + "</h1>");
    46.             out.println("<h1>Your name is " + name + "</h1>");
    47.             out.println("<h1>Your address is " + address + "</h1>");
    48.             out.println("<h1>Your phone is " + phone + "</h1>");
    49.             out.println("<h1>Your email is " + email + "</h1>");
    50.             out.println("</body>");
    51.             out.println("</html>");
    52.         } finally {
    53.             out.close();
    54.         }
    55.     }

    This is what I have so far:

    java Code:
    1. /*
    2.  * To change this template, choose Tools | Templates
    3.  * and open the template in the editor.
    4.  */
    5. package s2;
    6.  
    7. import java.io.IOException;
    8. import java.io.PrintWriter;
    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. /**
    16.  *
    17.  * @author
    18.  */
    19. @WebServlet(name = "Student", urlPatterns = {"/S"})
    20. public class Student extends HttpServlet {
    21.  
    22.     private String id;
    23.     private String name;
    24.     private String address;
    25.     private String telephone;
    26.     private String email;
    27.     public Student() {
    28.     }
    29.  
    30.     public Student(String id, String name, String address, String telephone, String email) {
    31.         this.id = id;
    32.         this.name = name;
    33.         this.address = address;
    34.         this.telephone = telephone;
    35.         this.email = email;
    36.     }
    37.  
    38.     /**
    39.      * @return the id
    40.      */
    41.     public String getId() {
    42.         return id;
    43.     }
    44.  
    45.     /**
    46.      * @param id the id to set
    47.      */
    48.     public void setId(String id) {
    49.         this.id = id;
    50.     }
    51.  
    52.     /**
    53.      * @return the name
    54.      */
    55.     public String getName() {
    56.         return name;
    57.     }
    58.  
    59.     /**
    60.      * @param name the name to set
    61.      */
    62.     public void setName(String name) {
    63.         this.name = name;
    64.     }
    65.  
    66.     /**
    67.      * @return the address
    68.      */
    69.     public String getAddress() {
    70.         return address;
    71.     }
    72.  
    73.     /**
    74.      * @param address the address to set
    75.      */
    76.     public void setAddress(String address) {
    77.         this.address = address;
    78.     }
    79.  
    80.     /**
    81.      * @return the telephone
    82.      */
    83.     public String getTelephone() {
    84.         return telephone;
    85.     }
    86.  
    87.     /**
    88.      * @param telephone the telephone to set
    89.      */
    90.     public void setTelephone(String telephone) {
    91.         this.telephone = telephone;
    92.     }
    93.  
    94.     /**
    95.      * @return the email
    96.      */
    97.     public String getEmail() {
    98.         return email;
    99.     }
    100.  
    101.     /**
    102.      * @param email the email to set
    103.      */
    104.     public void setEmail(String email) {
    105.         this.email = email;
    106.     }
    107.     /**
    108.      * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
    109.      * @param request servlet request
    110.      * @param response servlet response
    111.      * @throws ServletException if a servlet-specific error occurs
    112.      * @throws IOException if an I/O error occurs
    113.      */
    114.     protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    115.             throws ServletException, IOException {
    116.         response.setContentType("text/html;charset=UTF-8");
    117.         PrintWriter out = response.getWriter();
    118.         try {
    119.             Student s = new Student(request.getParameter("txtID"), request.getParameter("txtName"), request.getParameter("txtAddress"),
    120.                     request.getParameter("txtTel"), request.getParameter("txtEmail"));
    121.             out.println("<h3>Your ID: " + s.getId() + "</h3>");
    122.               out.println("<h3>Your Name: " + s.getName() + "</h3>");
    123.                out.println("<h3>Your Address: " + s.getAddress() + "</h3>");
    124.                out.println("<h3>Your Phone: " + s.getTelephone() + "</h3>");
    125.                out.println("<h3>Your Email: " + s.getEmail() + "</h3>");
    126.             out.close();
    127.         } finally {
    128.         }
    129.     }
    130. }

    However, if I try to submit the information when the project is running I receive:

    HTTP Status 405 - HTTP method POST is not supported by this URL

    type Status report

    messageHTTP method POST is not supported by this URL

    descriptionThe specified HTTP method is not allowed for the requested resource (HTTP method POST is not supported by this URL).
    GlassFish Server Open Source Edition 3.0.1

    I don't think I am doing this correctly?


    Nightwalker
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    Please consider giving me some rep points if I help you a lot.
    DON'T BUMP YOUR POSTS!!! Links to my code examples can now be found on my website: My websites
    Please rate my post if you find it helpful!
    Technology is a dangerous thing in the hands of an idiot! I am that idiot.

  2. #2
    Web developer Nightwalker83's Avatar
    Join Date
    Dec 01
    Location
    Adelaide, Australia
    Posts
    9,723

    Re: Using a Java Bean to process Form parameters

    I replaced this code in the servlet and it works.

    java Code:
    1. // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    2.     /**
    3.      * Handles the HTTP <code>GET</code> method.
    4.      * @param request servlet request
    5.      * @param response servlet response
    6.      * @throws ServletException if a servlet-specific error occurs
    7.      * @throws IOException if an I/O error occurs
    8.      */
    9.     @Override
    10.     protected void doGet(HttpServletRequest request, HttpServletResponse response)
    11.             throws ServletException, IOException {
    12.         processRequest(request, response);
    13.     }
    14.  
    15.     /**
    16.      * Handles the HTTP <code>POST</code> method.
    17.      * @param request servlet request
    18.      * @param response servlet response
    19.      * @throws ServletException if a servlet-specific error occurs
    20.      * @throws IOException if an I/O error occurs
    21.      */
    22.     @Override
    23.     protected void doPost(HttpServletRequest request, HttpServletResponse response)
    24.             throws ServletException, IOException {
    25.         processRequest(request, response);
    26.     }
    27.  
    28.     /**
    29.      * Returns a short description of the servlet.
    30.      * @return a String containing servlet description
    31.      */
    32.     @Override
    33.     public String getServletInfo() {
    34.         return "Short description";
    35.     }// </editor-fold>
    If this thread is finished with please mark it "Resolved" by selecting "Mark thread resolved" from the "Thread tools" drop-down menu.
    Please consider giving me some rep points if I help you a lot.
    DON'T BUMP YOUR POSTS!!! Links to my code examples can now be found on my website: My websites
    Please rate my post if you find it helpful!
    Technology is a dangerous thing in the hands of an idiot! I am that idiot.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •