0

I have a very simple servlet that should handle requests to the root of my app. It is deployed onto IBM Websphere.

Basically, I want to return index.jsp each time user requests my app like:

Here is the code:

@WebServlet(urlPatterns = {"", "/"})
public class MainPageController extends HttpServlet {

    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getServletContext().getRequestDispatcher("/index.jsp").forward(req, resp);
    }
}

It works as it should (I'm getting the JSP), but when JSP page is returned - JSP requests JS, CSS files and those requests (example http://localhost:8888/myapp/js/main.js) are caught by that servlet.

Why my servlet, which should handle only / also handles other requests (like /js/main.js)?

Nothing special in web.xml. No servlet mappings, no other servlets, no welcome file list declaration.

ikos23
  • 4,879
  • 10
  • 41
  • 60
  • you have to restrict your urlPattern to `urlPatterns = { "/myapp"}` or something like that because right now you have provided only "/" mns every request will be handled. – Akshay jain May 07 '18 at 12:23
  • @Akshayjain `myapp` is a context root. are you sure I need to add it my url patterns ? – ikos23 May 07 '18 at 12:25
  • @Akshayjain I know I could add something like `/home` and then it will be triggered for `http://localhost:8888/myapp/home`, but that's not my case. I need to request only root of my app. – ikos23 May 07 '18 at 12:29
  • what release/maintenance level? – covener May 07 '18 at 12:34
  • for your root page you dont need this doget url pattern method it will automatically search the index.jsp page or in the web.xml you can add this code ` index.jsp ` [https://stackoverflow.com/questions/14648167/how-to-configure-welcome-file-list-in-web-xml/] – Akshay jain May 07 '18 at 12:37
  • @Akshayjain and again that's not my case. I don't want it to be returned as a welcome file. I need to do some biz logic and then return `index.jsp`, that's why I need a servlet – ikos23 May 07 '18 at 13:03
  • @Akshayjain what shouldI do if user requests my app with query params like `http://localhost:8888/myapp/?someParam=someValue` and I need to parse them and process first. – ikos23 May 07 '18 at 13:05
  • In your jsp you could then just do: `if (request.getParameter("someParam") != null) { //do business logic}` – M. Broz May 07 '18 at 14:32
  • @M.Broz I think using that stuff (<% ... %>) is a very bad practice, – ikos23 May 07 '18 at 16:08
  • With the pattern you have, it will respond to all requests so the behaviour is what is expected even though that is not what you want. This looks like what you are looking for: https://stackoverflow.com/questions/13521946/how-to-prevent-static-resources-from-being-handled-by-front-controller-servlet-w – DanielBarbarian May 08 '18 at 06:58

1 Answers1

0

Use just the following urlPattern:

@WebServlet(urlPatterns = {""} )

it will only handle requests to your app context root, not css or any other subfolders. Dont add any patterns with / to it.

Gas
  • 17,601
  • 4
  • 46
  • 93