java实现留言板功能实例
程序员文章站
2024-03-01 22:08:04
jsp+javabean的留言板技术
messages.html
jsp+javabean的留言板技术
<span style="font-size:16px;">messages.html <html></span><span style="font-size:16px;"><head> <title> message board </title></head> <body> <center>留言板</center> <form action="addmessage.jsp" > <table border=1 align="center"> <tr><td>姓名:</td><td><input type="text" name="name" size=25> </td></tr> <tr><td>e-mail:</td> <td><input type="text" name="email" size=25></td></tr> <tr><td>主题:</td> <td><input type="text" name="title" size=25></td></tr> <tr><td>留言:</td> <td><textarea name="content" rows=7 cols=25></textarea> </td></tr> <tr><td colspan=3> <table align="center" width="100%" cellspacing="0" cellpadding="0" > <tr> <td align="center"><input type="submit" value="提交留言"></td> <td align="center"> <a href="viewmessages.jsp" rel="external nofollow" ><font size=2>查看留言</font></a></td> <td align="center"><input type="reset" value="重新填写"></td> </tr></table></td> </tr></table></form></body></html></span> ? <span style="font-size:16px;">messagedata.java package message; public class messagedata { private string name,email,title,content; //setter或者getter方法 public void setname(string name){this.name=name;} public void setemail(string email){ this.email=email;} public void settitle(string title){ this.title=title;} public void setcontent(string content){this.content=content;} public string getname(){ return this.name;} public string getcontent(){ return this.content;} public string gettitle(){ return this.title;} public string getemail(){ return this.email;}}</span> ? <span style="font-size:16px;">viewmessages.jsp <%@ page contenttype="text/html; charset=gbk" import="message.messagedata" %> <%@ page import="java.util.*"%> <jsp:usebean id="mybean" class="message.messagebean" scope="page"/> <html><head><title> show the message in the table </title></head> <body><p align="center">所有留言</p> <table align="center" width="80%" border=1 > <% int message_count=0; collection <messagedata> messages=mybean.getallmessage(); iterator <messagedata> it=messages.iterator(); while(it.hasnext()){ messagedata mg=(messagedata)it.next(); %> <tr> <td width="20%">留言人:</td> <td width="23%"><%=mg.getname()%></td> <td width="58%" align="center"><% out.println( "<a href=mailto:"+mg.getemail()+">"+mg.getemail()+"</a>"); %></td></tr> <tr> <td width="20%">主题:</td> <td colspan="3"><%=mg.gettitle()%></td> </tr> <tr> <td width="20%">内容:</td> <td colspan="3"><%=mg.getcontent()%></td> </tr> <% message_count++; } %> </table> <p align="center"><a href="messages.html" rel="external nofollow" >我要留言</a></p> </body></html></span> ? <span style="font-size:16px;">addmessage.jsp <%@ page language="java" contenttype="text/html; charset=gbk" pageencoding="gbk"%> <jsp:usebean id="mdata" class="message.messagedata" scope="page"> <jsp:setproperty name="mdata" property="*"/></jsp:usebean> <jsp:usebean id="mybean" class="message.messagebean" scope="page"/> <html><head><title> message into table </title></head> <body> <% try { mybean.setmessage(mdata); mybean.addmessage(); } catch(exception e) { e.printstacktrace();} %> <jsp:forward page="viewmessages.jsp" /> </body></html></span> ? <span style="font-size:16px;">messagedata.java package message; public class messagedata { private string name,email,title,content; //setter或者getter方法 public void setname(string name){this.name=name;} public void setemail(string email){ this.email=email;} public void settitle(string title){ this.title=title;} public void setcontent(string content){this.content=content;} public string getname(){ return this.name;} public string getcontent(){ return this.content;} public string gettitle(){ return this.title;} public string getemail(){ return this.email;}}</span> ? <span style="font-size:16px;">messagebean.java package message; import java.sql.*; //引入java.sql包 import java.util.*; public class messagebean { private connection con; messagedata msg; public messagebean() { string jdriver="com.mysql.jdbc.driver"; //定义驱动程序对象 string username="root"; //定义数据库用户名 string userpasswd=""; //定义数据库存取密码 string dbname="message"; //定义数据库名 string conurl="jdbc:mysql://localhost:3306/"+dbname; try{class.forname(jdriver).newinstance(); //加载jdbc驱动程序 con=drivermanager.getconnection(conurl,username,userpasswd); //连接数据库 } catch(exception e){system.err.println(e.getmessage());} } public void setmessage(messagedata msg) {this.msg=msg;} // 添加一条留言消息 public void addmessage()throws exception { try{ byte b1[]=msg.gettitle().getbytes("iso-8859-1"); string ti=new string(b1); byte b2[]=msg.getname().getbytes("iso-8859-1"); string na=new string(b2); byte b3[]=msg.getemail().getbytes("iso-8859-1"); string em=new string(b3); byte b4[]=msg.getcontent().getbytes("iso-8859-1"); string c=new string(b4); preparedstatement stm=con.preparestatement( "insert into messagetable values(?,?,?,?)"); stm.setstring(1,ti); stm.setstring(2,na); if((msg.getemail()).length()==0)stm.setstring(3,""); else stm.setstring(3,em); stm.setstring(4,c); try {stm.execute(); stm.close(); } catch(exception e) { } con.close(); //关闭数据库连接 } catch(exception e){ e.printstacktrace(); throw e;} } // 获得所有留言消息,并返回结果到jsp页面 public collection<messagedata> getallmessage()throws exception { collection<messagedata> ret=new arraylist<messagedata>(); try{ statement stm=con.createstatement(); resultset result=stm.executequery( "select count(*) from messagetable"); int message_count=0; if(result.next()){ message_count=result.getint(1); result.close(); } if(message_count>0) { result=stm.executequery("select * from messagetable "); while(result.next()) { string title=result.getstring("title"); string name=result.getstring("name"); string email=result.getstring("email"); string content=result.getstring("content"); messagedata message=new messagedata(); message.settitle(title); message.setname(name); message.setemail(email); message.setcontent(content); ret.add(message); } result.close(); stm.close(); } con.close(); } catch(exception e) { e.printstacktrace(); throw e; } return ret; } }</span>
功能简单的留言板,但是清楚的说明了jsp+javabean技术的运用,需要的朋友可以参考
上一篇: 如何在不同.net版本实现单点登录