JSP語言做簡易留言板

崔沈鵬發表於2017-11-11

首先奉上檢視:

主介面:

 

檢視留言介面:

 

看一下我前臺後臺:

 

下面開始給大家公佈程式碼並且解釋咯:

首先是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>

 

 

 

 

 

 

 

 

相關文章