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文件就不贴了