您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
本篇文章为大家展示了利用java编写一个留言板功能,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
JSP+JavaBean的留言板技术
<span >Messages.html <HTML></span><span ><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 >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 >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 >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 >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 >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>
上述内容就是利用java编写一个留言板功能,你们学到知识或技能了吗?如果还想学到更多技能或者丰富自己的知识储备,欢迎关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。