禁止访问Servlet

问题描述 投票:0回答:1

我正在使用JavaEE开发WebApp,并使用servlet来测试一些东西。

[当前,将我的WebApp设置为当我转到本地URL localhost:8080/myApp/test时,我可以运行test Servlet。

我的计划是将项目部署到Web上,但我想禁用Servlet,但是不删除它。我的意思是,如果将来我通过URL www.myNewWeb.com/test访问我的远程服务器,我希望它抛出错误或什么都不做。

我该怎么做?

servlets java-ee
1个回答
1
投票

这里有很多可能的选项:

选项1

删除映射(@WebServlet中的注释web.xml或URL映射条目)。在这种情况下,任何尝试调用此servlet的操作都会以您选择的JEE容器生成的错误结尾。它将尝试将servlet映射到URL,显然会失败并引发异常

此方法的明显缺点是,您需要更改部署配置,并且如果您想在该servlet应该起作用的另一个环境中运行相同的工件,则将无法执行。

选项2

创建某种配置,将该配置与您的应用程序一起加载。在doGet(仅出于示例目的)方法中,执行以下操作:

  public void doGet(request, response) {
     if(config.isTestServletEnabled()) { // this is where the data gets read from configuration that I've talked about before
        // do your regular processing here
     }
     else {
        // this will happen when the servlet should not be activated 
        // throw an exception, return HTTP error code of your choice, etc
      } 
  } 

这种方式没有上面我解释的第一种方法的缺点,但是涉及一些要编写的代码。

© www.soinside.com 2019 - 2024. All rights reserved.