浏览 3214 次
锁定老帖子 主题:对HttpServlet的扩展与设计
精华帖 (0) :: 良好帖 (0) :: 新手帖 (10) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2008-09-01
这几天在学习Extjs中,需要用到后台与前台作数据交互,所以用了简单的servlet作后台业务处理,基于此,扩展了一下servlet的设计. 下面是JAVA代码:
package com.zwr.app.servlet; import java.io.IOException; import java.util.ArrayList; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.zwr.app.model.WebSiteModel; import com.zwr.app.util.RequestParamUtil; /** * extjs前台AJAX与后台交互测试Servlet * * @author zhu wei rong * @since 2008-08-25 */ @SuppressWarnings( { "serial", "unused" }) public class UserServlet extends HttpServlet { private final String encoding = "utf-8"; private HttpServletRequest request; private HttpServletResponse response; protected void doBusiness() throws ServletException, IOException { String method = request.getParameter("method"); if (method.equals("save")) { doSave(); } else if (method.equals("list")) { doList(); } } /** * 测试grid分页显示数据,所有数据以json格式在JSP页面显示 */ private void doList() throws ServletException, IOException { int start = RequestParamUtil.getIntParam(request, "start", 0); int limit = RequestParamUtil.getIntParam(request, "limit", 0); String sort = RequestParamUtil.getParam(request, "sort", null); String dir = RequestParamUtil.getParam(request, "dir", null); System.out.println(start + "\t" + limit + "\t" + sort + "\t" + dir); List<WebSiteModel> list = new ArrayList<WebSiteModel>(); for (int i = 1; i <= 10; i++) { WebSiteModel model = new WebSiteModel(); model.setId(i); model.setName("站点_" + i); model.setUrl("url--" + i); list.add(model); } int totalCount = list.size(); // 总数 request.setAttribute("count", totalCount); request.setAttribute("entites", list.subList(start, (start + limit) < totalCount ? (start + limit) : totalCount)); forward("/scripts/app/data.jsp"); } private void doSave() throws ServletException, IOException { String name = request.getParameter("name"); String password = request.getParameter("password"); System.out.println("前台AJAX传递过来的参数:\t" + name + "\t" + password); forward("/"); } @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding(encoding); response.setCharacterEncoding(encoding); this.request = request; this.response = response; doBusiness(); } @Override protected void doPost(HttpServletRequest arg0, HttpServletResponse arg1) throws ServletException, IOException { this.doGet(arg0, arg1); } public void forward(String url) throws ServletException, IOException { getServletContext().getRequestDispatcher(url).forward(request, response); } }
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2008-09-01
看似有很多问题:
1.一个模块一个servlet,配置起来太麻烦,建议你用spring 2.private HttpServletRequest request;在servlet里面定义变量,这个应该有多线程问题 3.Ext与后台交互是通过ajax,request.setAttribute,然后forward明显是strtus的习惯,你forward到jsp之后怎么办呢?还是要转换成josn,建议你直接在doList里面输出json,省下一个jsp文件 |
|
返回顶楼 | |
发表时间:2008-09-01
连struts都拼错了,失败
|
|
返回顶楼 | |
发表时间:2008-09-01
Struts中有个dispatch的概念,为什么不只用这种方式呢?你的这种方式耦合度太高了,很难有效的扩展。我只做过Struts,我相信几乎所有的web MVC框架都有这个功能,而且做的更有弹性和扩展性。而且实在没看出来doSave是做什么用的,而且如果目的是扩展HttpServlet,这个方法至少应该是protected的吧。
|
|
返回顶楼 | |
发表时间:2008-09-01
有多线程问题
|
|
返回顶楼 | |
发表时间:2008-09-01
最少应该包装一下Servlet,或者做一个简单的Dispatcher。否则Servlet也太多了。
|
|
返回顶楼 | |
发表时间:2008-09-01
那个web。xml就够你忙乎的
|
|
返回顶楼 | |