java实现留言板功能实例
更新时间:2017年04月14日 14:38:51 投稿:wbb
这篇文章主要为大家详细介绍了JSP+JavaBean的留言板技术
,JavaWeb登陆功能实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
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技术的运用,需要的朋友可以参考
您可能感兴趣的文章:
相关文章
SpringCloud之消息总线Spring Cloud Bus实例代码
这篇文章主要介绍了SpringCloud之消息总线Spring Cloud Bus实例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧2018-04-04详解基于java的Socket聊天程序——客户端(附demo)
这篇文章主要介绍了详解基于java的Socket聊天程序——客户端(附demo),客户端设计主要分成两个部分,分别是socket通讯模块设计和UI相关设计。有兴趣的可以了解一下。2016-12-12
最新评论