March 2008
Intermediate to advanced
911 pages
20h 31m
English
HTML form

HTTP POST request

Servlet class
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { String colorParam = request.getParameter("color"); String bodyParam = request.getParameter("body"); // more code here }
Now the String variable colorParam has a value of “dark” and bodyParam has a value of “heavy”.
You can have multiple values for a single parameter! That means you’ll need getParameterValues() that returns an array, instead of getParameter() that returns a String.
Some form input types, like a set of checkboxes, can have more than one value. That means a single parameter (“sizes”, for example) will have multiple values, depending on how many boxes the user checked off. A form where a user can select multiple beer sizes (to say that he’s interested in ALL of those sizes) might look like this:
<form method=POST action="SelectBeer.do"> Select beer characteristics<p> Can Sizes: <p> <input type=checkbox name=sizes value="12oz"> 12 oz.<br> <input type=checkbox name=sizes value="16oz"> 16 oz.<br> <input type=checkbox name=sizes value="22oz"> 22 oz.<br> <br><br> <center> <input type="SUBMIT"> </center> </form>
In your code, you’ll use the getParameterValues() method that returns an ...
Read now
Unlock full access