File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Encapsulate input for servlet/jsp

 
Timotius Pamungkas
Greenhorn
Posts: 28
  • 0
  • Mark post as helpful
  • send pies
  • 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
 
Eddy Lee Sin Ti
Ranch Hand
Posts: 135
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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?
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64192
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic