欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

123

程序员文章站 2024-03-15 15:49:48
...
package cn.net.nit.mvcDemo.servlet;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.net.nit.mvcDemo.bean.ArticlePublish;
public class Controller extends HttpServlet {
private static final long serialVersionUID = 1L;
public Controller() {super();}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
}
	// 在Servlet中并没有直接处理所提交的请求,而是把请求的处理推后到ArticlePublish类中,
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
		// 获取JSP页面传入的操作参数,根据参数决定执行哪些操作,跳转到哪个页面。
	String commond = request.getParameter("commond");
	if (commond.equals("publish")) {
			// 获取JSP页面表单的内容
			String title = request.getParameter("title");
			String content = request.getParameter("content");
			// 如果出现中文乱码,要进行转码
			title = new String(title.getBytes("iso-8859-1"), "utf-8");
			content = new String(content.getBytes("iso-8859-1"), 	"utf-8");
			// 调用发布文章的方法,保存文章
			ArticlePublish ap = new ArticlePublish();
			boolean flag = ap.publish(title, content);
			// 将需要显示的内容放在request里面,
			request.setAttribute("title", title);
			request.setAttribute("content", content);
			// 跳转到文章显示页面
			RequestDispatcher dispatcher = null;
			if (flag == true)
				dispatche= getServletContext().getRequestDispatcher(
						"/show.jsp");
			else
				dispatcher=getServletContext().getRequestDispatcher(
						"/wrong.jsp");
			dispatcher.forward(request, response);
		} else {
			System.out.println("其他操作!!!");
		}
	}
}