Prentice Hall and Sun Microsystems. Personal use only; do not redistribute.
3.4 Example: Reading All Parameters
71
Notice that the servlet uses a 
doPost
 method that simply calls 
doGet
.
That's  because  I  want  it  to  be  able  to  handle  both 
GET
 and 
POST
 requests.
This  approach  is  a  good  standard  practice  if  you  want  HTML  interfaces  to
have some flexibility in how they send data to the servlet. See the discussion
of the 
service
 method in Section 2.6 (The Servlet Life Cycle) for a discus 
sion of why having 
doPost
 call 
doGet
 (or vice versa) is preferable to overrid 
ing 
service
 directly. The HTML form from Listing 3.4 uses 
POST
, as
should  all forms that have password fields (if you don't know why, see
Chapter 16). However, the 
ShowParameters
 servlet is not specific to that
particular front end, so the source code archive site at 
www.coreserv 
lets.com
 includes a similar HTML form that uses 
GET
 for you to experi 
ment with.
Listing 3.3 ShowParameters.java 
package coreservlets;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
public class ShowParameters extends HttpServlet {
  public void doGet(HttpServletRequest request,
                    HttpServletResponse response)
      throws ServletException, IOException {
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    String title = "Reading All Request Parameters";
    out.println(ServletUtilities.headWithTitle(title) +
                "\n" +
                "

" + title + "

\n" +
                "\n" +
                "
\n" +
                "
Parameter NameParameter Value(s)");
    Enumeration paramNames = request.getParameterNames();
    while(paramNames.hasMoreElements()) {
      String paramName = (String)paramNames.nextElement();
      out.print("
" + paramName + "\n");
      String[] paramValues =
        request.getParameterValues(paramName);
      if (paramValues.length == 1) {
        String paramValue = paramValues[0];
        if (paramValue.length() == 0)
          out.println("No Value");
Second edition of this book: www.coreservlets.com; Sequel: www.moreservlets.com.
Servlet and JSP training courses by book's author: courses.coreservlets.com.






footer




 

 

 

 

 Home | About Us | Network | Services | Support | FAQ | Control Panel | Order Online | Sitemap | Contact

jsp web hosting

 

Our partners: PHP: Hypertext Preprocessor Best Web Hosting Java Web Hosting Jsp Web Hosting Cheapest Web Hosting

Visionwebhosting.net Business web hosting division of Web Design Plus. All rights reserved