Pertemuan 11 Aplikasi Web dengan Servlet dan JSP Matakuliah : T0053/Web Programming

advertisement

Matakuliah

Tahun

Versi

: T0053/Web Programming

: 2006

: 2

Pertemuan 11

Aplikasi Web dengan Servlet dan JSP

1

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

2

Outline Materi

• Aplikasi web dengan Servlet dan JSP

• Aplikasi web menggunakan JDBC tingkat lanjut

3

Advanced Servlet

4

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>

….

5

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,

….

6

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

7

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()

{ catch( Exception e ) {

System.err.println( "bermasalah menutup database" );

}

} try { connection.close();

}}

8

Sample Web

9

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

10

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

11

Add Data

12

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

13

Edit Data

14

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>

15

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

16

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.

17

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

18

References

Deithel, “Java How To Program”, 5 th ed,

2006

Widodo Budiharto, “Panduan Lengkap

Pemrograman J2EE”, Andi Offset

Yogyakarta, 2006

www.apache.org

www.w3schools.com

www.struts.org

www.netbeans.org

19

Download