jsp中运用application实现共享留言板功能

时间:2021-05-16 18:34:25

jsp中application的知识点总结:

1.一个Web应用程序启动后,将会自动创建一个application对象,在整个应用程序的运行过程中只有这一个application对象,即所有访问该网站的客户都共享一个application对象。

2.作用:在整个应用运行期间保存共享数据,实现用户间数据的共享。

3.application对象的生命周期:从Web服务器启动,直到Web服务器关闭。

application对象是应用程序级的,如果application中不存在String name,则通过方法Object getAttribute(String name)获得的对象时null。

在同一个网站下的任何地方都可以对application对象进行操作,主要操作有两个,即下面的两个方法:
   Object getAttribute(String name) 从 application对象中提取指定的对象。
   void setAttribute(String name,Object value) 将对象添加到application对象中。

本应用的基本介绍:

 通过application,实现共享留言板功能,效果图如下:

jsp中运用application实现共享留言板功能

inputMessage.jsp:

<%@ page language="java" import="java.text.*,java.util.*"
	contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<style>
#form2 input {
	color: green;
	font-weight: bold;
}
</style>
</head>
<body bgcolor="#abcdef">

	<form action="checkMessage.jsp" method="post">
		请输入姓名: <input type="text" name="name" /><br> 请输入标题: <input
			type="text" name="title" /><br> 请输入内容:
		<textarea cols="40" rows="10" name="message"></textarea>
		<br> <br> <br> <input type="submit" value="留言" />
	</form>
	<br>
	<form id="form2" action="showMessage.jsp" method="post">
		<input type="submit" value="查看留言板" />
	</form>

</body>
</html>

  

checkMessage.jsp:

<%@ page language="java" import="java.text.*,java.util.*"
	contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body bgcolor="#abcdef">
	<%!Vector<String> v = new Vector<String>();
	int i = 0;%>
	<%
		String datetime = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(Calendar.getInstance().getTime()); //获取系统时间
	%>
	<%
		request.setCharacterEncoding("utf-8");
		String name = request.getParameter("name");
		String title = request.getParameter("title");
		String message = request.getParameter("message");
	%>
	<%
		if (name == null || "".equals(name.trim())) {
			//trim()主要解决里面只有空格的问题
			name = " 网友" + (int) (Math.random() * 100000 + 10000);
		}
		if (title == null || "".equals(title.trim())) {
			title = " 无";
		}
		if (message == null || "".equals(message.trim())) {
			message = " 无";
		}
	%>
	<%
		i++;
		String str = "第" + "<span class=span0>" + i + "</span> " + "楼  "
				+ ".<span class=span1>留言人: </span>" + name + ".<span class=span2>标题: </span>" + title
				+ ".<span class=span3>内容: </span><br>    " + message
				+ ".<span class=span4>时间: </span>  " + datetime + ".<hr>";

		v.add(str);
		application.setAttribute("message", v);
	%>
	留言成功.
	<a href="inputMessage.jsp">返回留言板</a>
</body>
</html>

showMessage.jsp:

<%@page
	import="com.sun.org.apache.xml.internal.serializer.utils.StringToIntTable"%>
<%@ page language="java" import="java.util.*"
	contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<style>
body {
	background: RGBA(38, 38, 38, 1);
}

div {
	width: 800px; //
	border: 1px solid RGBA(100, 90, 87, 1);
	color: white;
}

span {
	font-size: 20px;
	font-weight: bold;
}

.span0 {
	color: red;
	font-size: 25px;
}

.span1 {
	color: green;
}

.span2 {
	color: orange;
}

.span3 {
	color: green;
}

.span4 {
	color: red;
}
</style>
</head>
<body>
	<div>
		<%
			Object o = application.getAttribute("message");
			if (o == null) {
				out.print("暂时还没有留言呢");
			} else {
				Vector<String> v = (Vector<String>) o;
				for (int i = v.size() - 1; i >= 0; i--) {
					// 注意必须用/.	String[] st1 = v.get(i).split("/.");
					// 				for (int j = 0; j < st1.length; j++) {
					// 					out.print(st1[j] + "<br>");
					// 				}
					// 				out.print("<br>");

					StringTokenizer st = new StringTokenizer(v.get(i), ".");
					while (st.hasMoreElements()) {
						out.print(st.nextToken() + "<br>");
					}

				}
			}
		%>
	</div>
</body>
</html>