Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

MVC using JSP, Servlets and Java classes

 
Johnnie Smith
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic