School admission form to accept value from user and returns the form if any of the field is blank.
The value of the fields which are filled already, are retained.
http Address: http://localhost:8080/Excercise/form1.html
web.xml(To be stored in folder called apache tomcat/webapps/Excercise/WEB-INF in webapps):
<servlet>
<servlet-name>SchoolForm</servlet-name>
<servlet-class>Prog.Problem.School_Admission</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>SchoolForm</servlet-name>
<url-pattern>/Admission.do </url-pattern>
</servlet-mapping>
form1.html (School Admission form to be stored in apache tomcat/webapps/Excercise )
<html>
<body>
Fill in the form below for school admission for class I.
<form method="POST" action="Admission.do" >
Child's Name = <input name = "Name"></br>
DOB = <input name ="DOB"></br>
Father name = <input name = "Father"></br>
Residence Address = <input name = "Adress"> </br>
Previous School name = <input name = "School"></br>
<input type="Submit" value="Submit Form" >
<form>
</body>
</html>
School_Admission.java (.class to be stored in folder apache tomcat/webapps/Excercise/WEB-INF/classes/Prog/Problem)
package Prog.Problem;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class School_Admission extends HttpServlet{
public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException{
res.setContentType("text/html");
PrintWriter out = res.getWriter();
if((req.getParameter("Name")=="") || (req.getParameter("DOB")=="") || (req.getParameter("Adress")=="") || (req.getParameter("Father")=="") || (req.getParameter("School")=="") ){
/*Any of above field is unfilled, return the form to user.*/
RequestDispatcher view = req.getRequestDispatcher("Incomplete_Form.jsp");
view.forward(req, res);
}
/*If all fields are filled, show the summary of the form to user. */
else{
out.println("Form filled is </br>");
out.println("Child's Name : " + req.getParameter("Name")+ "</br>");
out.println("DOB : " + req.getParameter("DOB")+ "</br>");
out.println("Father's Name : " + req.getParameter("Father")+ "</br>");
out.println("Address : "+req.getParameter("Adress") + "</br>");
out.println("Previous School Name : " + req.getParameter("School")+ "</br></br>");
out.println("Thank you, we will get in touch asap");
}
}
}
Incomplete_Form.jsp(To be store din apache tomcat/webapps/Excercise)
This form is dispalyed till all the fields are filled by user
<html>
<body>
Enter all the fields:
<form method="POST" action="Admission.do" >
Child's Name = <input name = "Name" value="<%= request.getParameter("Name")%>"/></br>
DOB = <input name ="DOB" value="<%= request.getParameter("DOB")%>"></br>
Father name = <input name = "Father" value="<%= request.getParameter("Father")%>"/></br>
Residence Address = <input name = "Adress" value="<%= request.getParameter("Adress")%>"/> </br>
Previous School name = <input name = "School" value="<%= request.getParameter("School")%>"/></br>
<input type="Submit" value="Submit Form" >
<form>
</html>
</body>