• Post Reply Bookmark Topic Watch Topic
  • New Topic

how to pass data from view to model?

 
Yi Meng
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I usually have a jsp page display the input form, and then submit to another jsp page which is only 3 lines to set all the properties of the model and then forward to a servlet to perform other tasks.
e.g. i have test1.jsp displaying a new user application form. The form action is to test2.jsp, which is :

and perform other tasks in the servlet.
how do you evaluate this practice, good or poor? any beter alternatives instead of calling request.getParameter() for every parameter and use the setter of the model to set the value?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35709
408
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why can't you copy to the model in your servlet? Or have the servlet call a class/method that will do that?
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

NOT a good idea because "/servlet/" depends on the "invoker" servlet - this invariably causes trouble. You should only forward to a servlet defined correctly in a web.xml.
Bill
 
Yi Meng
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jeanne Boyarsky:
Why can't you copy to the model in your servlet? Or have the servlet call a class/method that will do that?

That means i have to call request.getParameter("...") in the servlet for quite a number of times in order to get all the attributes, and set them one by one or use the model's constructor which accepts all its attributes as parameters. This will result in quite lengthy code in servlet class and i dun like it.
what's the best practice?
 
Yi Meng
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by William Brogden:

NOT a good idea because "/servlet/" depends on the "invoker" servlet - this invariably causes trouble. You should only forward to a servlet defined correctly in a web.xml.
Bill

Of course i will have the correct and working mapping in web.xml for this. my question is what's the best and common practice for passing data from view to model?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65516
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what's the best practice?

In my opinion, the best practice is to do what's most straight-forward and easily understood. Using the JSP as a "pre-processor" to populate a bean in order to save yourself the trouble of writing the getParameter calls doesn't fall into that category.
bear
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 35709
408
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The JSP is doing the equivalent of the getParameter calls anyway. So I'm not sure what you are saving.
If the servlet is getting too long, you can break it up into different methods or classes.
 
Yi Meng
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bear Bibeault:

In my opinion, the best practice is to do what's most straight-forward and easily understood. Using the JSP as a "pre-processor" to populate a bean in order to save yourself the trouble of writing the getParameter calls doesn't fall into that category.
bear

your "pre-processor" refers to something like this, isn't it?
<jsp:useBean id="user" scope="request" class="com.abc.User" /> <jsp:setProperty name="user" property="*"/><jsp:forward page="/servlet/UserController"/>
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!