This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Servlets and the fly likes jsp:setProperty equivalent in servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "jsp:setProperty equivalent in servlet" Watch "jsp:setProperty equivalent in servlet" New topic

jsp:setProperty equivalent in servlet

Peter Straw
Ranch Hand

Joined: Jan 08, 2002
Posts: 79
Is there a way to get the values from an html form in a servlet in a 1 liner like the <jsp:setProperty> element?
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 12760
No. You have to get parameters individually.
Joe Broderick

Joined: Apr 23, 2003
Posts: 23
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?
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

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

Joined: Jan 08, 2002
Posts: 79
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

Joined: Feb 20, 2001
Posts: 3695

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.
I agree. Here's the link:
subject: jsp:setProperty equivalent in servlet
Similar Threads
jsp:setProperty. JSP to servlet
JavaBeans and Servlets
HFSJ book:Bean Example Page 357
jsp:useBean and default scope (head first mock exam question)
how to get the values in a servlet? the values are set in a bean using jsp