Java Collecting Web Input

The cgi m odule expect s URL param et ers t o be separat ed by charact ers. I f you generat e a hyperlink t o a script based on t he cgi m odule and t he URL includes param et ers, dont separat e t hem wit h ; charact ers.

18.6.8 Java

Wit hin JSP pages, t he im plicit request obj ect provides access t o t he request param et ers t hrough t he following m et hods: getParameterNames Ret urns an enum erat ion of String obj ect s, one for each param et er nam e present in t he request . getParameterValuesString name Ret urns an array of String obj ect s, one for each value associat ed wit h t he param et er, or null if t he param et er does not exist . getParameterValueString name Ret urns t he first value associat ed wit h t he param et er, or null if t he param et er does not exist . The following exam ple shows one way t o use t hese m et hods t o display request param et ers: page import=java.util. ul Enumeration e = request.getParameterNames ; while e.hasMoreElements { String name = String e.nextElement ; use array in case parameter is multiple-valued String[ ] val = request.getParameterValues name; out.println li name: + name + ; values:; for int i = 0; i val.length; i++ out.println val[i]; out.println li; } ul Request param et ers are also available wit hin JSTL t ags, using t he special variables param and paramValues . param[name] ret urns t he first value for a given param et er and t hus is m ost suit ed for single- valued param et ers: color value: c:out value={param[color]} paramValues[name] ret urns an array of values for t he param et er, so it s useful for param et ers t hat m ay have m ult iple values: accessory values: c:forEach var=val items={paramValues[accessories]} c:out value={val} c:forEach You can also access a param et er using dot not at ion if t he param et er nam e is legal as an obj ect propert y nam e: color value: c:out value={param.color} accessory values: c:forEach var=val items={paramValues.accessories} c:out value={val} c:forEach To produce a list of param et er obj ect s wit h key and value at t ribut es, it erat e over t he paramValues variable: ul c:forEach var=p items={paramValues} li name: c:out value={p.key} ; values: c:forEach var=val items={p.value} c:out value={val} c:forEach li c:forEach ul To const ruct URLs t hat point t o JSP pages and t hat have param et ers at t he end, you should separat e t he param et ers by charact ers.

18.7 Validating Web Input