JSP+JavaBean的留言板技术
java" id="highlighter_47979">
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
<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 id="codetool">
功能简单的留言板,但是清楚的说明了JSP+JavaBean技术的运用,需要的朋友可以参考 原文链接:http://www.2cto.com/kf/201610/555182.html 相关文章
热门资讯 |