Form : Basic

student-registration.jsp


<?xml version="1.0" encoding="ISO-8859-1" ?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.0">
	<jsp:directive.page language="java"
		contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" />
	<jsp:text>
		<![CDATA[ <?xml version="1.0" encoding="ISO-8859-1" ?> ]]>
	</jsp:text>
	<jsp:text>
		<![CDATA[ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> ]]>
	</jsp:text>
	<html xmlns="http://www.w3.org/1999/xhtml">
<SCRIPT type="text/javascript">
	function clearFields() {
		document.forms["reg-form"]["name"].value = "";
		document.forms["reg-form"]["age"].value = "";
		document.forms["reg-form"]["gender"].value = "";
		document.forms["reg-form"]["email"].value = "";
	}

	function validateForm() {
		var name = document.forms["reg-form"]["name"].value;
		if (name == null || name == "") {
			alert("Name must be filled out");
			return false;
		}
		return false;
	}
</SCRIPT>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Student Registration Form</title>
</head>
<body>
	<FORM action="student-register.do" method="post" name="reg-form"
		onsubmit="return validateForm()">
		<TABLE border="1" align="center">
			<TR>
				<TD></TD>
				<TD>${message}</TD>
			</TR>
			<TR>
				<TD>Name</TD>
				<TD><INPUT type="text" name="name" /></TD>
			</TR>
			<TR>
				<TD>Age</TD>
				<TD><INPUT type="text" name="age" /></TD>
			</TR>
			<TR>
				<TD>Gender</TD>
				<TD><INPUT type="text" name="gender" /></TD>
			</TR>
			<TR>
				<TD>Email</TD>
				<TD><INPUT type="text" name="email" /></TD>
			</TR>
			<TR>
				<TD></TD>
				<TD><INPUT type="submit" value="Submit" /> <INPUT
					type="button" value="Clear" onclick="clearFields();" /></TD>
			</TR>
		</TABLE>
	</FORM>
</body>
	</html>
</jsp:root>

StudentRegistrationController.java


package jkcs.basic;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.ws.Response;

import org.apache.catalina.connector.Request;

/**
 * Servlet implementation class StudentRegistrationController
 */
public class StudentRegistrationController extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public StudentRegistrationController() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.getWriter().write("S T U D E N T");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String name = request.getParameter("name");
		String age = request.getParameter("age");
		String gender = request.getParameter("gender");
		String email = request.getParameter("email");

		// validation
		boolean validForm = false;
		// name
		if (checkEmpty(name) && checkEmpty(age) && checkEmpty(gender)
				&& checkEmpty(email)) {
			// further validation
			validForm = true;
		} else {
			response.getWriter().write("errors");
			validForm = false;
		}

		RequestDispatcher view = request
				.getRequestDispatcher("student-registration.jsp");
		if (validForm) {
			request.setAttribute("message", "Thank you!");
		} else {
			request.setAttribute("message",
					"Please recheck and submit");
		}

		view.forward(request, response);

	}

	public boolean checkEmpty(String data) {
		if (data.equals("") || data == null) {
			return false;
		}
		// more validation
		return true;
	}

}

Reference

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: