1 / 19

Pertemuan 11 Aplikasi Web dengan Servlet dan JSP

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

amaris
Download Presentation

Pertemuan 11 Aplikasi Web dengan Servlet dan JSP

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Matakuliah : T0053/Web Programming Tahun : 2006 Versi : 2 Pertemuan 11Aplikasi Web dengan Servlet dan JSP

  2. 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

  3. Outline Materi • Aplikasi web dengan Servlet dan JSP • Aplikasi web menggunakan JDBC tingkat lanjut

  4. Advanced Servlet

  5. 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> ….

  6. 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, ….

  7. 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" ); …..

  8. 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" ); } }}

  9. Sample Web

  10. 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");

  11. 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();

  12. Add Data

  13. 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"/>

  14. Edit Data

  15. 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>

  16. 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"/>

  17. 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.

  18. 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);

  19. 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

More Related