After the method declaration, add the request code (in black text below):
import java.io.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; public class ServletSample extends HttpServlet { public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Enumeration keys; String key; String myName = ""; keys = request.getParameterNames(); while (keys.hasMoreElements()) { key = (String) keys.nextElement(); if (key.equalsIgnoreCase("myName")) myName = request.getParameter(key); } System.out.println("Name = "); if (myName == "") myName = "Hello"; } }
This new section of code handles the client request parameters. The HttpServletRequest class has the following specific methods to retrieve information provided by the client:
getParameterNames()
This method returns the names of the parameters in an enumeration of strings.
getParameterValues()
This method returns the values of the parameters in an array of strings.
getParameter()
This method returns a single parameter value as a string. Use this method when you know the client request returns only one parameter.
In the case of ServletSample, the getParameterNames() method gets the name of the parameters, and the getParameter() method gets the value of the myName parameter.