How to develop JSP/Servlets Web App using MVC pattern?

后端 未结 1 388
隐瞒了意图╮
隐瞒了意图╮ 2020-12-15 11:12

I\'m developing a JSP/Servlet web app (no frameworks). I want to use MVC pattern. I am going to design my project like this:

  1. Controlle
相关标签:
1条回答
  • 2020-12-15 12:01

    Get rid of index.jsp and just let the controller servlet listen on a specific url-pattern of interest. The controller itself should forward the request to the JSP page of interest using RequestDispatcher.

    request.getRequestDispatcher("/WEB-INF/page.jsp").forward(request, response);
    

    Alternatively you can let index.jsp forward or redirect to an URL which is covered by the controller servlet which in turn shows the "default" page (which seems to be frontpage.jsp).

    That said, in a correct MVC approach, you should have no scriptlets in JSP files. Whenever you need to write some raw Java code inside a JSP file which can't be replaced reasonably by taglibs (JSTL and so on) or EL, then the particular Java code belongs in any way in a real Java class, like a Servlet, Filter, Javabean, etcetera.

    With regard to the homegrown MVC approach, you may find this answer and this article useful as well.

    0 讨论(0)
提交回复
热议问题