Menggunakan HTML Tingkat Lanjut dengan Servlet

Penggunaan form kompleks dalam HTML, dapat ditangani dengan servlet sama seperti multiple parameter baik itu berupa method POST ataupun GET. Berikut contoh form dengan HTML tingkat lanjut.
more_html_form.html

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>More html form</title>
</head>
<body>
	<form action="MoreHtml" method="post">
		Name : <input type="text" name="name">
		<br />
		Gender : 
		<input type="radio" name="gender" value="male">Male</input>
		<input type="radio" name="gender" value="female">Female</input>
		<br />
		Religion : 
		<select name="religion">
			<option value="islam">Islam</option>
			<option value="kristen">Christian</option>
			<option value="katolik">Protestant Christian</option>
			<option value="hindu">Hindu</option>
			<option value="budha">Buddha</option>
		</select>
		<br />
		<input type="submit" value="Send">
	</form>
</body>
</html>

MoreHtml.java

package zisal.com.simpleweb;

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;

/**
 * Servlet implementation class MoreHtml
 */
@WebServlet("/MoreHtml")
public class MoreHtml extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public MoreHtml() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String name = request.getParameter("name");
		String gender = request.getParameter("gender");
		String religion = request.getParameter("religion");
		
		if( name != null && gender != null && religion != null ){
			PrintWriter writer = response.getWriter();
			writer.println("Your Name is ".
					concat(name).
					concat(" ").
					concat("You are a ").
					concat(gender).concat(" ").
					concat("and your faith is ").
					concat(religion));
		}
		
		
	}

}

output :

Tagged: , , , , ,

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: