Java Reference
In-Depth Information
</FORM>
</BODY>
</HTML>
When you open this HTML document in a browser, the input types marked as hidden will not
be visible. They will, however, be transmitted in the request.
Let's create a simple example that shows how this technique works. You'll create a servlet that
can service both POST and GET methods. In the doGet() method, you'll build a form that con-
tains hidden fields and an action that points to the servlet's doPost() method. The doPost()
method will then parse the hidden values sent in the request and echo them back to the client.
The example is found in Listing 5.1.
L ISTING 5.1
HiddenFieldServlet.java
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class HiddenFieldServlet extends HttpServlet {
public void init(ServletConfig config)
throws ServletException {
super.init(config);
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType(“text/html”);
PrintWriter out = response.getWriter();
out.println(“<html>”);
out.println(“<head><title>HiddenFieldServlet” +
“</title></head>”);
out.println(“<body>”);
5
// Create the Form with Hidden Fields
out.println(“<FORM ACTION=” +
“\”/djs/servlet/HiddenFieldServlet\” METHOD=\”POST\”>”);
 
Search WWH ::




Custom Search