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.
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.
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.