Form dengan Method GET & POST Menggunakan Servlet

Proses pengiriman parameter juga dapat dilakukan dengan menggunakan form html baik menggunakan get dan post. Untuk mengirim parameter tersebut, dibutuhkan html yang memuat form kemudian mengakses servlet untuk memproses parameter post/ get yang dikirim melalui form tersebut.
Berikut contoh penggunaan form menggunakan method GET pada servlet
file name : get_form_page.html

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Post Form</title>
</head>
<body>
	<form action="GetServlet" method="get">
		Name : <input type="text" name="name"> <br />
		Address : <input type="text" name="address"> <br />
		<input type="submit" value="Send Post">
	</form>
</body>
</html>

file name : “GetServlet.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 GetServlet
 */
@WebServlet("/GetServlet")
public class GetServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public GetServlet() {
        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
		String name = request.getParameter("name");
		String address = request.getParameter("address");
		PrintWriter writer = response.getWriter();
		if( name != null && address != null ){
			writer.println("<h3>Get Response : "+name.concat(" ".concat(address))+"</h3>");
		}
	}

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

}

output :


Untuk menerima parameter, dapat menggunakan request.getParameter dengan output berupa String object.Dapat dilihat pada get method, parameter dikirimkan dan ditampilkan pada url browser.
Selanjutnya, post method akan menyembunyikan parameter yang dikirimkan.
Berikut contoh post method menggunakan servlet :
file name : “post_form_page.html”

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Post Form</title>
</head>
<body>
	<form action="PostServlet" method="post">
		Name : <input type="text" name="name"> <br />
		Address : <input type="text" name="address"> <br />
		<input type="submit" value="Send Post">
	</form>
	
</body>
</html>

file name = “PostServlet.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 PostServlet
 */
@WebServlet(urlPatterns = "/PostServlet")
public class PostServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public PostServlet() {
        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
		PrintWriter in = response.getWriter();
		String name = request.getParameter("name");
		String address = request.getParameter("address");
		in.println("<h3>Your Request is :"+name.concat("-".concat(address))+"</h3>");		
	}

}

output :


Berdasarkan output diatas, dapat dilihat bahwa post method menyembunyikan parameternya dari url.

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: