Ommadawn
Ommadawn

Reputation: 2730

Disable access to a Servlet

I'm developing a WebApp using JavaEE and I use a servlet to test some stuff.

Currently, my WebApp is set as when I go to my local url localhost:8080/myApp/test , I can run my test Servlet.

My plan is to deploy my project to a Web and I want to disable the Servlet, but not delete it. I mean, if in the future I visit my remote server via URL www.myNewWeb.com/test , I would like it throws an error od do nothing.

How could I do that?

Upvotes: 0

Views: 405

Answers (1)

Mark Bramnik
Mark Bramnik

Reputation: 42431

There are many possible options here:

Option 1

Remove the mapping (annotation @WebServlet or url mapping entry in web.xml). In this case, any attempt to call this servlet will end with an error generated by the JEE container of your choice. It will try to map the servlet to URL, will obviously fail and throw an exception

The obvious drawback of this method is that you need to change the deployment configuration and if you'll want to run the same artifact in another envrironment where this servlet should work you won't be able to do so.

Option 2

Create some kind of configuration, load this configuration along with your application. In the doGet (just for the sake of example) method do something like this:

  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
      } 
  } 

This way doesn't have a drawback of the first method that I've explained above, however involves some code to be written.

Upvotes: 2

Related Questions