jsp实验9 内置对象application

一、实验目的

掌握怎样在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>

效果图

  • 25
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值