Java Web Learning (iii) servlet salad

Source: Internet
Author: User
Tags date date now html form html page http request java web

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/

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.