• 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:
  • Campbell Ritchie
  • Devaka Cooray
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Jeanne Boyarsky
  • Tim Cooke
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
Bartenders:

Best way to capture HTML form data

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 71
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 https://coderanch.com/t/353686/Servlets/java/Uploading-file-server-oreilly-MultiPartRequest.
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 64
MySQL Database PHP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 28009
94
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bill Clar,
Your post was moved to a new topic.
 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
You're not going crazy. You're going sane in a crazy word. Find comfort in this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic