利用application实现留言板功能。
Submit.jsp
<%@ page contentType="text/html;charset=GB2312" %>
<HTML><BODY>
<FORM action="message.jsp" method="post" name="form">
<p>姓名:
<Input type="text" name="name">
<BR>主题:
<Input type="text" name="zhuti">
<BR>留言内容:
<BR><TextArea name="messages" ROWS="10" COLS="20" WRAP="physical" >
</TextArea>
<BR><Input type="submit" value="提交" name="submit">
</FORM>
<FORM action="showmessage.jsp" method="post" name="form1">
<Input type="submit" value="查看留言板" name="look">
</FORM>
</BODY></HTML>
Message.jsp
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import="java.util.*" %>
<HTML><BODY>
<%!Vector v=new Vector();
ServletContext application;
synchronized void sendMessage(String s){
application=getServletContext();
v.add(s);
application.setAttribute("Mess",v);
}
%>
printform <% request.setCharacterEncoding("GBK");
String Parameter("name");
String Parameter("zhuti");
String Parameter("messages");
if(name==null){
name="guest"+(int)(Math.random()*10000);
}
if(zhuti==null){
zhuti="无标题";
}
if(messages==null){
messages="无信息";
}
String time=new Date().toString();
String s="#"+name+"#"+zhuti+"#"+time+"#"+messages+"#";
sendMessage(s);
%>
</BODY></HTML>
Showmessage.jsp
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import="java.util.*" %>
<HTML><BODY>
<% Vector v=(Attribute("Mess");
out.print("<table border=2>");
out.print("<tr>");
out.print("<td bagcolor=yellow>"+"姓名"+"</td>");
out.print("<td bagcolor=yellow>"+"主题"+"</td>");
out.print("<td bagcolor=yellow>"+"留言时间"+"</td>");
out.print("<td bagcolor=yellow>"+"留言内容"+"</td>");
out.print("</tr>");
for(int i=0;i<v.size();i++){
out.print("<tr>");
String message=(String)v.elementAt(i);
StringTokenizer fenxi=new StringTokenizer(message,"#");
out.print("<tr>");
int untTokens();
for(int k=0;k<number;k++){
String Token();
if(k<number-1){
out.print("<td bgcolor=yellow>"+str+"</td>");
}
else{
out.print("<td><TextArea rows=3 cols=12>"+str+"</TextArea></td>");
}
}
}
out.print("</tr>");
out.print("</table>");
%>
</BODY></HTML>
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论