190 likes | 429 Views
Matakuliah : T0053/Web Programming Tahun : 2006 Versi : 2. Pertemuan 11 Aplikasi Web dengan Servlet dan JSP. Learning Outcomes. Pada akhir pertemuan ini, diharapkan mahasiswa akan mampu : Menjelaskan Teknik membangun aplikasi web berbasis Servlet dan JSP yang terhubung ke database
E N D
Matakuliah : T0053/Web Programming Tahun : 2006 Versi : 2 Pertemuan 11Aplikasi Web dengan Servlet dan JSP
Learning Outcomes Pada akhir pertemuan ini, diharapkan mahasiswa akan mampu : • Menjelaskan Teknik membangun aplikasi web berbasis Servlet dan JSP yang terhubung ke database • Membuat aplikasi web yang lebih komplek menggunakan Servlet dan JSP yang terkoneksi ke database • Menggunakan session & cookie
Outline Materi • Aplikasi web dengan Servlet dan JSP • Aplikasi web menggunakan JDBC tingkat lanjut
Codes <TITLE>Guest Book Form www.widodo.com</TITLE> </HEAD> <BODY bgcolor=silver> <font color=blue> <H1 align =center>Guest Book Binus</H1></font> <hr> <FORM ACTION=http://localhost:8084/WebJ2EE /GuestBookServlet METHOD=POST> <PRE> Email address: <INPUT TYPE=text NAME=Email> First Name: <INPUT TYPE=text NAME=FirstName> ….
Servlet import java.io.*;import javax.servlet.*;import javax.servlet.http.*; import java.util.*;import java.sql.*; public class GuestBookServlet extends HttpServlet { private Statement statement = null; private Connection connection = null; private String URL = "jdbc:odbc:Kopi"; public void init( ServletConfig config )throws ServletException { super.init( config ); try { Class.forName( "sun.jdbc.odbc.JdbcOdbcDriver" ); connection = DriverManager.getConnection( URL, "sa", "" ); } catch ( Exception e ) { e.printStackTrace();connection = null; }} public void doPost( HttpServletRequest req,HttpServletResponse res ) throws ServletException, IOException { String email, firstName, lastName, company, ….
Servlet public void doPost( HttpServletRequest req,HttpServletResponse res ) throws ServletException, IOException { String email, firstName, lastName, company, snailmailList, cppList, javaList, vbList, iwwwList; email = req.getParameter( "Email" ); firstName = req.getParameter( "FirstName" ); lastName = req.getParameter( "LastName" ); company = req.getParameter( "Company" ); snailmailList = req.getParameter( "mail" ); cppList = req.getParameter( "c_cpp" ); javaList = req.getParameter( "java" ); vbList = req.getParameter( "vb" ); iwwwList = req.getParameter( "iwww" ); PrintWriter output = res.getWriter(); res.setContentType( "text/html" ); …..
Codes private boolean insertIntoDB( String stringtoinsert ) { try { statement = connection.createStatement(); statement.execute( "INSERT INTO GuestBook values (" + stringtoinsert + ");" ); statement.close(); } catch ( Exception e ) { System.err.println( "ERROR: bermasalah menambahkan data baru" ); e.printStackTrace(); return false; } return true; } public void destroy() { try { connection.close(); } catch( Exception e ) { System.err.println( "bermasalah menutup database" ); } }}
Display Data <font color=blue><h1>Daftar Programmer</h1></font> <table border="1" cellpading="5" cellspacing="0" bgcolor=silver bordercolor=green> <tr><td>No</td><td>nama</td><td> Deskripsi</td><td>Aksi</td></tr> <%@ page import="java.sql.*"%> <% Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con = DriverManager.getConnection ("jdbc:odbc:kopi","sa",“password"); Statement stmt = con.createStatement (); ResultSet rst = stmt.executeQuery ("Select * from programmer");
Display Data while(rst.next()) //baca data { int no=rst.getInt("no"); String nama=rst.getString("nama"); String deskripsi=rst.getString("deskripsi"); out.print ("<tr><td>"+no+"</td>"); out.print ("<td>"+nama+"</td>"); out.print ("<td>"+deskripsi+"</td>"); out.print ("<td><a href=rubahdb.jsp?" + no + ">Rubah Data"); out.print ("<a> | <a href =hapusdb.jsp?" + no + ">Hapus Data</a>"); out.println ("</td></tr>"); } stmt.close(); con.close();
Add Data <%@ page import="java.sql.*" %> <% String nama=request.getParameter ("nama"); String ganti=request.getParameter ("TextGanti"); String sql="insert into programmer (nama,deskripsi) " + "values"('"+ nama + "','" + ganti+ "')"; Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con = DriverManager.getConnection ("jdbc:odbc:kopi","sa","mautauaja"); Statement stmt = con.createStatement (); stmt.executeUpdate(sql); stmt.close(); con.close(); %> <jsp:forward page="programmer.jsp"/>
Edit Data String kunci=request.getQueryString(); String sql= "Select * from programmer where programmer.no = " + kunci; Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con = DriverManager.getConnection ("jdbc:odbc:Trisakti","sa",“password"); Statement stmt = con.createStatement (); ResultSet rs=stmt.executeQuery(sql); rs.next(); %> <form method="POST" action="simpandb.jsp"> <% out.println ("<h3> Silahkan Update data </h3><br>");%> <textarea rows="5" name="nama" cols="22"> <%= rs.getString ("nama") %> </textarea>
Save Data <%@ page import="java.sql.*" %> <% String kunci=request.getParameter ("kunci"); String nama=request.getParameter("nama"); String ganti=request.getParameter ("TextGanti"); String sql ="update programmer set nama='" + nama + "', deskripsi='" + ganti+"' where no="+ kunci; Class.forName ("sun.jdbc.odbc.JdbcOdbcDriver"); Connection con = DriverManager.getConnection ("jdbc:odbc:Trisakti","",""); Statement stmt = con.createStatement (); stmt.executeUpdate(sql); stmt.close(); con.close(); %> <jsp:forward page="programmer.jsp"/>
Session & Cookie When you are working with an application, you open it, do some changes and then you close it. This is much like a Session. The computer knows who you are. It knows when you start the application and when you end. But on the internet there is one problem: the web server does not know who you are and what you do because the HTTP address doesn't maintain state. Java solves this problem by creating a unique cookie for each user. The cookie is sent to the client and it contains information that identifies the user. This interface is called the Session object.
Cookie //buat session session.setAttribute("idk", idk); //baca session String idk=(String)session.getAttribute("idk"); Cookie[] cookies = request.getCookies(); for (int i = 0; i < cookies.length; i++) { Cookie c = cookies[i]; String name = c.getName(); String value = c.getValue(); out.println(name + " = " + value);
References Deithel, “Java How To Program”, 5th ed, 2006 Widodo Budiharto, “Panduan Lengkap Pemrograman J2EE”, Andi Offset Yogyakarta, 2006 www.apache.org www.w3schools.com www.struts.org www.netbeans.org