Win a copy of Java EE 8 High Performance this week in the Java/Jakarta EE forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

MVC Architecture  RSS feed

Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!!

Ranch Hand
Posts: 440
Eclipse IDE Hibernate Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 .
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!