一、实验目的
掌握怎样在JSP中使用内置对象application
二、实验项目内容(实验题目)
编写代码,掌握application的用法。【参考课本例题4-16 留言板 】
三、源代码以及执行结果截图:
example4_16.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<style>
#textStyle{
font-family:宋体; font-size:18;color:bule
}
</style>
<body id='textStyle'bgcolor=#ffccff>
<form action='example4_16_pane.jsp'method='post'>
留言者:<input type='text'name='peopleName'size=40/>
<br>标题:<input type='text'name='title'size=42/>
<br>留言:<br>
<textArea name='contens' id='textStyle' rows="10" cols=36 wrap='physical'>
</textArea>
<br><input type='submit'id='textStyle' value='提交留言'name='submit'/>
</form>
<a href='example4_16_delete.jsp'>删除留言</a>
<a href='example4_16_show.jsp'>查看留言</a>
</body></html>
example4_16_pane.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ page import= "java.time.LocalDateTime" %>
<%@ page import= "java.util.Stack" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body>
<%!
Stack<Integer>maxAmount = null;
synchronized void addMess(ServletContext application, StringBuffer mess){
int index= -1;
if(!maxAmount.empty()){
index = maxAmount.pop();
mess.insert(0,"No."+index+".");
application.setAttribute(""+index, new String(mess));
}
}
%>
<%
if(maxAmount == null){
maxAmount = new Stack < Integer>();//最多可以有999条留言
for(int i=99999; i>=1; i--){
maxAmount.push(i);
}
}
boolean isSave= true;
request.setCharacterEncoding("utf-8");
String peopleName = request.getParameter("peopleName");
String title = request.getParameter("title");
String contents = request.getParameter("contents");
if(peopleName.length() == 0 || title.length() == 0 || contents.length() == 0){
isSave = false;
out. print("<h2>" + "请输人留言者、标题和内容");
}
if(isSave){
LocalDateTime dateTime = LocalDateTime. now();
StringBuffer message = new StringBuffer();
message.append("留言者:" + peopleName+"#");
message.append("< br>留言标题«" + title+"»#");
message.append("<br>留言内容:<br>" + contents+"#");
String timeFormat =
String.format("%tY年%<tm月%<td日,%<tH:%<tM:%<tS",dateTime);
message.append("<br>留言时间<br>" + timeFormat+"#");
if(maxAmount.empty()){
out.print("<h2>"+"留言板已满,无法再留言"+"</h2");
}
else{
addMess(application,message);
}
}
%>
<br><a href= "example4_16.jsp">返回留言页面</a><br>
<a href = "example4_16_show.jsp">查看留言板</a>
</body>
</html>
example4_16_show.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ page import = "java.util.Enumeration" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body bgcolor=cyan>
<p style="font-family:宋体; font-size:14; color:black" >
<a href = "main.jsp" >返回留言页面</a><br><br>
<%
Enumeration<String>e= application.getAttributeNames();
while(e.hasMoreElements()){
String key= e.nextElement( );
String regex = "[1-9][0-9]*";
if (key.matches(regex)){
String message = (String) application.getAttribute(key);
String [] mess = message.split("#");
out. print(mess[0]);
out. print(mess[1]);
out. print(mess[2]);
out. print(mess[3]);
out. print("<br>------<br>");
}
}
%>
</body>
</html>
example4_16_delete.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ page import = "java.util.Enumeration" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Insert title here</title>
</head>
<body bgcolor=pink>
<p style="font-family:宋体; font-size:18; color:blue" >
管理员删除留言
<form action = "" method=post>
输入密码:<input type="password" name="password" size=12/><br>
输入留言序号:<input type="text" name="index" size=6/>
<br><input type="submit" name="submit" value="删除"/>
</form>
<%
String password=request.getParameter("passwoed");
String index=request.getParameter("index");
if(password==null)password="";
if(index==null)index="";
if(password.equals("123456")){
Enumeration<String>e = application.getAttributeNames();
while(e.hasMoreElements()){
String key=e.nextElement();
if(key.equals(index)){
application.removeAttribute(key);
out.print("<br>删除了第" + index+ "条留言<br>");
}
}
}
%>
<a href= "example4_16.jsp">返回留言页面</a><br>
<a href = "example4_16_show.jsp">查看留言板</a>
</p></body></html>
效果图