JSP语言做简易留言板

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/csp_6666/article/details/78509088

首先奉上视图:

主界面:

 

查看留言界面:

 

看一下我前台后台:

 

下面开始给大家公布代码并且解释咯:

首先是Index.jsp界面:

 

<%@ page language="java"  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>
<%@page contentType="text/html; charset=gb2312" %>
<form action="WordServlet" method="post">
留言者:    <input type="text" name="author"/><br>
留言标题:<input type="text" name="title"/><br> 
留言内容:<textarea rows="5" cols="30" name="content"></textarea><br><br>
<input type="submit" value="确定"/>
<input type="reset" value="重置"/>
<a href="show.jsp">查看留言</a>
</form>
</body>
</html>

 

show.jsp代码:

 

<%@ page language="java"
	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>
	<%@ page contentType="text/html; charset=gb2312"%>
	<%@ page import="java.util.*"%>
	<%@ page import="xyz.Servlet.valuebean.*"%>
	<%
		ArrayList wordlist = (ArrayList) application
				.getAttribute("wordlist");
		if (wordlist == null || wordlist.size() == 0)
			out.print("没有留言可显示");
		else {
			for (int i = wordlist.size() - 1; i >= 0; i--) {
				WordSingle single = (WordSingle) wordlist.get(i);
	%>
	留言者:<%=single.getAuthor()%>
	<p>
		留言时间:<%=single.getTime()%>
	<p>
		留言标题:<%=single.getTitle()%>
	<p>
		留言内容:
		<textarea rows="7" cols="30" readonly> <%=single.getConent()%></textarea>
		<a href="index.jsp">我要留言</a>
	<hr width="100%">
	<%
		}

		}
	%>
</body>
</html>

 

 

 

接下来是后台代码:

 

 

WordServlet.java页面代码:

 

package xyz.Servlet;


import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;


import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


import xyz.Servlet.toolbean.MyTools;
import xyz.Servlet.valuebean.WordSingle;


/**
 * Servlet implementation class WordServlet
 */
public class WordServlet extends HttpServlet {
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		 doPost(request,response);
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//以下代码用来获取表单中字段内容并进行转码
		String author = MyTools.toChinese(request.getParameter("author"));
		String title = MyTools.toChinese(request.getParameter("title"));
		String content = MyTools.toChinese(request.getParameter("content"));
		//获取当前时间并格式化时间为指定格式
		String today=MyTools.changeTime(new Date());
		//创建值JavaBean对象来封装获取的信息
		WordSingle single= new WordSingle();
		single.setAuthor(MyTools.changeHTML(author)); //创建JavaBean对象用来封装获取信息
		single.setTitle(MyTools.changeHTML(title));
		single.setConent(content);
		single.setTime(today);
		
		HttpSession session = request.getSession(); //获取Sessoion
		ServletContext scx = session.getServletContext();//通过Session对象获取应用上下文
		ArrayList wordlist = (ArrayList) scx.getAttribute("wordlist");	//获取存储在应用上下文中的对象
		if(wordlist==null){ 			
				wordlist=new ArrayList();}
				wordlist.add(single);	//将封装了信息的值JavaBean存储到集合对象中
				scx.setAttribute("wordlist", wordlist);  //将集合对象保存到应用上下文
				response.sendRedirect("show.jsp");		//请求重定向show.jsp界面
		
	}


}
工具类页代码:
 
package xyz.Servlet.toolbean; import java.io.UnsupportedEncodingException; import java.text.SimpleDateFormat; import java.util.Date; public class MyTools { /** * @功能:转换字符串中属于HTML语言的特殊字符 * @参数:source为要转换的字符串 * @返回值:String类型值 */ public static String changeHTML(String source) { String changeStr = ""; changeStr = source.replace("&", "&");// &符号 changeStr = source.replace(" ", " "); changeStr = source.replace("<", "<"); changeStr = source.replace(">", ">"); changeStr = source.replace("\r\n", "<br>");// 回车换行 return changeStr; } /** * @功能:日期格式化 * @param date * @return String */ public static String changeTime(Date date) { // 创建一个格式化日期SimpleDateFormat对象,并同时指定日期最终被转换的格式 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return format.format(date); // 调用format方法格式化日期 } /** * 功能:解决提交表单产生的中文乱码 * * @param value为要转换的字符串 * @return String */ public static String toChinese(String str) { if (str == null) str = ""; try { str = new String(str.getBytes("ISO-8859-1"), "gb2312"); } catch (UnsupportedEncodingException e) { str = ""; e.printStackTrace(); } return str; } }


valuebean类代码:

 

 
 
package xyz.Servlet.toolbean; import java.io.UnsupportedEncodingException; import java.text.SimpleDateFormat; import java.util.Date; public class MyTools { /** * @功能:转换字符串中属于HTML语言的特殊字符 * @参数:source为要转换的字符串 * @返回值:String类型值 */ public static String changeHTML(String source) { String changeStr = ""; changeStr = source.replace("&", "&");// &符号 changeStr = source.replace(" ", " "); changeStr = source.replace("<", "<"); changeStr = source.replace(">", ">"); changeStr = source.replace("\r\n", "<br>");// 回车换行 return changeStr; } /** * @功能:日期格式化 * @param date * @return String */ public static String changeTime(Date date) { // 创建一个格式化日期SimpleDateFormat对象,并同时指定日期最终被转换的格式 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); return format.format(date); // 调用format方法格式化日期 } /** * 功能:解决提交表单产生的中文乱码 * * @param value为要转换的字符串 * @return String */ public static String toChinese(String str) { if (str == null) str = ""; try { str = new String(str.getBytes("ISO-8859-1"), "gb2312"); } catch (UnsupportedEncodingException e) { str = ""; e.printStackTrace(); } return str; } }

web.xml页面代码:

 
 
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <servlet> <servlet-name>myServlet</servlet-name> <servlet-class>xyz.Servlet.WordServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>myServlet</servlet-name> <url-pattern>/WordServlet</url-pattern> </servlet-mapping> </web-app>

 

 

 

 

 

 

 

 

展开阅读全文

没有更多推荐了,返回首页