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

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

nova http://www.jdon.com Oct 14, 2004 10:51 AM 回复此消息 回复

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

谢谢!


Re: 一个小的WEB项目中的实现方法讨论 发表时间: Oct 19, 2004 9:51 AM
回复此消息回复
发表人: banq    发表文章: 3807 / 来  自: 上海 / 注册时间: 2002-08
这其实是一个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。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值