JSP+Servlet实现留言板(简陋)

今天参考着图书馆借来的JSP程序设计教程(第二版)完成了简陋留言板的制作,书样如下:JSP程序设计教程(第二版)
期间遇到了以下问题:
1.show.jsp代码第1行加载Word类出错
show.jsp代码
2.以上show.jsp代码3、4行运行出错的编码问题,用utf-8则错误页面显示的文字乱码,换为gb2312反而文字显示正常,但总之都是页面报错无法正常显示留言内容

但第二天再次运行程序发现留言内容已经可以正常显示且页面已经不报错,此时1、2问题均未再出现,无论show.jsp代码2、3行用utf-8还是gb2312均无中文乱码且显示正常。具体原因未详。下附完整代码

  1. AddWord.java
package Servlet;

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

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

import Class.Word;

/**
 * Servlet implementation class AddWord
 */
public class AddWord extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddWord() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		String author=request.getParameter("author");
		String title=request.getParameter("title");
		String content=request.getParameter("content");
		String time=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
		Word word=new Word(author, title, content, time);
		HttpSession session=request.getSession();
		ServletContext context=session.getServletContext();//通过session对象获取应用上下文
		@SuppressWarnings("unchecked")
//		ArrayList<Word> wordlist=(ArrayList<Word>) session.getAttribute("wordlist");
		ArrayList<Word> wordlist=(ArrayList<Word>) context.getAttribute("wordlist");
		if(wordlist==null){
			wordlist=new ArrayList<Word>();
		}
		wordlist.add(word);
		//session.setAttribute("wordlist", wordlist);
		context.setAttribute("wordlist", wordlist);
		response.sendRedirect("Show.jsp");
//		System.out.println(author);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

  1. Word.java
package Class;

public class Word {
	private String author;
	private String title;
	public Word(String author, String title, String content, String time) {
		super();
		this.author = author;
		this.title = title;
		this.content = content;
		this.time = time;
	}
	private String content;
	private String time;
	public String getAuthor() {
		return author;
	}
	public void setAuthor(String author) {
		this.author = author;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	public String getTime() {
		return time;
	}
	public void setTime(String time) {
		this.time = time;
	}
	
}

  1. Index.jsp
<%@ 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>留言板</title>
</head>
<body>
<form action="AddWord" method="post"">&nbsp;&nbsp;&nbsp;&nbsp;者:<input name="author" type="text" size="31" /><br>
	留言标题:<input name="title" type="text" size="31" /><br>
	留言内容:<textarea name="content" rows="7" cols="30"></textarea><br>
	<p>
	<input type="submit" value="提交">
	<input type="reset" value="重置">
	<a href="Show.jsp">查看留言</a>
</form>
</body>
</html>
  1. Show.jsp
<%@page import="Class.Word"%>
<%@page import="java.util.ArrayList"%>
<%@ 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>查看留言</title>
</head>
<body>
<% 
	//request.setCharacterEncoding("utf-8");
	//ArrayList wordlist=(ArrayList)session.getAttribute("wordlist");
	ArrayList wordlist=(ArrayList)application.getAttribute("wordlist");
	if(wordlist==null||wordlist.size()==0){
		out.print("没有留言可显示");
	}
	else{
		for(int i=wordlist.size()-1;i>=0;i--){
			Word word=(Word)wordlist.get(i);
%>
留言者:<%=word.getAuthor() %><p>
留言时间:<%=word.getTime() %><p>
留言标题:<%=word.getTitle() %><p>
留言内容:<textarea rows="7" cols="30"><%=word.getContent() %></textarea><p>
<a href="Index.jsp">我要留言</a>
<hr width="100%">
<%
		}
	}
%>
</body>
</html>
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值