File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSP and the fly likes MVC using JSP, Servlets and Java classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "MVC using JSP, Servlets and Java classes" Watch "MVC using JSP, Servlets and Java classes" New topic

MVC using JSP, Servlets and Java classes

Johnnie Smith

Joined: May 02, 2012
Posts: 23
Hello I am trying to implement the MVC architecture using JSP(view), Servlet(Controller) and Java classes(Model).

I have named my Application as First_App.In this application there are 2 JSPs namely First.jsp and Second.jsp.

To implement the controller, I have written a servlet whose job is to analyse a request for any page in this application and perform the following tasks:

1. Create an object of the model class,
2.Call one of its methods that will fetch data from a database,
3. Create an instance of a bean
4. Call its respective setter methods to set the attribute values with the retrieved data
5. Set the bean as a request attribute.

After doing all this, it uses the following code:




to forward the request to either of the JSPs depending on some criteria.

The web.xml file contains the following entry:


The problem that I am facing is that data does not get populated on the JSP. On the JSP, I retrieve the brean from the request and display its attribute values.

Please note that I want to map any request for any resource within this web application to the servlet because I want the servlet to forward it to the particular JSP.

I believe that this is the essence of MVC. But what happens is that the forwarded request again goes through the controller servlet

and again the processing is done.

How do I code the controller servlet in such a manner that a user request passes through it but the forwarded request from within the servlet's post method is ignored

and the concerned JSP is displayed?

Version of Tomcat: 6.26
Servlet: 2.5

Ashwini Kashyap
Ranch Hand

Joined: Aug 30, 2012
Posts: 90

For forwarding request parameter to JSP page from within servlet, you need to set that param in request object using:

Also, map your actions like:
1) From JSP:
<form action="user" method="post">
2) Make appropriate entry in web.xml like:

3) In Servlet, override doPost()/service(), write your business logic and forward it to other JSP accordingly.

Ashwini Kashyap |

I agree. Here's the link:
subject: MVC using JSP, Servlets and Java classes
It's not a secret anymore!