gpt4 book ai didi

Java 继承设计与 HttpServlet - 会导致问题吗

转载 作者:行者123 更新时间:2023-12-02 13:34:31 25 4
gpt4 key购买 nike

我需要一些帮助,我的意思是对这个继承设计的意见。

我创建了Controller.class,它扩展了HttpServletAjaxController.class 扩展了 Controller.class ,并且 PostBackController.class 也同样如此。

正如你们所看到的,SomeServlet.classSomeServlet1.class 扩展了 < em>Controller.classAjaxController.class 分别。

如果我继续使用此设计来创建更多我想要创建为 servlet 的类,例如扩展 Controller.classs 的 Search.class 等等。

此代码是否会在 Web 容器中产生任何问题,因为我不是直接继承(扩展)HttpServlet,而是通过层次结构。

如果您发现此设计有任何问题,请告诉我。谢谢

Controller .java

 package utility;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.hibernate.Session;

public abstract class Controller extends HttpServlet {

@Override
final protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doProcess(req, resp);
}

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doProcess(req, resp);
}

public void onError(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.sendError(500);
}

final private void doProcess(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Session session = Utils.openSession();
session.beginTransaction();
PrintWriter out = resp.getWriter();
try {
process(req, resp, session, req.getSession(), out);
session.getTransaction().commit();
session.close();
} catch (Exception e) {
session.getTransaction().rollback();
session.close();
if (showErrorLog()) {
e.printStackTrace();
}
this.onError(req, resp);
} finally {
out.close();
}
}

public boolean showErrorLog() {
return false;
}

public abstract void process(HttpServletRequest req, HttpServletResponse resp, Session session, HttpSession httpSession, PrintWriter out) throws Exception;
}

AjaxController.java

package utility;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public abstract class AjaxController extends Controller {

@Override
public void onError(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
PrintWriter out = resp.getWriter();
out.print("error");
out.close();
}

@Override
final protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.onError(req, resp);
}

}

PostBackController.java

package utility;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public abstract class PostBackController extends Controller{

@Override
final protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.onError(req, resp);
}
}

SomeServlet.java

    package utility;

import java.io.PrintWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.hibernate.Session;

@WebServlet("/someurl")
public class SomeServlet extends Controller{

@Override
public void process(HttpServletRequest req, HttpServletResponse resp, Session session, HttpSession httpSession, PrintWriter out) throws Exception {
//does some work
//includes a jsp for view
}

}

如果我创建更多这样的类,它会导致网络服务器/容器出现任何问题吗?SomeServlet1.java

package utility;

import java.io.PrintWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.hibernate.Session;

@WebServlet("/someurl1")
public class SomeServlet1 extends PostBackController{

@Override
public void process(HttpServletRequest req, HttpServletResponse resp, Session session, HttpSession httpSession, PrintWriter out) throws Exception {
//does some work
//USED TO HANDLE AJAX REQUEST
}

}

最佳答案

Will this code create any problem in the web Container as I am not directly Inheriting (extending) HttpServlet but going through a hierarchy?

如果您的 servlet 不直接扩展 HttpServlet,只要 HttpServlet 作为继承层次结构中的父类之一存在,就没有问题。

因为您创建了太多的 servlet 类/实例,所以真正的问题是您违反了 FrontController 设计模式(所有流行的框架(如 Spring、Struts 等)都遵循该模式)。 ),你可以看here在这个模式上。

FrontController 设计模式的思想是,一个 servlet 充当所有请求的中心点,然后该请求将被委托(delegate)给相应的 Controller/Action 类(不是 servlet)。

以下是前端 Controller 模式的主要优点(取自上面的链接):

Common system services processing completes per request. For example, the security service completes authentication and authorization checks.

A centralized point of contact for handling a request may be useful, for example, to control and log a user's progress through the site.

Logic that is best handled in one central location is instead replicated within numerous views.

Decision points exist with respect to the retrieval and manipulation of data.

更新:

My Main worry is that will it cause any issue when 40 classes extend Controller by giving the implementation of the process method like SomeServlet.java. Not extending HttpServlet directly, will it cause any problems?

这是一个糟糕的设计,您将失去前端 Controller 的上述好处。

关于Java 继承设计与 HttpServlet - 会导致问题吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43075292/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com