• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

how to pass data from view to model?

 
Ranch Hand
Posts: 270
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author & internet detective
Posts: 40793
828
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 67595
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 40793
828
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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"/>
 
reply
    Bookmark Topic Watch Topic
  • New Topic