Contoh Java Web Program Servlet Membuat Form Registrasi
SERVLET
Servlet merupakan dasar dari teknologi java web, yaitu
sebuah class java yang memiliki kemampuan sebagai server. Servlet bekerja
berdasarkan model request-respone, dan dasar dari teknologi bersama JSP,
Seperti dilihat dari J2EE, Servlet termasuk komponen web container. Siklus hidup
sevlet terdiri atas 3 tahap , yaitu init(), service(), dan destroy()
Tahap init merupakan tahap awal dimana init() dipanggil web
container setelah servlet
diinisialisasi, kemudian web container memanggil metode servlet(). Pada tahap
ini servlet siap menunggu request untuk melakukan proses mengirim respon ke
client. Dan terakhir metode destroy , merupakan metode yang dipanggil , ketika
web container dimatikan
Membuat Form Registrasi
1. Buka netbean dan perhatikan gambar tutorialnya
2
3
4
5
6
7
8
9
10. Masukkan perintah berikut
11
12
13
14. Masukkan perintah berikut
15
16
17
18
19
8
2
3
4
5
6
7
8
9
10. Masukkan perintah berikut
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class datatampil extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
String nama = request.getParameter("nama");
String email = request.getParameter("email");
String jenismember = request.getParameter("jenismember");
String jenisakses = request.getParameter("jenisakses");
PrintWriter out = response.getWriter();
try {
out.println("<html>");
out.println("<head>");
out.println("<title>Form Registration</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Member Baru</h1>");
out.println("<table border='1' cellpadding='4' cellspacing='2'>" +
"<tr><td>Nama: </td><td>" + nama + "</td></tr>" +
"<tr><td>Email: </td><td>" + email + "</td></tr>" +
"<tr><td>Jenis Member: </td><td>" + jenismember + "</td></tr>" +
"<tr><td>Pilihan Akses: </td><td>" + jenisakses + "</td></tr>" +
"</table>");
out.println("</body>");
out.println("</html>");
} finally {
out.close();
}
}}
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class datatampil extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
String nama = request.getParameter("nama");
String email = request.getParameter("email");
String jenismember = request.getParameter("jenismember");
String jenisakses = request.getParameter("jenisakses");
PrintWriter out = response.getWriter();
try {
out.println("<html>");
out.println("<head>");
out.println("<title>Form Registration</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Member Baru</h1>");
out.println("<table border='1' cellpadding='4' cellspacing='2'>" +
"<tr><td>Nama: </td><td>" + nama + "</td></tr>" +
"<tr><td>Email: </td><td>" + email + "</td></tr>" +
"<tr><td>Jenis Member: </td><td>" + jenismember + "</td></tr>" +
"<tr><td>Pilihan Akses: </td><td>" + jenisakses + "</td></tr>" +
"</table>");
out.println("</body>");
out.println("</html>");
} finally {
out.close();
}
}}
11
12
13
14. Masukkan perintah berikut
package servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class datatampil extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
String nama = request.getParameter("nama");
String email = request.getParameter("email");
String jenismember = request.getParameter("jenismember");
String jenisakses = request.getParameter("jenisakses");
PrintWriter out = response.getWriter();
try {
out.println("<html>");
out.println("<head>");
out.println("<title>Form Registration</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Member Baru</h1>");
out.println("<table border='1' cellpadding='4' cellspacing='2'>" +
"<tr><td>Nama: </td><td>" + nama + "</td></tr>" +
"<tr><td>Email: </td><td>" + email + "</td></tr>" +
"<tr><td>Jenis Member: </td><td>" + jenismember + "</td></tr>" +
"<tr><td>Pilihan Akses: </td><td>" + jenisakses + "</td></tr>" +
"</table>");
out.println("</body>");
out.println("</html>");
} finally {
out.close();
}
}}
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class datatampil extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
String nama = request.getParameter("nama");
String email = request.getParameter("email");
String jenismember = request.getParameter("jenismember");
String jenisakses = request.getParameter("jenisakses");
PrintWriter out = response.getWriter();
try {
out.println("<html>");
out.println("<head>");
out.println("<title>Form Registration</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Member Baru</h1>");
out.println("<table border='1' cellpadding='4' cellspacing='2'>" +
"<tr><td>Nama: </td><td>" + nama + "</td></tr>" +
"<tr><td>Email: </td><td>" + email + "</td></tr>" +
"<tr><td>Jenis Member: </td><td>" + jenismember + "</td></tr>" +
"<tr><td>Pilihan Akses: </td><td>" + jenisakses + "</td></tr>" +
"</table>");
out.println("</body>");
out.println("</html>");
} finally {
out.close();
}
}}
15
16
17
18
19
8
bang bagi codingan
ReplyDeleteweb.xml