*
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
Author

MVC using JSP, Servlets and Java classes

Johnnie Smith
Greenhorn

Joined: May 02, 2012
Posts: 22
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:

getServletConfig().getServletContext().getRequestDispatcher("First.jsp").forward(request,response);

or

getServletConfig().getServletContext().getRequestDispatcher("Second.jsp").forward(request,response);


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

The web.xml file contains the following entry:


<servlet>
<servlet-name>MyServlet</servlet-name>
<servlet-class>mypack.MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/MyFirstApp/*</url-pattern>
</servlet-mapping>

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
JSP:2.1
Servlet: 2.5
JDK:1.6

Regards,
John.
Ashwini Kashyap
Ranch Hand

Joined: Aug 30, 2012
Posts: 90
Hi,

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

Also, map your actions like:
1) From JSP:
<form action="user" method="post">
2) Make appropriate entry in web.xml like:
<servlet>
<servlet-name>User</servlet-name>
<servlet-class>com.info.UserServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>User</servlet-name>
<url-pattern>/user</url-pattern>
</servlet-mapping>
3) In Servlet, override doPost()/service(), write your business logic and forward it to other JSP accordingly.


Thanks,
Ashwini Kashyap | www.infocepts.com


 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: MVC using JSP, Servlets and Java classes
 
Similar Threads
error "Status 404" on redirect
jsp:include page="header.html" causes IllegalStateException
Servlet Ques:- How to 'include' other files into them.(ie .html .jsp files)
jsp:include page="header.html" causes IllegalStateException
passing object from one servlet to the other.