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

最近对一个别人的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的就不要说了)

谢谢!

这其实是一个Command模式实现,Service是Command的接口,这个小系统具备了MVC模式基础,我认为是不错的代码。

关于你的两个问题:
>1.这种实现方案怎么样?为什么这么做,有什么好处
这样做主要是使用Command模式的好处,一般在服务器这边接受外界处理命令都是使用Command模式,这是一个习惯了,主要优点有:
1. 条理清楚,扩展容易,增加一个新Service只要继承Service就可以。
2. 性能好,由于服务器端直接将请求提交给具体Service处理,没有其他中间延迟过程。


>2.产生的service 类对象有没有必要用hashmap保存,以避免产生更多的对象

这主要也是提升性能,因为newInstance生成实例比new要慢,所以第一次生成后,保存起来,第二次就没有创建的开销了,当然推荐这里使用对象池Pool来取代hashmap。

对象池使用有三种选择:
1. 使用Apache的Pool组件直接使用。
2. 透过Spring的AOP拦截器间接使用Apache的Pool.
3. 将Service变成EJB的无态Bean。

不错的帖子,,关注中.....

不错,是不错,还差一点,对元数据,美好好利用,这样都是从前段纵向控制,没有利用元数据横向控制。你这个action,和我的很类似,不过没有达到aop,不过也快了。差一点了。呵呵。

另外,若是改用struts等结合jbuiderX等,就可以更"工业化些了"。

本人不才,是否是得产生很多service实现类

这里好象没有收藏的功能哦~
这篇文章真的不错!

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

大部分的MVC框架都是这样实现的,我个人认为即便是小型项目,利用一下成熟框架应该也是首选。
个人推荐: Turbine 或者 WebWork。
我自己做了一个jWorks的扩展,用于兼容Turbine和WebWork框架,有兴趣的朋友可以看看,提点宝贵意见。

http://www.softme.org/jwiki/v_1.2.1/jwiki.jsp?topic=jWorks

找到了

找到了

我门公司用asp做项目时候,也是使用类似的方式。

首先,一个Framework.IBusiness接口中有一个IBusiness_Run方法,

然后,每个叶面对应一个“业务类”,所有的“业务类”都是继承Framework.IBusiness接口,并仅仅实现IBusiness_Run方法,当IBusiness_Run方法运行完成后,叶面的所有信息就呈现出来了。
在这些“业务类”中,request/response等交互对象都是从IBusiness_Run方法的参数中创建的。。。

不知道大家有何感想?

YAWL的工作流引擎也有点儿类似这种的做法。

个人认为既然是自己的项目,就不用着什么命令模式了。按照自己的流程来做一个贴切的,要省力的多。其它程序开发也容易得多。
从最简单的做起。

赋值程序做值对象来完成,页面只是得到。这样向c/s上转化比较容易。如果没有这方面的要求。这个还是原来的更直接简洁。

大家都说的好哈