Di sini, di posting ini kita akan melihat aplikasi web sederhana menggunakan Servlet dan JSP di mana kita akan memberikan rincian pelanggan di halaman JSP kemudian mendapatkan rincian pelanggan di servlet dan menunjukkan rincian pelanggan yang dimasukkan di halaman Selamat Datang.
Environment yang digunakan:
- Eclipse 3.3
- JDK6
- Tomcat Servler 6.x
1. Buat "Dynamic Web Project" dengan versi modul web dinamis - 2.5 Kemudian Klik Selesai. Struktur aplikasi web dalam eclipse akan terlihat seperti ini.

2. Buat halaman Customer.jsp di dalam folder WebContent struktur aplikasi web.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Customer Details</title>
</head>
<body>
<form action="insertCustomer" method="post">
<table align="center" bgcolor="#99FFCC" border="1" width="70%">
<tr>
<td colspan="2" align="center">Customer Details </td>
</tr>
<tr>
<td>Name </td>
<td><input type="text" name="name" maxlength="25"></td>
</tr>
<tr>
<td>Address </td>
<td><input type="text" name="address" maxlength="40"></td>
</tr>
<tr>
<td>Mobile </td>
<td><input type="text" name="mobile" maxlength="10"></td>
</tr>
<tr>
<td>EmailId </td>
<td><input type="text" name="emailid" maxlength="30"></td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="Submit"></td>
</tr>
</table>
</form>
</body>
</html>
Halaman pelanggan ini akan terlihat seperti di bawah ini.

3. Buat kelas servlet InsertCustomerServlet.java. Saat mengirimkan formulir dari halaman "Customer.jsp", servlet akan mendapatkan nilai formulir dalam objek HttpServletRequest.
package com.room.sample.servlet;
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 com.room.sample.view.Customer;
public class InsertCustomerServlet extends HttpServlet{
private static final long serialVersionUID = 1L;
public void doPost(HttpServletRequest request, HttpServletResponse response){
System.out.println("----- InsertCustomerServlet -----");
try {
// Get the customer value submitted from Customer.jsp page through HttpServletRequest object
String name=request.getParameter("name");
String address=request.getParameter("address");
String mobile=request.getParameter("mobile");
String emailid=request.getParameter("emailid");
//Set the Customer values into Customer Bean or POJO(Plain Old Java Object) class
Customer customer=new Customer();
customer.setName(name);
customer.setAddress(address);
customer.setMobile(Long.valueOf(mobile));
customer.setEmailid(emailid);
RequestDispatcher dispatcher=request.getRequestDispatcher("/Welcome.jsp");
//Set the customer instance into request.Then only the customer object
//will be available in the Welcome.jsp page
request.setAttribute("cust",customer);
dispatcher.forward(request, response);
} catch (ServletException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}
}
}
4. Bean Class untuk menyimpan informasi yang diterima di kelas Servlet.
package com.room.sample.view;
import java.io.Serializable;
public class Customer implements Serializable{
private static final long serialVersionUID = 1L;
private String name;
private String address;
private Long mobile;
private String emailid;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Long getMobile() {
return mobile;
}
public void setMobile(Long mobile) {
this.mobile = mobile;
}
public String getEmailid() {
return emailid;
}
public void setEmailid(String emailid) {
this.emailid = emailid;
}
}
5. Buat halaman Welcome.jsp untuk menampilkan detail pelanggan yang diproses.
<%@page import="com.room.sample.view.Customer"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>View Customer Details</title>
</head>
<body>
<%
Customer customer=(Customer)request.getAttribute("cust");
%>
<table align="center" bgcolor="#FFFFCC" border="1" width="70%">
<tr>
<td colspan="2" align="center"><%="Welcome "+customer.getName()+" !!!!. Your details Processed." %></td>
</tr>
<tr>
<td>Name </td>
<td><%=customer.getName()%></td>
</tr>
<tr>
<td>Address </td>
<td><%=customer.getAddress() %></td>
</tr>
<tr>
<td>Mobile </td>
<td><%=String.valueOf(customer.getMobile()) %></td>
</tr>
<tr>
<td>EmailId </td>
<td><%=customer.getEmailid() %></td>
</tr>
</table>
</body>
</html>
6. Hubungkan aliran data menggunakan web.xml. Untuk pendefinisian servlet harus memperhatikan nilai elemen di bawah elemen dan nilai elemen di bawah harus sama. Maka hanya request "insertCustomer" yang dapat menemukan mana servlet yang sesuai untuk dieksekusi dari file web.xml.
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>Simple Customer Application</display-name>
<welcome-file-list>
<welcome-file>Customer.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>customer</servlet-name>
<servlet-class>com.room.sample.servlet.InsertCustomerServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>customer</servlet-name>
<url-pattern>/insertCustomer</url-pattern>
</servlet-mapping>
</web-app>
Halaman Welcome.jsp akan terlihat seperti di bawah ini.
