aspose file tools*
The moose likes Servlets and the fly likes MVC Architecture Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "MVC Architecture" Watch "MVC Architecture" New topic
Author

MVC Architecture

Amit Singh Hora
Greenhorn

Joined: Dec 23, 2012
Posts: 8
Hi all,

I have been reading a lot on MVC but really don't know if i am clear on the concepts of MVC or not
recently developed an application what i did is
1)on jsp load called a function
2)using AJAX called a servlet and servlet is there performing all the logic
3)servlet called a java bean and a java class to perform some logic and return result
4)based on the result returned form the class i am displaying an image say if result is 1 then image A ,if 0 then Image b
5)on servlets POST method i am using out.println()-->to write the complete output
6)the function on jsp after returning the call will set the innetHTML of required div by the output generated by the servlet

now say the output servlet is producing is the table
__________________________________
instance name|instance state
________________________________

now if i want at some time to change the display for this table to say
______________________________
instance state|instance name
____________________________
to do the above mentioned change i have to recompile my servlet and redeploy the war

is it really a MVC?
and someone suggested me to use JSON store object of a bean containing data as JSON and then return the JSON object to the jsp
and at jsp using this object contruct the table!

any pointers on this will be of great help!!

Saif Asif
Ranch Hand

Joined: Aug 11, 2011
Posts: 440

Greetings Amit Singh Hora ,

I went through your steps and you pretty much got the whole concept of MVC . A servlet is called which accepts all required parameters ( via ajax or javascript or anyother way ) and then passes these parameters to a Model class . This is where all the business logic is and all the calulations and logical operations are performed here. When the operations are complete , a result is returned and based on this results , a view ( another JSP ) is displayed. Now here you said that you are actualy writing the response JSP inside the servlet doPost method . Although this can be done , but its not a preferred way of doing it and the major reasons behind it is the readability issue and the re-deployment ( re-compilation of the servlet ) for every change in the HTML code since it is a java class.

To over-come this , it is most preferred to use some MVC framework ( struts or spring or play ) where you map an actual JSP page with the result obtained from response servlet. In these frameworks, the servlet is just responsible to handle java code and then return a result . The framwork then decides which view ( JSP ) to display by parsing through a configuration file ( usualy an xml file ) based on the returned result.

The rest of the steps you mentioned are all correct in accordance with MVC architecture.

As for the JSON object on JSP , Yes it is most preferred to store data as JSON because its easier to handle JSON object via javascript for view puropses. You can use JSTL to iterate over the JSON object. There are multiple possibilites that you can use .


Muhammad Saif Asif Mirza
OCJA(5/6) OCJP(6) OCJWCD(6)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: MVC Architecture