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,实现共享留言板功能,效果图如下:

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>

  

jsp中运用application实现共享留言板功能的更多相关文章

  1. Web开发从零单排之二:在自制电子请帖中添加留言板功能,SAE+PHP+MySql

    在上一篇博客中介绍怎样在SAE平台搭建一个html5的电子请帖网站,收到很多反馈,也有很多人送上婚礼的祝福,十分感谢! web开发从零学起,记录自己学习过程,各种前端大神们可以绕道不要围观啦 大婚将至 ...

  2. 利用反馈字段给帝国cms添加留言板功能(图文教程)

    帝国cms的插件中提供信息反馈字段,很多人却不会用.这里谢寒教大家如何来给自己的帝国cms网站添加留言板功能 1.找到添加地址 2.添加字段 3.你可以在字段中添加多种字段类型(有文本域,单行文本框, ...

  3. php实现留言板功能

    这个小小的留言板功能适合班级内或者公司内部之间的讨论,对话和留言,非常的方便,更重要的是无需网络,对于公司管理层来说是非常乐于常见的, 下面是这个留言板的写法: 1 首先是登录页面: <form ...

  4. 使用PHP连接数据库实现留言板功能

    PHP实现留言板功能: 1 首先是登录页面: <!DOCTYPE html><html>    <head>        <meta charset=&qu ...

  5. JS原生编写实现留言板功能

    实现这个留言板功能比较简单,所以先上效果图: 实现用户留言内容,留言具体时间. <script> window.onload = function(){ var oMessageBox = ...

  6. 原生JS实现简单留言板功能

    原生JS实现简单留言板功能,实现技术:css flex,原生JS. 因为主要是为了练手js,所以其中布局上的一些细节并未做处理. <!DOCTYPE html> <html lang ...

  7. jsp 用application对象制作留言板

    <%@ page contentType="text/html; charset=gb2312"%> <html> <body> <for ...

  8. jsp中 scope="application" 表示

    jsp中 <jsp:useBean id="countbean" scope="application" class="count.counte ...

  9. wordpress中page页添加非插件留言板功能

    把下面的代码插入到page页面中即可 <!-- 留言板 --> <div class="wrap"> <div id="primary&qu ...

随机推荐

  1. SQL Server 大数据量分页建议方案

    简单的说就是这个 select top(20) * from( select *, rowid = row_number() over(order by xxx) from tb with(noloc ...

  2. Mysql 分段统计

    今天遇到个小问题觉得挺有意思,与大家分享. 需求是这样的,对数据库中的一张表做按时间的分段统计,结果只要每个区间的数量. select YEAR(create_time) as nian,MONTH( ...

  3. $(function(){})、$(document).ready(function(){})....../ ready和onload的区别

    1.window.onload 当一个文档完全下载到浏览器中时,会触发 window.onload 事件. 这意味着页面上的全部元素对 javascript 而言都是可以访问的,这种情况对编写功能性的 ...

  4. 安卓奇葩问题之:返回按键监听,使Dialog不消失

    本文出处:http://bbs.9ria.com/thread-204406-1-1.html 在做自动更新的时候,弹出Dialog提示,要求是只能点击更新或者取消更新时Dialog才会消失.但是在这 ...

  5. 在centos上配置IP

    当我们安装好系统后,最先做的应该就是配置IP了,因为无论是要下载工具软件.还是远程链接,网络必不可少,所以我们要先来配置IP! 一.查看IP 如何在centos上查看IP呢,使用 ifconfig 命 ...

  6. Python成长笔记 - 基础篇 (九)

    创建一个socketserver 至少分以下几步: First, you must create a request handler class by subclassing the BaseRequ ...

  7. 安利eclipse插件之log4E

    敲完代码之后,据说要加注释.加log:OTL~~~~~~~~,在我仰天长叹之际,师父发给我了一个插件压缩包,解压-->拷贝-->重启-->了事.安装方法已经如此之便捷,简直是我辈懒癌 ...

  8. 你好,欢迎来到我的博客,我是博主royalmice

    你好,欢迎来到我的博客,我是博主royalmice

  9. 回调函数(callback)

    参考维基百科链接: 中文:http://zh.wikipedia.org/wiki/%E5%9B%9E%E8%B0%83%E5%87%BD%E6%95%B0 英文:http://en.wikipedi ...

  10. 练习1-23:删去C语言程序中所有的注释语句(C程序设计语言 第2版)

    #include <stdio.h> main() { FILE * fp_i; FILE * fp_o; fp_i = fopen("input.txt", &quo ...