• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Encapsulate input for servlet/jsp

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I want to ask about buffering. I'm just starting learn JSP/servlet, and now I use something like this to gather user input :

String fullName = request.getParameter("fullName");
String dateOfBirth = request.getParameter("dateOfBirth");
String townOfBirth = request.getParameter("townOfBirth");

I wonder if I can create a class -Person.java- and encapsulate full name, date of birth, and town of birth in that class. So I think, it is possible to do this :

Person person = request.getParameter("person");

It might be something like struts's ActionForm, but in pure JSP/servlet.

Thanks before
 
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, jsp:useBean might meet your need. What it does is that it will auto populate your Javabean from the http request parameter. Of course, some naming conventions has to be followed.

Hope this helps. Cheers
 
Timotius Pamungkas
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your answer Eddy. However, I'm just started learning JSP/Servlet, thus it might take a while for me to understand jsp:usebean. If you don't mind, can you give me a simple example about that?
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


That, you won't be able to do.
request.getParameter returns Strings, not objects.

What you could do is create your person object with a constructor that takes either a reference to HttpServletRequest or a Map of all the request parameters.
http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/ServletRequest.html#getParameterMap()

I think what Eddy Lee Sin Ti was referring to was the jsp:setProperty tag that goes with jsp:useBean.
If you're using a JSP to retrieve the HTTP post parameters, there is a convenient way to transfer all of them to a bean.

Section JSP5.2 of the JSP spec (link in my signature) covers this.

These days, you don't see too many people using JSPs to retrieve form parameters. It is now considered bad form to use JSPs for anything but formatting output.
 
Timotius Pamungkas
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
These days, you don't see too many people using JSPs to retrieve form parameters. It is now considered bad form to use JSPs for anything but formatting output.


>> So, is there more elegant way to retrieve form parameters?
 
Sheriff
Posts: 67645
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

Originally posted by Timotius Pamungkas:
So, is there more elegant way to retrieve form parameters?



In a servlet controller that gets invoked before the JSP.

Maybe this article on web application structure might be helpful.
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic