当前位置:首页 > 开发 > 编程语言 > Servlet > 正文

Servlet基类AbstractServlet

发表于: 2010-05-23   作者:酷的飞上天空   来源:转载   浏览:
摘要: AbstractServlet类主要是对一些常用的方法进行扩展 内容如下 public class AbstractServlet extends HttpServlet { private static final long serialVersionUID = 1L; private static String resultPage = "result.jsp&quo

AbstractServlet类主要是对一些常用的方法进行扩展

内容如下

public class AbstractServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	private static String resultPage = "result.jsp";

	/**
	 * 
	 * @return /tmp 的实际路径
	 */
	public String getTmp() {
		return getServletContext().getRealPath("/tmp");
	}

	/**
	 * 绑定数据到HttpServletRequest,使用setAttribute方法实现
	 * 
	 * @param request
	 * @param key
	 * @param data
	 */
	public void BindData(HttpServletRequest request, String key, Object data) {
		request.setAttribute(key, data);
	}

	/**
	 * 此方法仅是BindData的简略版,key为errorMsg。多次调用此方法则只记录最后一次的数据。
	 * 绑定数据到HttpServletRequest,使用setAttribute方法实现,
	 * 
	 * @param request
	 * @param data
	 */
	public void BindErrorData(HttpServletRequest request, Object data) {
		BindData(request, "errorMsg", data);
	}

	/**
	 * 转发到/WEB-INF/jsp/目录下的jspName文件
	 * 
	 * @param jspPath
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	public void forward(String jspName, HttpServletRequest req,
			HttpServletResponse resp) throws ServletException, IOException {
		req.getRequestDispatcher("/WEB-INF/jsp/" + jspName).forward(req, resp);
	}

	/**
	 * 转发到result.jsp页面
	 * 
	 * @param req
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	public void forwardResultPage(HttpServletRequest req,
			HttpServletResponse resp) throws ServletException, IOException {
		req.getRequestDispatcher("/WEB-INF/jsp/" + resultPage).forward(req,
				resp);
	}

	/**
	 * 重定向到jspPath页面,仅限本站页面,jspName为相对于当前应用的路径名。否则请用HttpServletResponse进行操作
	 * 
	 * @param jspPath
	 * @param resp
	 * @throws ServletException
	 * @throws IOException
	 */
	public void sendRedirect(String jspName, HttpServletRequest req,
			HttpServletResponse resp) throws ServletException, IOException {
		resp.sendRedirect(req.getContextPath() + "/" + jspName);
	}

	/**
	 * 将内容输出给客户端,作为Ajax调用的返回字符串。
	 * 
	 * @param resp
	 * @param content
	 * @throws ServletException
	 * @throws IOException
	 */
	public void print(HttpServletResponse resp, String content)
			throws ServletException, IOException {
		PrintWriter w = resp.getWriter();
		w.write(content);
	}

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doGet(req, resp);
	}

}

 具体方法不再累述,都有注释

 

贴出一个使用的例子,这个是blog主页的后台servlet

public class HomeServlet extends AbstractServlet {
	private static final long serialVersionUID = 1L;

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		//用户信息
		User user = BlogFilter.getBlogContext().getUser();
		ArticleDao articleDao = DaoFactory.getArticleDao();
		//主页的5篇最近发表(更新)的文章
		List<Article> articleList = articleDao.getLaterArticle(5);
		//每篇文章对应的阅读数
		List<Integer> articleSeeNum = new ArrayList<Integer>();
		//每篇文章所对应的评论数
		List<Integer> commentList = new ArrayList<Integer>();
		//每篇文章所对应的分类
		List<Category> categoryList = new ArrayList<Category>();
		//取得外站的连接列表
		List<OutLink> outLinkList = DaoFactory.getOutLinkDao().getListOutLink();
		//显示今天的日期
		String time = DateFormateUtil.getStringByDate(new Date(), "yyyy年MM月dd日");
		//显示星期几
		String dayOfWeek = DateFormateUtil.getDayOfWeek(new Date());
		//访问量
		int num = BlogFilter.getTotalNum();
		
		CommentDao commentDao = DaoFactory.getCommentDao();
		ArticleSeeDao articleSeeDao = DaoFactory.getArticleSeeDao();
		CategoryDao categoryDao = DaoFactory.getCategoryDao();
		for(int i=0;i<articleList.size();i++){
			articleSeeNum.add(i, articleSeeDao.getReadNumber(articleList.get(i).getArticleId()));
			categoryList.add(i, categoryDao.getCategory(articleList.get(i).getCategoryId()));
			commentList.add(i, commentDao.getNumOfCommentForArticle(articleList.get(i).getArticleId()));
		}
		
		BindData(req, "user", user);
		BindData(req, "articleList", articleList);
		BindData(req, "articleSeeNum", articleSeeNum);
		BindData(req, "commentList", commentList);
		BindData(req, "categoryList", categoryList);
		BindData(req, "outLinkList", outLinkList);
		BindData(req, "time", time);
		BindData(req, "dayOfWeek", dayOfWeek);
		BindData(req, "num", num);
		forward("home.jsp", req, resp);
 	}
}

 

至于home.jsp文件就不贴了

Servlet基类AbstractServlet

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

编辑推荐
#include <iostream> #include <cstring> using namespace std; class Teacher { publi
/* * 程序的版权和版本声明部分 * Copyright (c)2012, 烟台大学计算机学院学生 * All rightsreserve
/* * 程序的版权和版本声明部分 * Copyright (c)2012, 烟台大学计算机学院学生 * All rightsreserve
先来对比一下用虚基类和不用虚基类的区别 #include<iostream> using namespace std; class Ba
/* * Copyright (c)2013, 烟台大学计算机学院学生 * All rightsreserved. * 文件名称: array.cpp *
虚基类是用关键字virtual声明继承的父类,即便该基类在多条链路上被一个子类继承,但是该子类中只包
/*烟台大学计算机学院学生 *All right reserved. *文件名称:体验虚基类 *作者:杨飞 *完成日期:201
1. 先建一个普通的窗体,until1 2. 先把类实现基类, 并需要实现基类需要继承的方法, 可以先不用再
//派生类成员函数对基类成员函数的覆盖 #include "stdafx.h" #include<iostream> class A { p
1. 顺序 当创建一个派生类的对象时,系统首先自动创建一个基类对象,也就是说,在调用派生类构造函
版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号