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

Posted by A.S al-shammari on Stack Overflow See other posts from Stack Overflow or by A.S al-shammari
Published on 2010-04-04T18:35:09Z Indexed on 2010/04/04 18:43 UTC
Read the original article Hit count: 337

Filed under:
|
|

I'm developing a JSP/Servlets web app (no frameworks). I want to use MVC pattern. I designed my project like this :

  1. Controller :a servlet that reads a request, extracts the values,communicates with model objects and gives information to a JSP page.
  2. View : JSP Pages.
  3. Model : Java Classes / Java Beans .. etc .

The problem :

Index.jsp is the starting point (default page) in my web site. So, the Index.jsp becomes the controller to parse the request! .For example , the following request :

index.jsp?section=article&id=10

parsed in index.jsp as following :

<div class="midcol">
<!-- Which section? -->
<%String fileName = request.getParameter("section");
if (fileName == null) {
fileName = "WEB-INF/jspf/frontpage.jsp";
} else {
fileName = "WEB-INF/jspf/" + fileName + ".jsp";
}
%>
<jsp:include page='<%= fileName%>' />
</div>

Here, I can't force the servlet to be a controller .. because the index.jsp is the controller here since it's the starting point!

Is there any solution to forward the request from index.jsp to the servlet and then go back to index.jsp ?
Or any solution that achieves the MVC goal - the servlet should be the controller - ?

I'm thinking of making a FrontPageController servlet as default page instead of index.jsp ! but I don't know if it's a perfect idea ?!

© Stack Overflow or respective owner

Related posts about jsp

Related posts about servlets