Welcome to Abdul Malik Ikhsan's Blog

Implementasi MVC mggnkn JSTL

Posted in Tutorial JSP, Tutorial Servlet by samsonasik on April 16, 2008

Persiapan :
1. satu gelas kopi.
2. Tomcat yang sudah terkonfigurasi.
3. Copykan library JSTL yaitu standard.jar dan jstl.jar dari tomcat\webapps\examples\WEB-INF\lib ke tomcat\webapps\ROOT\WEB-INF\lib
4. Restart Tomcatnya
5. Let’s Coding…
———-
Siapkan kelas Modelnya (contoh, simpan di WEB-INF\classes\MVCModel)

Code:
package MVCModel;
import java.sql.*;

public class OpenConnect{
private String driver=”com.mysql.jdbc.Driver”;
private Connection con;
private Statement state;
private ResultSet rs;
//konstruktor…
public OpenConnect() throws ClassNotFoundException, SQLException{
Class.forName(driver);
con=DriverManager.getConnection(“jdbc:mysql://localhost/belajarMVC”, “root”,””);
state = con.createStatement();
}
public boolean strSQL(String sql) throws SQLException{
rs = state.executeQuery(sql);
return (rs!=null);
}
public boolean strNext() throws SQLException{
return rs.next();
}
public String getField(String field) throws SQLException{
return rs.getString(field);
}
public void lastrecord() throws SQLException{
rs.last();
}
public int getCount() throws SQLException{
return rs.getRow();
}
public void firstrecord()throws SQLException{
rs.beforeFirst();
}
}

—>compile deh

Selanjutnya, kita buat kelas Controlnya(Contoh, simpan di WEB-INF\classes\MVCControl)

Code:
package MVCControl;
import MVCModel.OpenConnect;

import java.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;

public class Controller extends HttpServlet{
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
try{
OpenConnect open = new OpenConnect();
open.strSQL(“select *  from buku”);
//letakkan cursor pada akhir record
open.lastrecord();
//dpt jumlah record
int jml = open.getCount();
//letakkan cursor kembali pada awal record
open.firstrecord();

//set atribut jumlah dengan variable jml
request.setAttribute(“jumlah”,jml);

//deklarasi array field2 tabel
String[] field1 = new String[jml];
String[] field2 = new String[jml];

for (int i=0;i<=jml ;i++ )
{
//next ResultSet
open.strNext();

//isi array ke i dengan field2 yang ada…
field1[i] = open.getField(“kodebuku”);
field2[i] = open.getField(“namabuku”);

//isi atribut…
request.setAttribute(“kodenya”,field1);
request.setAttribute(“namanya”,field2);

}
}catch(Exception e){    }
request.getRequestDispatcher(“MVCView/view.jsp”).forward(request,response);
}
}

–>compile deh
Di sini saya contohkan menggunakan database mysql, ada tabel buku dengan field2nya kodebuku dan namabuku.
————
Nah, sekarang, tingggal ditampilkan di View

Code:
<%@ taglib prefix=”c” uri=”http://java.sun.com/jsp/jstl/core&#8221; %>
<table border=”1″>
<tr>
<td>No.</td>
<td>Kode Buku</td>
<td>Nama Buku </td>
</tr>
<c:forEach var=”i” begin=”1″ end=”${jumlah}”>
<tr>
<td>${i}</td>
<td>${kodenya[i-1]} </td>
<td>${namanya[i-1]}</td>
</tr>
</c:forEach>
</table>


Oh, ya, jangan lupa setting DD (Deployment Descriptor-nya) di web-xml, –>tambahkan

Code:
<servlet>
<servlet-name>controller</servlet-name>
<servlet-class>MVCControl.Controller</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>controller</servlet-name>
<url-pattern>/mvc.view</url-pattern>
</servlet-mapping>


—————–
Tinggal testing…

Quote:
http://localhost:8080/mvc.view
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: