How can I take advantage of introspection and have a servlet process the entire form like I can using the JSP directive

Govind Seshadri

You can make use of the HttpUtils class to gain similar introspective functionality within servlets. It may not be as elegant as the introspection feature provided by the servlet engine when using JSPs, but still, it works fine.

For example, let's assume that you've created your form as:

<form method=post action=/servlet/FooServlet>
      Value1 <input name="one">
      Value2 <input name="two">
      Value3 <input name="three">
      <input type=submit> 
</form> 

You can automagically create a Hashtable object from the parsed key-value pairs of the POSTed data within doPost() of the servlet via the following snippet:

                        
Hashtable h = HttpUtils.parsePostData(request.getContentLength(),request.getInputStream()); 
String[] one = (String[])h.get("one"); 
String[] two = (String[])h.get("two"); 
String[] three = (String[])h.get("three"); 
//print out the value of 1st input element 
pw.println("<h2>One: "+one[0]+"</h2>

"); //print out the value of 2nd input element pw.println("<h2>Two: "+two[0]+"</h2>

"); //print out the value of 3rd input element pw.println("<h2>Three: "+three[0]+"</h2>

");

0 Comments  (click to add your comment)
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

About | Sitemap | Contact