JSP (Java Server Page)
Web Program
Ketika dijalankan di browser
Kode Program (listing kode) nya :
Form_ Barang.jsp
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0
Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@page contentType="text/html"
pageEncoding="UTF-8"%>
<%@ page
language="java" import="java.sql.*" import=
"java.sql.Connection.*"import= "java.sql.DriverManager.*"
import= "java.sql.ResultSet.*"
import=" java.sql.Statement.*"
errorPage="" %>
<%@ page
import="java.util.*" %>
<%@ page
import="java.io.*" import="java.lang.Exception" %>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type"
content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style type="text/css">
<!--
#apDiv1 {
position:absolute;
width:900px;
height:675px;
z-index:1;
left:
4px;
top:
2px;
}
#apDiv2 {
position:absolute;
width:309px;
height:247px;
left:
46px;
top:
82px;
}
#l1 {
position:absolute;
left:
15px;
}
#t1 {
position:absolute;
left:
116px;
top:
-1px;
}
#t2 {
position:absolute;
left:
116px;
top:
34px;
}
#t3 {
position:absolute;
left:
116px;
top:
73px;
}
#t4 {
position:absolute;
left:
116px;
top:
110px;
}
#t5 {
position:absolute;
left:
116px;
top:
148px;
}
#t6 {
position:absolute;
left:
116px;
top:
179px;
}
#pf {
position:absolute;
left:
162px;
top:
280px;
}
#bf {
position:absolute;
left:
400px;
top:
280px;
}
#t7 {
position:absolute;
left:
30px;
top:
37px;
}
#t8 {
position:absolute;
left:
24px;
top:
37px;
}
#b1 {
position:absolute;
left:
20px;
top:
10px;
width:
54px;
}
#b2 {
position:absolute;
left:
89px;
top:
10px;
width:
54px;
}
#b3 {
position:absolute;
left:
156px;
top:
10px;
width:
52px;
}
#b4 {
position:absolute;
left:
222px;
top:
10px;
width:
53px;
}
#b5 {
position:absolute;
left: 22px;
top:
48px;
width:
51px;
}
#b6 {
position:absolute;
left:
89px;
top:
48px;
width:
53px;
}
#b7 {
position:absolute;
left:
159px;
top:
48px;
width:
49px;
}
#b8 {
position:absolute;
left:
223px;
top:
48px;
width:
51px;
}
#b9 {
position:absolute;
left:
365px;
top:
48px;
color:
black;
border:
1px solid black;
background:
whitesmoke;
}
#b10 {
position:absolute;
left:
364px;
top:
12px;
color:
black;
border:
1px solid black;
background:
whitesmoke;
}
#b11 {
position:absolute;
left:
30px;
top:
76px;
width:
54px;
}
#b12 {
position:absolute;
left:
100px;
top:
76px;
}
#b13 {
position:absolute;
left:
24px;
top:
74px;
width:
57px;
}
#b14 {
position:absolute;
left:
90px;
top:
74px;
}
#b15 {
position:absolute;
left:
474px;
top:
12px;
color:
black;
border:
1px solid black;
background:
whitesmoke;
width: 100px;
}
#bc {
color:
black;
border:
1px solid black;
background:
whitesmoke;
}
#im1 {
position:absolute;
left:
0px;
top:
202px;
width:
96px;
height:
96px;
border:
2px solid silver;
}
#tr1 {
background-color:
#99CCFF;
border-style:
solid;
}
#apDiv3 {
position:absolute;
width:179px;
height:26px;
z-index:2;
left:
367px;
top:
18px;
}
.style1 {
font-size:
24px;
font-weight:
bold;
}
#apDiv4 {
position:absolute;
left:40px;
top:340px;
width:443px;
height:80px;
z-index:2;
}
#apDiv5 {
position:absolute;
width:200px;
height:120px;
z-index:3;
left:
460px;
top:
80px;
background-color:
#99CCFF;
border:
2px solid silver;
}
#apDiv6 {
position:absolute;
left:685px;
top:80px;
width:190px;
height:120px;
z-index:3;
background-color:
#99CCFF;
border:
2px solid silver;
}
.style4 {font-size: 14px}
#apDiv7 {
position:absolute;
left:47px;
top:438px;
width:700px;
height:132px;
z-index:4;
}
#apDiv8 {
position:absolute;
left:126px;
top:310px;
width:95px;
height:24px;
z-index:5;
}
#apDiv9 {
position:absolute;
left:223px;
top:310px;
width:29px;
height:24px;
z-index:6;
}
#apDiv10 {
position:absolute;
left:254px;
top:310px;
width:88px;
height:24px;
z-index:7;
}
#apDiv11 {
position:absolute;
left:750px;
top:236px;
width:141px;
height:358px;
z-index:9;
}
#apDiv12 {
position:absolute;
left:50px;
top:420px;
}
-->
-->
</style>
</head>
<body>
<div id='apDiv1'>
<%
String
saveFile="";
//----------------------------------------------------------
//to get the
content type information from JSP Request Header
String
contentType = request.getContentType();
//here
we are checking the content type is not equal to Null an
if
((contentType != null) &&
(contentType.indexOf("multipart/form-data") >= 0)) {
//if
((contentType == "image/jpeg") &&
(contentType.indexOf("multipart/form-data") >= 0)) {
DataInputStream
in = new DataInputStream(request.
getInputStream());
//we
are taking the length of Content type data
int
formDataLength = request.getContentLength();
byte
dataBytes[] = new byte[formDataLength];
int
byteRead = 0;
int
totalBytesRead = 0;
//this
loop converting the uploaded file into byte code
while
(totalBytesRead < formDataLength) {
byteRead
= in.read(dataBytes, totalBytesRead,
formDataLength);
totalBytesRead
+= byteRead;
}
String
file = new String(dataBytes);
//for
saving the file name
saveFile
= file.substring(file.indexOf("filename=\"") + 10);
saveFile
= saveFile.substring(0, saveFile.indexOf("\n"));
saveFile
= saveFile.substring(saveFile.lastIndexOf("\\")
+
1,saveFile.indexOf("\""));
int
lastIndex = contentType.lastIndexOf("=");
String
boundary = contentType.substring(lastIndex + 1,
contentType.length());
int
pos;
//extracting
the index of file
pos
= file.indexOf("filename=\"");
pos
= file.indexOf("\n", pos) + 1;
pos
= file.indexOf("\n", pos) + 1;
pos
= file.indexOf("\n", pos) + 1;
int
boundaryLocation = file.indexOf(boundary, pos) - 4;
int
startPos = ((file.substring(0, pos)).getBytes()).length;
int
endPos = ((file.substring(0, boundaryLocation))
.getBytes()).length;
//
creating a new file with the same name and writing the content in new file
FileOutputStream
fileOut = new FileOutputStream("C:/Documents and Settings/agus/My
Documents/NetBeansProjects/WebApplicationPtun/web/upload/"+saveFile);
fileOut.write(dataBytes,
startPos, (endPos - startPos));
fileOut.flush();
fileOut.close();
%><table
border="0"><tr><td>File <% out.println(saveFile);
%> telah sukses di upload dan sekarang tinggal add(tambah) recordnya !
</td></tr><tr><td>Atau
file <% out.println(saveFile); %> hapalkan bila akan di edit dan masukan
</td></tr><tr><td>ke field <b>Picture
Web</b> record yang diedit !</td></tr></table> <%
}
//-----------------------------------------------------------
int count=0;
String ppf=request.getParameter("hpf");
if (ppf==null){
ppf="0";
}
int psf=Integer.parseInt(ppf);
String pesanfilter="";
if (psf==-5){
pesanfilter="Data Dalam keadaan sedang di filter
!";
}
if (psf==0){
pesanfilter="";
}
String tt1,tt2,tt3,tt4,tt5,tt6,tk1;
tt1="";
tt2="";
tt3="";
tt4="";
tt5="";
tt6="";
String pimage="";
String countstring="";
countstring=request.getParameter("h2");
if (countstring==null){
countstring="1";
}
String perintah=request.getParameter("hp");
if (perintah==null){
perintah="0";
}
int p=Integer.parseInt(perintah);
//------------------------------------------
String tk7="";
tk7=request.getParameter("t7");
if (tk7==null){
tk7="";
}
String tk8=request.getParameter("t8");
if (tk8==null){
tk8="";
}
//-------------------------------------------
if (p==-2){
tt1=request.getParameter("t1");
tt2=request.getParameter("t2");
tt3=request.getParameter("t3");
tt4=request.getParameter("t4");
tt5=request.getParameter("t5");
tt6=request.getParameter("th6");
try{
Class.forName("com.mysql.jdbc.Driver");
Connection connection =
DriverManager.getConnection("jdbc:mysql://localhost:3306/ptun",
"root", "awsm");
if
(tt6=="" ){
tt6=request.getParameter("t6");
File
imgFile= new File("C:/Documents and Settings/agus/My
Documents/NetBeansProjects/WebApplicationPtun/web/kosong.jpeg");
FileInputStream fin =new FileInputStream(imgFile);
PreparedStatement pre=connection.prepareStatement("INSERT into
brgptun(kbrgptun,nmbrgptun,typebrgptun,pictfilebrgptun,vabrgptun,pictfilewebbrgptun,pictbrgptun)VALUES(?,?,?,?,?,?,?)");
pre.setString(1, tt1);
pre.setString(2, tt2);
pre.setString(3, tt3);
pre.setString(4, tt4);
pre.setBinaryStream(7,fin,(long)imgFile.length());
pre.setString(5, tt5);
pre.setString(6, tt6);
pre.executeUpdate();
pre.close();
connection.close();
}else
if (tt6!="" ){
File
imgFile= new File("C:/Documents and Settings/agus/My
Documents/NetBeansProjects/WebApplicationPtun/web/upload/"+tt6);
FileInputStream fin =new FileInputStream(imgFile);
PreparedStatement pre=connection.prepareStatement("INSERT into
brgptun(kbrgptun,nmbrgptun,typebrgptun,pictfilebrgptun,vabrgptun,pictfilewebbrgptun,pictbrgptun)VALUES(?,?,?,?,?,?,?)");
pre.setString(1, tt1);
pre.setString(2, tt2);
pre.setString(3, tt3);
pre.setString(4, tt4);
pre.setBinaryStream(7,fin,(long)imgFile.length());
pre.setString(5, tt5);
pre.setString(6, tt6);
pre.executeUpdate();
pre.close();
connection.close();
}
}
catch (Exception ex){
}
p=-6;
tk7=request.getParameter("t1");
}
if (p==-6){
String numc="0";
try{
Class.forName("com.mysql.jdbc.Driver");
Connection connection =
DriverManager.getConnection("jdbc:mysql://localhost:3306/ptun",
"root", "awsm");
ResultSet
rsnt = null;
Statement
statement = connection.createStatement();
String sql
="";
if
(pesanfilter!=""){
sql =
("SELECT count(*) as cosum from brgptun where
kbrgptun<='"+tk7+"' and nmbrgptun='"+tk8+"'");
}else{
sql =
("SELECT count(*) as cosum from brgptun where
kbrgptun<='"+tk7+"'");
}
rsnt=statement.executeQuery(sql);
if (rsnt.next()){
numc=rsnt.getString(1);
}
rsnt.close();
connection.close();
}
catch (Exception ex){
}
int rr=Integer.parseInt(numc);
if(rr==0){
rr=1;
}
count=rr;
}else{
count=Integer.parseInt(countstring);
}
//--------------------------------------------------------------------
if (p==-3){
tk1=request.getParameter("t1");
tt2=request.getParameter("t2");
tt3=request.getParameter("t3");
tt4=request.getParameter("t4");
tt5=request.getParameter("t5");
tt6=request.getParameter("t6");
try{
Class.forName("com.mysql.jdbc.Driver");
Connection connection =
DriverManager.getConnection("jdbc:mysql://localhost:3306/ptun",
"root", "awsm");
if
(tt6==""){
PreparedStatement
pre=connection.prepareStatement("UPDATE brgptun SET
nmbrgptun=?,typebrgptun=?,pictfilebrgptun=?,vabrgptun=?,pictfilewebbrgptun=?
WHERE kbrgptun=?");
pre.setString(1, tt2);
pre.setString(2, tt3);
pre.setString(3, tt4);
pre.setString(4, tt5);
pre.setString(5, tt6);
pre.setString(6, tk1);
pre.executeUpdate();
pre.close();
connection.close();
}else
if (tt6!="" ){
File
imgFile= new File("C:/Documents and Settings/agus/My
Documents/NetBeansProjects/WebApplicationPtun/web/upload/"+tt6);
FileInputStream fin =new FileInputStream(imgFile);
PreparedStatement pre=connection.prepareStatement("UPDATE brgptun
SET
nmbrgptun=?,typebrgptun=?,pictfilebrgptun=?,vabrgptun=?,pictfilewebbrgptun=?,pictbrgptun=?
WHERE kbrgptun=?");
pre.setString(1, tt2);
pre.setString(2, tt3);
pre.setString(3, tt4);
pre.setString(4, tt5);
pre.setString(5, tt6);
pre.setString(7, tk1);
pre.setBinaryStream(6,fin,(int)imgFile.length());
pre.executeUpdate();
pre.close();
connection.close();
}
}
catch (Exception ex){
}
countstring=request.getParameter("h2");
if (countstring==null){
countstring="1";
}
count=Integer.parseInt(countstring);
}
if (p==-4){
tk1=request.getParameter("t1");
try{
Class.forName("com.mysql.jdbc.Driver");
Connection connection =
DriverManager.getConnection("jdbc:mysql://localhost:3306/ptun",
"root", "awsm");
PreparedStatement
pre=connection.prepareStatement("DELETE from brgptun WHERE kbrgptun=?
");
pre.setString(1, tk1);
pre.executeUpdate();
pre.close();
connection.close();
}
catch (Exception ex){
}
countstring=request.getParameter("h2");
if (countstring==null){
countstring="1";
}
count=Integer.parseInt(countstring);
count=count-1;
}
//------------------------------------------------------
int n=count-1;
try{
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/ptun",
"root", "awsm");
ResultSet
rs = null;
Statement
statement = connection.createStatement();
String
sql ="";
if (pesanfilter!=""){
sql = ("SELECT * from brgptun where
nmbrgptun='"+tk8+"' limit "+n+",1");
}else{
sql = ("SELECT * from brgptun limit
"+n+",1");
}
rs=statement.executeQuery(sql);
while (rs.next()){
pimage=rs.getString(1);
tt1=rs.getString(1).toString().replace("null",
"");
tt2=rs.getString(2);
tt3=rs.getString(3);
tt4=rs.getString(4);
tt5=rs.getString(5);
tt6=rs.getString(6);
}
rs.close();
connection.close();
}
catch (Exception ex){
}
//----------------------------------------------------------
String boldis="disabled";
if (p==-1){
tt1="";
tt2="";
tt3="";
tt4="";
tt5="";
tt6="";
boldis="";
}
if (saveFile!="" ){
tt1="";
tt2="";
tt3="";
tt4="";
tt5="";
tt6="";
boldis="";
}
//-------------------------------------------------------
String
jrt="0";
try{
Class.forName("com.mysql.jdbc.Driver");
Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/ptun",
"root", "awsm");
ResultSet
rsnt = null;
Statement
statement = connection.createStatement();
String sql =
"";
if (pesanfilter!=""){
sql = ("SELECT count(*) as co from brgptun where nmbrgptun='"+tk8+"'");
}else{
sql = ("SELECT count(*) as co from brgptun");
}
rsnt=statement.executeQuery(sql);
if (rsnt.next()){
jrt=rsnt.getString(1);
}
rsnt.close();
connection.close();
}
catch (Exception ex){
}
//--------------------------------------
String sf=tt6;
String pff=request.getParameter("ff");
if (pff==null){
pff="0";
}
int perf=Integer.parseInt(pff);
if (perf==-8){
File imgFile2= new
File("C:/Documents and Settings/agus/My Documents/NetBeansProjects/WebApplicationPtun/web/upload/"+sf);
if
(imgFile2.exists()){
imgFile2.delete();
}
}
//---------------------------------------------
%>
<form name='f1'
id='f1' method='POST' action='http://localhost:8084/Form_Barang.jsp' >
<div
id='apDiv2'>
<label>Kode
Barang</label>
<p>
<input
type='text' name='t1' id='t1' value='<%=tt1%>' />
<label>Nama
Barang</label>
<input
type='text' name='t2' id='t2' value='<%=tt2%>' />
</p>
<p><label>Type Barang</label>
<input
type='text' name='t3' id='t3' value='<%=tt3%>' />
</p>
<p><label>Picture File</label>
<input
type='text' name='t4' id='t4' value='<%=tt4%>' />
</p>
<p><label>VA
Barang</label>
<input type='text' name='t5' id='t5'
value='<%=tt5%>' />
</p>
<p><label>Picture
Web</label><br/><label>(Upload(.jpeg))</label>
<input
type='text' name='t6' id='t6' value='<%=tt6%>' />
</p>
<input
name='h1' id='12'type='hidden' value='<%=count%>' />
<input
name='h2' id='h2' type='hidden'
value='<%=countstring%>' />
<input
name='hpf' type='hidden' value='<%=ppf%>'
/>
<input
name='hdel' id='hdel' type='hidden' value='delete' />
<input
name='hp' type='hidden' value='0' />
<input
name='th6' type='hidden'
value='<%=saveFile%>' />
</div>
<div
class='style1' id='apDiv3'>DATA BARANG</div>
<div
id='apDiv5'>
<input
type='text' name='t7' id='t7' value='<%=tk7%>' />
<input
type='submit' name='b11' id='b11' onclick='bcari()' value='Find' />
<input
type='submit' name='b12' id='b12' value='End Find' />
<label
id='l1'><span class='style4'>Untuk Cari Kode Barang
disini</span></label>
</div>
<div id='apDiv4'>
<input
type='submit' name='b1' id='b1' onclick='bt1()' value='<<' />
<input
type='submit' name='b2' id='b2' onclick='bt2()' value='<' />
<input
type='submit' name='b3' id='b3' onclick='bt3()' value='>' />
<input
type='submit' name='b4' id='b4' onclick='bt4()' value='>>' />
<input
type='submit' name='b5' id='b5' onclick='bt5()' value='Add' />
<input
type='submit' name='b6' id='b6' onclick='bt6()' value='Save' <%=boldis%> />
<input
type='submit' name='b7' id='b7' onclick='bt7()' value='Edit' />
<input
type='submit' name='b8' id='b8' onclick='bt8()' value='Delete'/>
<a id='b9'
href='http://localhost'>_Exit_</a>
<a id='b10'
href='http://192.168.1.252:8084/pdfjsp.jsp'
target='_BLANK'>_Report_</a>
<a id='b15'
href='http://192.168.1.252:8084/exportexcel.jsp'
target='blank'>_Export(excel)_</a>
</div>
<div id='apDiv6'>
<input type='text'
name='t8' id='t8' value='<%=tk8%>'/>
<input
type='submit' name='b13' id='b13' onclick='bt13()' value='Filter' />
<input
type='submit' name='b14' id='b14' onclick='bt14()' value='End Filter'
/><label>U<span class='style4'>ntuk Filter berdasarkan
Nama Barang disini
...</span></label>
</div>
<div id='apDiv7'>
<table id='tb1'
width='700' border='1' cellspacing='0' cellspading='0'>
<tr
id='tr1'>
<td>*</td>
<td>Kode
Barang</td>
<td>Nama
Barang</td>
<td>Type
Barang</td>
<td>Picture File</td>
<td>VA
Barang</td>
<td>Picture Web</td>
</tr>
<%
int cn=count-1;
int ns=cn/3;
int nn=ns*3;
try{
Class.forName("com.mysql.jdbc.Driver");
Connection connection =
DriverManager.getConnection("jdbc:mysql://localhost:3306/ptun",
"root", "awsm");
ResultSet
rs = null;
Statement
statement = connection.createStatement();
String
sql ="";
if (pesanfilter!=""){
sql =
("SELECT * from brgptun where nmbrgptun='"+tk8+"' limit
"+nn+",3");
}else{
sql =
("SELECT * from brgptun limit "+nn+",3");
}
rs=statement.executeQuery(sql);
int ii=0;
int is;
String caricur;
while (rs.next()){
ii=ii+1;
is=ii+nn;
if (is==count){
caricur="
>>";
}else
{
caricur=String.valueOf(is);
}
%>
<tr>
<td><a id='bc'
href='http://localhost:8084/Form_Barang.jsp?h2=<%=is%>&ff=-8&hpf=<%=ppf%>&t8=<%=tk8%>'>_<%=caricur%>_</a></td>
<td><%=rs.getString(1)%></td>
<td><%=rs.getString(2)%></td>
<td><%=rs.getString(3)%></td>
<td><%=rs.getString(4)%></td>
<td><%=rs.getString(5)%></td>
<td><%=rs.getString(6)%></td>
</tr>
<%
}
rs.close();
connection.close();
}
catch (Exception ex){
}
%>
</table>
</div>
<div
id="apDiv8"><%=count%></div>
<div class="style4"
id="apDiv9">Dari</div>
<div id="apDiv10"><%=jrt%></div>
<div id='apDiv11'>
<input
type='image' name='im1' id='im1'
src="http://localhost:8084/imgfile.jsp?id='<%=pimage%>'"
alt='(Gambar/ foto kosong) Ket: Gambar
harus berekstensi (.jpeg/.jpg)'/>
</div>
<div
id="apDiv12"><b><%=pesanfilter%></b></div>
</form>
<form name='f2'
id='f2' method='POST' action='http://localhost:8084/Form_Barang.jsp'
ENCTYPE="multipart/form-data">
<input type='file'
name='pf' id='pf' />
<input
type='submit' name='bf' id='bf' value='Upload & Add/ Edit'/>
</form>
<script language='javascript'>
function bt14(){
document.f1.hpf.value =0
document.f1.t8.value = ""
}
function bt13(){
document.f1.hpf.value =-5
document.f1.h2.value = "1"
}
function bcari(){
document.f1.hp.value =-6
}
function bt1(){
document.f1.h2.value = "1"
}
function bt2(){
var
counter = 0
counter = parseInt(document.f1.h1.value) - 1
document.f1.h2.value = counter
if
(counter<=1){
document.f1.h2.value = "1"
alert('Sudah di awal record');
}
}
function bt3(){
var
counter = 0
counter = parseInt(document.f1.h1.value) + 1
document.f1.h2.value = counter
if
(counter>=<%=jrt%>){
document.f1.h2.value = <%=jrt%>
alert('Sudah di akhir record');
}
}
function bt4(){
document.f1.h2.value = "<%=jrt%>"
}
function bt5(){
document.f1.hp.value =-1
}
function bt6(){
document.f1.hp.value =-2
}
function bt7(){
document.f1.hp.value =-3
}
function bt8(){
var
pesan=confirm('Apakah anda yakin data akan di hapus ?')
if (pesan==1){
document.f1.hp.value =-4
}else{
}
}
</script>
</div>
</body>
</html>
imgfile.jsp
<%@page contentType="text/html"
pageEncoding="UTF-8"%>
<%@ page
language="java" import="java.sql.*" import=
"java.sql.Connection.*"import= "java.sql.DriverManager.*"
import= "java.sql.ResultSet.*"
import=" java.sql.Statement.*"
errorPage="" %>
<%@ page
import="java.util.*" %>
<%@ page
import="java.io.*" import="java.lang.Exception" %>
<%
Class.forName("com.mysql.jdbc.Driver");
Connection connection =
DriverManager.getConnection("jdbc:mysql://localhost:3306/ptun",
"root", "awsm");
ResultSet
rs = null;
Statement
statement = connection.createStatement();
String sql = ("SELECT pictbrgptun from brgptun where
(kbrgptun=" + request.getParameter("id") + ") ");
rs=statement.executeQuery(sql);
while (rs.next()){
response.setContentType("image/jpeg");
OutputStream os =
response.getOutputStream();
os.write(rs.getBytes(1));
os.flush();
os.close();
}
%>
Tidak ada komentar:
Posting Komentar