aspose file tools*
The moose likes Servlets and the fly likes Encapsulate input for servlet/jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Encapsulate input for servlet/jsp" Watch "Encapsulate input for servlet/jsp" New topic
Author

Encapsulate input for servlet/jsp

Timotius Pamungkas
Greenhorn

Joined: Feb 26, 2005
Posts: 28
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

Joined: Oct 06, 2005
Posts: 135
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


SCJP, SCWCD, SCJWS, IBM 700,IBM 701, IBM 704, IBM 705, CA Clarity Technical<br /> <br /><a href="http://eddyleesinti.blogspot.com" target="_blank" rel="nofollow">http://eddyleesinti.blogspot.com</a>
Timotius Pamungkas
Greenhorn

Joined: Feb 26, 2005
Posts: 28
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

Joined: Dec 11, 2004
Posts: 13410



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.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Timotius Pamungkas
Greenhorn

Joined: Feb 26, 2005
Posts: 28
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

Joined: Jan 10, 2002
Posts: 61610
    
  67

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Encapsulate input for servlet/jsp