一个小的WEB项目中的实现方法讨论

nova1980 2004-10-14 11:02:37
最近对一个别人的WEB项目进行维护,看到这样的实现方法:
1.只有一个Controller的servlet 类
2.一个Service接口
3.一些实现Service接口的类

Controller类负责进行控制,动态产生业务逻辑的类的实例(所有的类需要实现Service接口),然后通过
httpservletrequest.setAttribute("USERLIST", userList);向WEB端赋值,

具体的可以参考部分代码:
Controller 类(extends HttpServlet )

protected void doPost(
HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
//service name,example for packagename.ServiceName
String serviceName = request.getParameter(Constant.SERVICE);
if (serviceName == null)
throw new ServletException("There isn't service parameter![?service=]");
String targetName = request.getParameter(Constant.TARGET);
//the targeted file name,example for /fileName.jsp
if (targetName == null)
throw new ServletException("There isn't target parameter![?target=]");
ServletContext servletcontext = getServletContext();
try {
//TODO:hashmap to reduce the generated instance?
Class serviceClass = Class.forName(serviceName);
Service service = (Service) serviceClass.newInstance();
service.execute(request, response, servletcontext);
} catch (ClassNotFoundException classnotfoundexception) {
throw new ServletException(classnotfoundexception.getMessage());
} catch (IllegalAccessException illegalaccessexception) {
throw new ServletException(illegalaccessexception.getMessage());
} catch (Exception exception) {
throw new ServletException(exception.getMessage());
}
forward(request, response, targetName);
}


Service 接口


public interface Service {
public abstract void execute(
HttpServletRequest httpservletrequest,
HttpServletResponse httpservletresponse,
ServletContext servletcontext)
throws Exception;
}


一个实现service的类(相当于业务类)
public class StartService implements Service {

public void execute(
HttpServletRequest httpservletrequest,
HttpServletResponse httpservletresponse,
ServletContext servletcontext)
throws Exception {
//test data
List userList = new ArrayList();
httpservletrequest.setAttribute("USERNAME", "TestUser");
httpservletrequest.setAttribute("USERLIST", userList);
}

}

JSP 页面文件


<%
String userName=(String)request.getAttribute("USERNAME");
List userList=(List)request.getAttribute("USERLIST");%>



访问的时候:
/service.Controller?service=StartService&target=/StartPage.jsp


我现在想知道的
1.这种实现方案怎么样?为什么这么做,有什么好处
2.产生的service 类对象有没有必要用hashmap保存,以避免产生更多的对象

//TODO:hashmap to reduce the generated instance? Class serviceClass = Class.forName(serviceName);Service service = (Service) serviceClass.newInstance();service.execute(request, response, servletcontext);

3.大家有没有好的类似的方案(只是针对小型的WEB项目,利用Framework的就不要说了)

谢谢!
...全文
121 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
nova1980 2004-10-15
  • 打赏
  • 举报
回复
谢谢 bdsc()

我对servlet 不是了解的,

filter 是干什么用的
nova1980 2004-10-14
  • 打赏
  • 举报
回复
youthy_yy(阿远<广州> 谢谢!

那有没有好一点的方案呢,,
youthy_yy 2004-10-14
  • 打赏
  • 举报
回复
这种方案不好,我就做过一个类似的项目,全部用servlet实现,由一个单一的StaticServlet提供入口,用TrxID区分业务servlet分支,servlet对象统一用Vector保存输出,但最终呢,很难处理复杂的页面,不得不全部用JSP改写!
nova1980 2004-10-14
  • 打赏
  • 举报
回复
UP
Plat 2004-10-14
  • 打赏
  • 举报
回复
学习……
bdsc 2004-10-14
  • 打赏
  • 举报
回复
1.这种实现方案怎么样?为什么这么做,有什么好处
挺好,一定程度的代码重用,通过配置添加功能。

2.产生的service 类对象有没有必要用hashmap保存,以避免产生更多的对象
有,目的是cache对象,可以避免产生太多的对象和产生对象时的资源消耗

3.大家有没有好的类似的方案(只是针对小型的WEB项目,利用Framework的就不要说了)
再好,个人认为也许就复杂了.这实际已经是Framework了。

我觉得youthy_yy(阿远<广州>) 说不好,原因可能是用了servlet写显示层。

考虑你再维护以前的web,那时可能没有filter,用filter更简单和符合标准(如配置)
「已注销」 2004-10-14
  • 打赏
  • 举报
回复
uping.....
nova1980 2004-10-14
  • 打赏
  • 举报
回复
UP

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧