Maison >Java >javaDidacticiel >Interface utilisateur simple Java implémentant la fonction de forum de messages Java
La technologie JSP JavaBean a été utilisée dans un forum de messages que j'ai réalisé auparavant. C'était un développement relativement complet, alors apprenons-en. Elle peut être utilisée dans ce projet de stage, alors j'y ai pensé pour la revoir un instant !
Technologie de forum de messages 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"><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">我要留言</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>
Un forum de messages avec des fonctions simples, mais qui illustre clairement la technologie JSP JavaBean, je crois cet exemple peut nous aider à mieux comprendre les principes de cette technologie.
Ce qui précède est l'interface utilisateur simple Java implémentant la fonction de forum de messages Java. Pour plus de contenu connexe, veuillez faire attention au site Web PHP chinois (www.php.cn) !