The basic process and setup of the servlet are described in the servlet appetizer. Here, we'll look at some servlet instances. These instances are common problems in HTTP traffic (reference HTTP protocol: http://www.cnblogs.com/vamei/archive/2013/05/11/3069788.html). We implement the HTTP protocol functionality by manipulating request and response.
Build Page
The purpose of the servlet is to dynamically generate pages. For example, the following examples:
Package foo;
Import javax.servlet.*;
Import javax.servlet.http.*;
Import java.io.*;
Import java.util.*;
public class Testpage extends HttpServlet {public
void doget (HttpServletRequest request,
HttpServletResponse Response)
throws IOException, servletexception
{
PrintWriter out = Response.getwriter ();
Date now = new Date (); Date & Time
String page = '
Above, we call the response Getwriter () method, which writes the text to the reply, which is the main part of the reply. For Java reading and writing, refer to the Java IO Foundation.
Sometimes our main body part is not text, then we need to outputstream the type of writer, by invoking the response Getoutputstream () method to obtain.
We use the functionality provided by Java.util.Date () to dynamically generate the date and time display. The effect is as follows:
Submit Form
The HTTP request has a POST method. The servlet uses the Dopost () method to process post requests. The purpose of the Post method is to submit data to the server, especially the data contained in the HTML form element.
Let's start by writing an HTML page that contains a form:
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/Java/