jsp 用application对象制作留言板

 <%@ page contentType="text/html; charset=gb2312"%>
<html>
<body>
<form action="show.jsp" method="post">
输入名字:<input type="text" name="name"><br> 留言标题:<input
type="text" name="title"><br> 留言:<br>
<textarea rows="" cols="" name="text" wrap="physical"></textarea>
<br> <input type="submit" value="提交"><br>
</form>
<form action="chuli.jsp" method="get">
<input type="submit" name="look" value="查看留言板"><br>
</form> </body>
</html>
<%@ page contentType="text/html; charset=gb2312"%>
<%@page import="java.util.Vector"%>
<%@ page import="java.util. *"%>
<%
request.setCharacterEncoding("gb2312");
String name = request.getParameter("name");
String title = request.getParameter("title");
String text = request.getParameter("text");
if (name == null || name.length() == )
name = "";
if (title == null || title.length() == )
title = "";
if (text == null || text.length() == )
text = "";
String s = name + "#" + title + "#" + text;
add(s);
%>
<%!Vector vector = new Vector();
ServletContext application;
//有些服务器不直接支持application对象,
//必须用ServletContext 类类声明这个对象,
//再使用getServletContext方法
//对application对象初始化
int i = ; synchronized void add(String s) { application = getServletContext();
i++;
vector.add("NO." + i + "," + s);
application.setAttribute("mess", vector);
//如果添加的2个对象的关键字相同,则先前的关键字被清除,mess叫索引关键字
}%>
<a href="chuli.jsp">返回留言界面</a>
<%@ page contentType="text/html; charset=gb2312"%>
<%@ page import="java.util.Vector"%>
<%!public String handString(String s) {
try {
byte b[] = s.getBytes("gb2312");
s = new String(b);
} catch (Exception exp) {
}
return s;
}%>
<%
Vector vector = (Vector) application.getAttribute("mess");
//因为任何对象都可以添加到application中,取回对象时,
//所以要强制转回原来的类型。
for (int i = ; i < vector.size(); i++) {
//vector的长度就是vector.size();
String message = (String) vector.elementAt(i);
//Vector就是一个放数据的地方啊。elementat(i)就是取出数据的作用
String str[] = message.split("#");
out.print("留言人:" + handString(str[]) + ";");
out.print("标题:" + handString(str[]) + "<br>");
out.print("留言内容:" + "<br>" + handString(str[])); }
%>
<a href="input.jsp">返回主界面</a>
上一篇:关于reset.css的疑问:为什么一定要重置浏览器样式?


下一篇:C++ Vector