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:
<!-- To change this template, choose Tools | Templates and open the template in the editor. --> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Course Registration</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> </head> <body> <form name="RegForm" action="RegController" method="POST"> <table border="1"> <tbody> <tr> <td>Student ID</td> <td><input type="text" name="txtID" value="" /></td> </tr> <tr> <td>Full Name</td> <td><input type="text" name="txtName" value="" /></td> </tr> <tr> <td>Address</td> <td><input type="text" name="txtAddress" value="" /></td> </tr> <tr> <td>Telephone</td> <td><input type="text" name="txtTelphone" value="" /></td> </tr> <tr> <td>Email</td> <td><input type="text" name="txtEmail" value="" /></td> </tr> <tr> <td><input type="submit" value="Submit" name="btnSubmit" /></td> <td></td> </tr> </tbody> </table> </form> </body> </html>
Servlet:
java Code:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package s2; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * * @author */ @WebServlet(name = "RegistrationControllerServlet", urlPatterns = {"/RegController"}) public class RegistrationControllerServlet extends HttpServlet { /** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods. * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); try { String id = request.getParameter("txtID"); String name = request.getParameter("txtName"); String address = request.getParameter("txtAddress"); String phone = request.getParameter("txtTelphone"); String email = request.getParameter("txtEmail"); out.println("<html>"); out.println("<head>"); out.println("<title>Student Registration Information</title>"); out.println("</head>"); out.println("<body>"); out.println("<h1>Your details are as follows:" + "</h1>"); out.println("<h1>Your ID is " + id + "</h1>"); out.println("<h1>Your name is " + name + "</h1>"); out.println("<h1>Your address is " + address + "</h1>"); out.println("<h1>Your phone is " + phone + "</h1>"); out.println("<h1>Your email is " + email + "</h1>"); out.println("</body>"); out.println("</html>"); } finally { out.close(); } }
This is what I have so far:
java Code:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package s2; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * * @author */ @WebServlet(name = "Student", urlPatterns = {"/S"}) public class Student extends HttpServlet { private String id; private String name; private String address; private String telephone; private String email; public Student() { } public Student(String id, String name, String address, String telephone, String email) { this.id = id; this.name = name; this.address = address; this.telephone = telephone; this.email = email; } /** * @return the id */ public String getId() { return id; } /** * @param id the id to set */ public void setId(String id) { this.id = id; } /** * @return the name */ public String getName() { return name; } /** * @param name the name to set */ public void setName(String name) { this.name = name; } /** * @return the address */ public String getAddress() { return address; } /** * @param address the address to set */ public void setAddress(String address) { this.address = address; } /** * @return the telephone */ public String getTelephone() { return telephone; } /** * @param telephone the telephone to set */ public void setTelephone(String telephone) { this.telephone = telephone; } /** * @return the email */ public String getEmail() { return email; } /** * @param email the email to set */ public void setEmail(String email) { this.email = email; } /** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods. * @param request servlet request * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); try { Student s = new Student(request.getParameter("txtID"), request.getParameter("txtName"), request.getParameter("txtAddress"), request.getParameter("txtTel"), request.getParameter("txtEmail")); out.println("<h3>Your ID: " + s.getId() + "</h3>"); out.println("<h3>Your Name: " + s.getName() + "</h3>"); out.println("<h3>Your Address: " + s.getAddress() + "</h3>"); out.println("<h3>Your Phone: " + s.getTelephone() + "</h3>"); out.println("<h3>Your Email: " + s.getEmail() + "</h3>"); out.close(); } finally { } } }
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


Reply With Quote