Wednesday, March 2, 2011

Chapter 10: Form Parameters

By now, we all know what a JSP page is and the contents that can be placed inside it. One of those is the “Form” or the “HTML Form”. This is the entity that is going to capture all the data displayed on the screen (including user entered values) and bring it back to the Servlet.
In this chapter, we are going to take a look at how the servlet will handle/understand the contents of the data that the form sends.

So, lets get started!!!

The ServletRequest Interface

The contents of the HTML Form is made available to the Servlet through the ServletRequest interface. This interface is implemented by the Web container to get the parameters from a request. Parameters are sent in the query string or posted form data. This interface has methods that can be used the servlet to access the contents of the form or the query string. They are:

• getParameter(String). You use this method if you know the particular parameter name. It returns the value of a request parameter as a string, or null if the parameter does not exist. Use this method when you are sure the parameter has only one value
• getParameterMap(). You use this method to create a map of the form parameters supplied with this request.
• getParameterNames(). This method returns an Enumeration of string objects containing the names of the parameters contained in this request, or an empty Enumeration if the request has no parameters.
• getParameterValues(String). This method returns an array of values as strings, or null if the parameter does not exist. If the parameter has a single value, the array has a length of 1. One of the common uses of getParameterValues() is for processing drop down lists that have their “multiple” attribute set.

Exam Trivia:
If you use the getParameter() method with a multivalued parameter, you won't get an error. You will get the first value in the array returned by getParameterValues() and you will end up losing the other values that were part of the return data.
Let us now take a look at a simple servlet that can read the contents of the form from the request.

Servlet Code

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class RequestParametersReadingServlet extends HttpServlet
{
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException
{
Enumeration parameterNames = request.getParameterNames();

PrintWriter out = res.getWriter ();

while (parameterNames.hasMoreElements()) {
String name = (String)parameterNames.nextElement();
String value = request.getParameter(name);
out.println(“Parameter ” + name + " = " + value + "< br / >");
}
}
}

This Servlet is pretty straightforward. It just uses the getParameterNames() method to get the enumeration of all parameters in the request and then loops through it to print out the contents.

Previous Chapter: Chapter 9 - Triggering HttpServlet GET, POST, and HEAD Methods

Next Chapter: Chapter 11 - Obtaining a Servlets Initialization Parameters

No comments:

Post a Comment

© 2013 by www.inheritingjava.blogspot.com. All rights reserved. No part of this blog or its contents may be reproduced or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise, without prior written permission of the Author.

ShareThis

Google+ Followers

Followers