Question

The use case I am trying to implement.
Display different content based on the {{random_string}} in the URL path.
Users will see different content based on the {{random_string}} that the URL contains.

eg:

www.example.com/{{random_string}}/index.jsp

The URLS will look like these below. ( They include random characters before the JSP)

www.example.com/xc/index.jsp
www.example.com/2b/index.jsp
www.example.com/43/index.jsp

My question

  1. How do I setup nginx and tomcat to be able to support the {{random_string}} in the URL without throwing 404?

My Current Environment/Setup (this works fine)

Nginx along with Tomcat. Requests that come to nginx are then redirected to tomcat to access ROOT.war e,g - www.example.com/index.jsp

Was it helpful?

Solution

You shouldn't have to change anything in Nginx or Tomcat config. What you could do is to create a servlet that will intercept the requests and extract the {{random_string}} before forwarding to the JSP. Here are the basic steps:

1) Create a servlet with a URL pattern of /* so that all requests go to it.

2) In the servlet's doGet() method, use request.getPathInfo() to retrieve the URL path and parse it to extract the {{random_string}}.

3) Use request.setAttribute() to set attributes for the data you want to display in the JSP page.

4) Forward the request to the JSP using a RequestDispatcher, e.g.:

RequestDispatcher dispatcher = request.getRequestDispatcher("/index.jsp");  
dispatcher.forward(request, response);  

5) In the JSP, use the request attributes that you have set in step 3 to display the content.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top