Java Reference
In-Depth Information
￿ String getParameter(String <name>)
Returns the value of a single parameter sent with GET or POST.
￿ Enumeration getParameterNames()
Returns the names of all parameters sent with POST.
￿ String[] getParameterValues(String <name>)
Returns the values for a parameter that may have more than one value.
Only the fi rst of these methods is needed for a single parameter sent via GET ,
which is all we require for our current example. The code below shows our fi rst
servlet modifi ed so that it adds the name entered by the user to the greeting that is
displayed on the returned page. The code shown in bold type indicates the very few
changes made to the original program.
Example
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.WebServlet;
@WebServlet(" /PersonalServlet ")
public class PersonalServlet extends HttpServlet
{
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException,ServletException
{
response.setContentType("text/HTML");
PrintWriter out = response.getWriter();
out.println("<HTML>");
out.println("<HEAD>");
out.println("<TITLE>Simple Servlet</TITLE>");
out.println("</HEAD>");
out.println("<BODY>");
out.println("<BR><BR><BR>");
String name = request.getParameter("FirstName");
out.println(" < H1 > A Simple Servlet for ");
out.println(name + " < /H1 >< /CENTER > ");
out.println("</BODY>");
out.println("</HTML>");
out.fl ush();
}
}
This is what the initial page looks like (after a name has been entered into the
text box) (Fig . 8.3 ):
The servlet-generated page (after the above button has been clicked) is shown
in Fig. 8.4 .
Search WWH ::




Custom Search