留言板的制作

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: rebeccapurple;
            font-weight: bold;
        }
    </style>
</head>
<body bgcolor="#f0ffff">

<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 = "第" + + i  + "楼 <br> "
            + "留言人: " + name + "<br>"
            + "标题: " + title + "<br>"
            + "内容:  " + message + "<br>"
            + "时间:  " + datetime + ".<hr>";

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

showMessage.jsp:
<%@ 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>
   </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--) {
                StringTokenizer st = new StringTokenizer(v.get(i), ".");
                while (st.hasMoreElements()) {
                    out.print(st.nextToken() + "<br>");
                }

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

结果展示:
在这里插入图片描述
在这里插入图片描述在这里插入图片描述

发布了101 篇原创文章 · 获赞 47 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/TONGZONGE/article/details/104069952