Welcome to Abdul Malik Ikhsan's Blog

nl2br pada jsp

Posted in Tutorial JSP by samsonasik on January 8, 2008

sama halnya dengan php, di jsp kita juga bisa menggunakan method untuk ganti baris saat si user mengetikkan komentar pada text area sehingga tampil apa adanya seperti yang user ketikkan, misalnya mengetikkan “wah, bagus banget nih, <Enter>boleh minta tanda tangan ga ?: ) “, kalau tanpa function untuk memanggil ganti baris, maka halaman web hanya akan menampilkan satu baris, karena value  [] Enter tidak dikenali sehingga dianggap seperti spasi.
Berikut contoh penerapan pada aplikasi buku tamu :
Seperti biasa, siapkan file koneksi di Bean-nya :
/* =============================
simpan di –> web-inf/learn/config.java;
*/

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

public class config{

public Connection con=null;
public Statement st=null;
public ResultSet rs=null;
public String driver=”com.mysql.jdbc.Driver”;
public String url=”jdbc:mysql://localhost/belajar”;
public String user=”root”;
public String pass=””;

public void konekdb()throws Exception{
try{
Class.forName(driver);
con=DriverManager.getConnection(url,user,pass);
}catch(Exception e){}
st=con.createStatement();
}

public boolean getnext()throws Exception{
return rs.next();
}

public String getText(String t)throws Exception{
return rs.getString(t);
}
}

/*——–web-inf/learn/Insert.java———–*/

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

public class Insert extends config{
public void insert(String nama,String email,String komentar)throws Exception{
st.executeUpdate(“insert into bukutamu values(‘”+nama+”‘,'”+email+”‘,'”+komentar+”‘)”);
}
}

/* ———–web-inf/learn/Tampil.java ————–*/

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

public class Tampil extends config{
public void seleksi(String query)throws Exception{
rs=st.executeQuery(query);
}
public void bukutamu()throws Exception{
seleksi(“select * from bukutamu”);
}
}

==============
Nah, sekarang kita buat file inputan di *.jsp nya

Code:
<form id=”form1″ name=”form1″ method=”post” action=”save.jsp”>
Nama :
<input type=”text” name=”nama” />
<br />
Email :
<input type=”text” name=”email” />
<br />
Komentar :
<textarea name=”komentar”></textarea>
<input type=”submit” name=”Submit” value=”Submit” />
</form>

/*===========
/* save.jsp */====
===============
*/

Code:
<jsp:useBean id=”obj” class=”learn.Insert”/><%
obj.konekdb();
String nama=request.getParameter(“nama”);
String email=request.getParameter(“email”);
String komentar=request.getParameter(“komentar”);

obj.insert(nama,email,komentar);
response.sendRedirect(“tampilbukutamu.jsp”);
%>

================
tampilbukutamu.jsp
================

Code:
<%!
//deklarasi method ganti baris pada komentar
//( sama dengan function nl2br pada php)
String nl2br(String str){
return str.replaceAll(“\n”, “<br>”);
}
%>
<jsp:useBean id=”obj” class=”learn.Tampil”/>
<%
obj.konekdb();
obj.bukutamu();while (obj.getnext()){
%>
<table width=”307″ border=”1″>
<tr>
<td width=”71″>Nama</td>
<td width=”10″>:</td>
<td width=”204″> <%=obj.getText(“nama”)%></td>
</tr>
<tr>
<td>Email</td>
<td>:</td>
<td> <%=obj.getText(“email”)%></td>
</tr>
<tr>
<td valign=”top”>Komentar</td>
<td valign=”top”>:</td>
<td valign=”top”> <%=nl2br(obj.getText(“komentar”))%></td>
</tr>
</table>
<%
}
%>
Advertisements

2 Responses

Subscribe to comments with RSS.

  1. bisri said, on December 27, 2008 at 3:27 am

    Nggak bisa Om…error trus

  2. samsonasik said, on December 27, 2008 at 9:18 am

    errornya apa? mungkin ada kesalahan dalam konfigurasi, coba baca deh konfigurasi web server, kalau pakai tomcat, bisa baca http://www.coreservlets.com/Apache-Tomcat-Tutorial


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: