|
|
|
|
Let's take a simple form as an example: <?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Form 1</title>
</head>
<body>
<h1 align="center">First Form</h1>
<form action="Form1Servlet">
First Name: <input type="text" name="first-name"/><br />
Last Name: <input type="text" name="last-name"/><br />
Occupation: <input type="text" name="occupation"/><br />
<input type="submit"/>
</form>
</body>
</html>
which displays as:
where I've already filled in some form data. Now, let's write a servlet that can handle the parameters passed in from this form: /*
* Form1Servlet.java
*
* Created on November 24, 2006
*/
package formExamples;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* A servlet to process the parameters from a form.
*
* @author Bryan Higgs
* @version
*/
public class Form1Servlet extends HttpServlet
{
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
* @param request servlet request
* @param response servlet response
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html; charset=UTF-8");
PrintWriter out = response.getWriter();
String docType = "<!DOCTYE html PUBLIC \"-//w3c//DTD XHTML 1.0 Strict//EN\"" +
" \"http://www.w3c.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n";
out.println(docType);
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet Form1Servlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet Form1Servlet at " + request.getContextPath () + "</h1>");
out.println("<ul>");
out.println( "<li><b>first-name:</b> " + request.getParameter("first-name") + "</li>");
out.println( "<li><b>last-name:</b> " + request.getParameter("last-name") + "</li>");
out.println( "<li><b>occupation:</b> " + request.getParameter("occupation") + "</li>");
out.println("</ul>");
out.println("</body>");
out.println("</html>");
out.close();
}
/**
* Handles the HTTP <code>GET</code> method.
* @param request servlet request
* @param response servlet response
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
processRequest(request, response);
}
/**
* Handles the HTTP <code>POST</code> method.
* @param request servlet request
* @param response servlet response
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*/
public String getServletInfo()
{
return "Short description";
}
}
When the above form is submitted, we get the following displayed from the servlet:
Note the URL resulting from this form was: http://localhost:8084/WebApplication1/Form1Servlet (cont)
?first-name=Bill&last-name=Gates&occupation=Modern-day+Rich+Baron
so we are doing a GET HTTP request. |
| The page was last updated February 19, 2008 |