This week's book giveaway is in the General Computing forum. We're giving away four copies of Arduino in Action and have Martin Evans, Joshua Noble, and Jordan Hochenbaum on-line! See this thread for details.
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.
@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.
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.
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.
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.
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.