0

We had a monolithic application where there was a dispatcher servlet handling all the requests. Later we adopted web-fragment to have more modularity in app. But with this approach we are having multiple dispatcher servlet. one for each web-fragment. So now we have wf1-servlet(/wf1/*), wf2servlet(/wf1/*) and main-servlet in web.xml(/*) [we can't avoid this as there are lot of urls out there which can't be namespaced].

Is there anyway to order the execution of the servlet so that main-servlet get picked in the last as this works on /*?

user207421
  • 305,947
  • 44
  • 307
  • 483

1 Answers1

0

Use <url-pattern> tag to map a specific url to specific servlets:

<servlet>
 <servlet-name>wf1servlet</servlet-name>  //servlet name
 <servlet-class>package.Wf1servlet</servlet-class>  //servlet class
</servlet>
<servlet-mapping>
 <servlet-name>wf1servlet</servlet-name>   //servlet name
 <url-pattern>/wf1/*</url-pattern>  //how it should appear
</servlet-mapping>

you can also inside the the servlet you can forward a request to another specific request like this:

URL url = new URL("http://otherserver:otherport/url");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();

//set http method if required
connection.setReqeustMethod("POST");

//set request header if required
connection.setRequestProperty("header1", "value1");

//check status code
if(connection.getResponseCode() == 200){

InputStream is = connection.getInputStream();
//transfer is to the required output stream
} else {
 //write error
}

also I think the mapping order of servlets are their came order in the web.xml file, here and also here you can see something useful about it

Community
  • 1
  • 1
void
  • 7,760
  • 3
  • 25
  • 43