File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes How to implement mvc using servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to implement mvc using servlets" Watch "How to implement mvc using servlets" New topic
Author

How to implement mvc using servlets

Siddharth Bhargava
Ranch Hand

Joined: Feb 23, 2007
Posts: 248
Hi All, Could anyone please give me an example on how to implement mvc design pattern using servlets and jsp. Any practical example would be helpful. Please do guide me on how to implement mvc using servlets and jsps. What's the role of each one of them in the example. Please also give me the links to resources to sites which explains the concept best. Thanks in advance.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

The servlets section of our CodeBarn has some very simple examples.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Siddharth Bhargava
Ranch Hand

Joined: Feb 23, 2007
Posts: 248
Hi Ben, I saw the source code of the example given in Code Barn. Could you explain me the working of ControllerServlet.java




According to my understanding :
This ControllerServlet acts as a controller. In this servlet we are populating the model which in this case is ModelBean.We are setting the model in the request attribute which is modelBean. Then we are forwarding to view.jsp and passing the request and response. Please do clear my concepts.
I read that the controller performs some logic and then according to the result passes on the information to the view. Here where we are implementing some logic, we are simply passing the model to the view.
Another question, can I use Login validation as an example where I use a class Login as a Bean, then in the controller pass the username and password to the Login bean to do the validation. and then the bean passes on the result to the controller which according to the result passes on to the appropriate view.
I would like to clear my concept with a clear and simple example. Please do guide me. Thanks in advance.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

It sounds like it is already clear to you.

Yes, this is a common way to write validation.
Often the model will communicate with an object that will perform a database look up on the username and password.
The important thing is that the servlet and JSP don't need to know that there is a database transaction going on. Likewise your data access objects should have no idea that they're being called from a servlet app.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to implement mvc using servlets
 
Similar Threads
What is MVC
Need Info
RMI using servlet
Spring AOP with mvc
Sequence of steps for Structs?