aspose file tools*
The moose likes Servlets and the fly likes Best way to capture HTML form data Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Best way to capture HTML form data" Watch "Best way to capture HTML form data" New topic
Author

Best way to capture HTML form data

Ankit Mishra
Ranch Hand

Joined: Jul 15, 2009
Posts: 40
Hi All,
What is the best way to capture HTML form data in servlet. What I mean is I don't want to directly say request.getParameter("x") in servlet. I want to create one class which can capture the form data and I can use that object in that servlet class.

Is it the correct approach? and what other approach I can use it.

I have worked on MVC Design pattern earlier but need to know how can I enhance this I mean capturing of data not directly in a servlet.

Note: I dont want struts, springs, Hibernates.

Regards,
Ankit Mishra (SCJP 5.0 93%), (SCWCD 5.0 97%)
Deepak Kumar Jena
Ranch Hand

Joined: Sep 08, 2010
Posts: 68

Hi Ankit, Yes we can declare a class and it can be instantiated from a URL. Then we can add request parameters and cookies to it. Also we need to retrieve an InputStream, so that we can use it in other resources for example, Servlet. For other approach you can view the Post at http://www.coderanch.com/t/353686/Servlets/java/Uploading-file-server-oreilly-MultiPartRequest.


dkj4jdk
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

@Ankit: then you'll need to reproduce the logic used by various frameworks to initialize an object (or multiple objects) from the request parameters. There are any number of ways to do this.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12781
    
    5
If I understand your question correctly -

Why not use the getParmeterMap() method of javax.servlet.Request - this gets you a collection with all the form data in a single call.

If you have all subsequent processes work with that map they are completely independent of the servlet environment which means you can do testing outside the servlet environment, a great simplification.

Bill
Ankit Mishra
Ranch Hand

Joined: Jul 15, 2009
Posts: 40
Thank you guys for your replies. I have understood your points. Just one clarification will following be a worth for a small web application

JSP (View) - sending data
1 servlet controller which will redirect to different servlets based upon say some hidden parameter.
Bean classes to capture data which will then interact with database codes to do database things.


Actually I just need one servlet where requests from jsp will be redirected from that servlet will call other business logic classes.
Ravi Sree
Ranch Hand

Joined: Jan 24, 2010
Posts: 62

Ankit Mishra wrote:Thank you guys for your replies. I have understood your points. Just one clarification will following be a worth for a small web application

JSP (View) - sending data
1 servlet controller which will redirect to different servlets based upon say some hidden parameter.
Bean classes to capture data which will then interact with database codes to do database things.


Actually I just need one servlet where requests from jsp will be redirected from that servlet will call other business logic classes.


Hi Ankit,

I too was looking for the solution for the same problem, like a generic method
by which all the form-data will be captured as a bean before going to the servlet
and in the request scope as in struts etc.
In my case the request is just sent to the appropriate 'Action' class to the handle request,
where i manually parse the request parameters for each request.

Please suggest if you get more effective method to do this.

Regards,
Sree
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18570
    
    8

Bill Clar,
Your post was moved to a new topic.
Hikari Shidou
Ranch Hand

Joined: Jan 22, 2013
Posts: 88
William Brogden wrote:If I understand your question correctly -

Why not use the getParmeterMap() method of javax.servlet.Request - this gets you a collection with all the form data in a single call.

If you have all subsequent processes work with that map they are completely independent of the servlet environment which means you can do testing outside the servlet environment, a great simplification.

Bill


That's not a good solution. Keep HTTP data in servlet only, don't move it around your software.

It's better to create a Bean with the real data, and convert request to that bean. Op wants exactally to automatically do that conversion instead of doing a lot of getParameter()s and sets.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Best way to capture HTML form data