Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Best way to capture HTML form data

 
Ankit Mishra
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Deepak Kumar Jena
Ranch Hand
Posts: 68
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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
Posts: 13071
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 64
Java MySQL Database PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21314
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bill Clar,
Your post was moved to a new topic.
 
Hikari Shidou
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic