JSP实现留言板
submitMsg.jsp
代码语言:javascript复制<%@page import="java.util.ArrayList"%>
<%@page import="bean.Message"%>
<%@page import="java.util.List"%>
<%@ page language="java" 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>
<form action="submitMsg.jsp" method="post">
<p>请输入姓名
<input type="text" name="nikename"><p/>
<p>请输入标题
<input type="text" name="title"><p/>
<p>请输入内容
<textarea rows="10" cols="36" name="message"></textarea><p/>
<input type="submit" value="提交" name="submit">
</form>
<form action="showMessage.jsp" method="post">
<input type="submit" value="查看" name="look">
</form>
<%
List<Message> list = (List<Message>)application.getAttribute("allMsgs");
String nikename=request.getParameter("nikename");
String title=request.getParameter("title");
String message=request.getParameter("message");
Message msg=new Message(nikename,title,message);
if(nikename!=null && title!=null && message!=null){
if(list==null){
list=new ArrayList<>();
list.add(msg);
}else{
list.add(msg);
}
application.setAttribute("allMsgs", list);
}
%>
</body>
</html>
showMessage.jsp
代码语言:javascript复制<%@page import="bean.Message"%>
<%@page import="java.util.List"%>
<%@ page language="java" 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>
<%
List<Message> list = (List<Message>)application.getAttribute("allMsgs");
if(list==null){
out.print("暂无数据!");
}else{
for(int i=0;i<list.size();i ){
%>
<ul>
<li>NO.<%=i 1 %></li>
<li>姓名:<%=list.get(i).getNikename() %></li>
<li>标题:<%=list.get(i).getTitle() %></li>
<li>内容:<%=list.get(i).getMessage() %></li>
</ul>
<%
}
}
%>
</body>
</html>
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/184238.html原文链接:https://javaforall.cn