mvc 中的静态或非静态动作?

发布于 2024-12-29 03:48:03 字数 2042 浏览 2 评论 0原文

我想问你关于mvc的事。它是如何运作的。 简单示例(我不使用任何框架)

所以,这是控制器(Servlet)中的

protected void doGet(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    processRequest(request, response);
}

protected void doPost(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    processRequest(request, response);
}

private void processRequest(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    String page = null;
    AbstractCommand action;

    action = ActionFactory.getAction(request);// get command from factory
    page = action.execute(request, response); 
    RequestDispatcher dispatcher = getServletContext()
            .getRequestDispatcher(page);
    dispatcher.forward(request, response);
}

:对于操作,我们创建一个公共接口(策略模式):

public interface AbstractAction {

    public String execute(HttpServletRequest request, HttpServletResponse response);
}

简单操作(示例):

public class HelloAction implements AbstractAction {

    @Override
    public String execute(HttpServletRequest request,
            HttpServletResponse response) {
        //....(some actions to modify request)
        String page = "/main.jsp";
        return page;
    }   
}

现在,我们的工厂:

public class ActionFactory {

    private enum Actions{
        HELLO;
    }

    public static AbstractAction getAction(HttpServletRequest request){
        String action = request.getParameter("action");//take parameter from jsp
        Actions actionEnum = Actions.valueOf(action.toUpperCase());
        switch (actionEnum) {
        case HELLO:
            return new HelloAction(); 
        }
    }
}

我们来到了这个地方我很困惑。 Servlet 只初始化一次,并且对于所有请求都只初始化一次。只是将请求转发到我们修改请求或响应的操作。但是,我们为每个请求创建该类的新实例。这里会发生内存溢出吗!?或不?

我们可以将这些操作设为静态(静态方法,一个用于所有请求)吗?如果两个请求同时出现会发生什么?
对此你有何看法,欢迎分享你的经历。

PS抱歉英语不好。

i want to ask you about mvc. How it works. So, this is simple example(I don't use any frameworks)

in Controller(Servlet):

protected void doGet(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    processRequest(request, response);
}

protected void doPost(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    processRequest(request, response);
}

private void processRequest(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    String page = null;
    AbstractCommand action;

    action = ActionFactory.getAction(request);// get command from factory
    page = action.execute(request, response); 
    RequestDispatcher dispatcher = getServletContext()
            .getRequestDispatcher(page);
    dispatcher.forward(request, response);
}

for action we create a common interface(Strategy pattern):

public interface AbstractAction {

    public String execute(HttpServletRequest request, HttpServletResponse response);
}

Simple Action(Example):

public class HelloAction implements AbstractAction {

    @Override
    public String execute(HttpServletRequest request,
            HttpServletResponse response) {
        //....(some actions to modify request)
        String page = "/main.jsp";
        return page;
    }   
}

And now, our factory:

public class ActionFactory {

    private enum Actions{
        HELLO;
    }

    public static AbstractAction getAction(HttpServletRequest request){
        String action = request.getParameter("action");//take parameter from jsp
        Actions actionEnum = Actions.valueOf(action.toUpperCase());
        switch (actionEnum) {
        case HELLO:
            return new HelloAction(); 
        }
    }
}

We came to the place where I am in confused. Servlet is initialized only once, and only one for all requests. Just forwards requests to the actions where we modify request or response. But, we create NEW instance of the class for every request. Here can occur memory overflow!? Or not?

Can we make these actions static(static method, one for all request)? If two requests come at the same time what will happen to them?
What do you think about this, please share your experience.

P.S. sorry for bad english.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

七度光 2025-01-05 03:48:03

单例模式如何获取 Action 类的实例?

  1. 只需在 AbstractAction 中添加一些 abstact getInstance() 方法即可。
  2. 使每个实现都提供自己的实例。
  3. 在每个实现类中,使用单例模式,以便只存在一个实例。
  4. 确保没有操作类存储与特定请求相关的任何数据。

How about Singleton pattern to get the instance of the Action class ?

  1. Just add some abstact getInstance() method in AbstractAction.
  2. Make every implementation provide its own instance.
  3. In every implementation class, use Singleton pattern so that only one instance exists.
  4. Make sure no action class stores any data related to a specific request.
2025-01-05 03:48:03

据我了解jsp,整个事情是无状态的,如果你通过http请求访问servlet,servlet将在一个新实例中创建。
通过 .forward() 离开 servlet 后,它将被垃圾回收释放。

2,3,...,n 个请求 = 2,3,...,n 个 servlet。

通过转发到jsp,从jsp访问servlet的唯一方法是一个新的http请求=新的servlet。 (将转到 doPost 方法)

As i understood the jsp, the whole thing is stateless, if u access the servlet by http request, the servlet will be created in a new instance.
After leaving the servlet by .forward(), it will be released by garbage collection.

2,3,...,n requests = 2,3,...,n servlets.

by forwarding to a jsp, the only way to access the servlet from jsp is a new http request = new servlet. ( will move to the doPost method)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文