• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

jsp:setProperty equivalent in servlet

 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there a way to get the values from an html form in a servlet in a 1 liner like the <jsp:setProperty> element?
 
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
No. You have to get parameters individually.
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So you would have to use setters and getters? How would you do that? Would you have a setter in a bean which interacts with the JPS, and then have the servlet get the data from the bean?
 
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To a servlet, "there is no bean".

There is only the request and the response.

If you have 20 form fields named param1, param2, ... param20, you'd need to use:

String param1 = request.getParameter("param1");
String param2 = request.getParameter("param2");
.
.
.

and so on, down to 20, in order to retrieve all the submitted data.

Thought of another way... 'getParameter' on the request object, is analogous to a bean's getter, but a request is NOT analogous to a bean. The request object's "getter" method has the word 'parameter' in it, and it doesn't have a corresponding 'set' method.
 
Peter Straw
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hmm... so if I have a webapp that is really just a whole load of forms for input/display of a database would it be better to use JSPs only? That way it can benefit from <jsp:setProperty> instead of the repetitve get/set methods in a servlet?
- Except then there is the problem of doing SQL interaction in the jsp code - isn't it bad practise to do this sort of code in a JSP? I thought they were intended to separate business logic from presentation. I'm a bit confused as to the best approach for this, any advice much appreciated...
[ April 29, 2003: Message edited by: Peter Straw ]
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well the 'benefit' of the jsp:setProperty is sorta kinda covered by the 'request' object, in my opinion. The jsp mega-setter (just use * to set all the properties) is useful when the target of your form submission is another JSP page. Sort of.

The :setProperty only transfers all the params from the request into your bean. But still.. you'd have to the use :getProperty every time you wanted one of the params. Which you'd need to do for processing some sort of business transaction. Same thing in the servlet with request. So where are you saving time by using a bean in a JSP?

Well, ok... the savings is potentially here:

If you were in a servlet, you might be tempted to send an instance of HttpRequest into the business object's OrderTicket method. After all, the request contains all of our fields right? But this is a bad separation of the HTTP layer from the business layer. So what you *should* do is construct a bean that contains all the fields of interest , and pass *that* object to the method. If this were a JSP page, and not a servlet, then you'd certainly save some time by being able to use the mega-setter method.

Having said that, there's nothing stopping you from coding your own little method that does this sort of thing. After all, Sun had to code that "*" behaviour into their jsp:setProperty tag. You could too. I imagine something like:


And yah, it's a bad idea to have JDBC code in a JSP. Just google for 'model 2 servlet' and you'll find plenty of articles.
 
reply
    Bookmark Topic Watch Topic
  • New Topic