Getting Started with servlet development (2) a servlet program to run the detailed

Source: Internet
Author: User
Tags http request

Knowing the role of the servlet, let's look at a servlet's program running an instance, why do you want to see the running instance? Because you want to run a servlet program successfully, you need to configure some files.

The writing of the servlet program

A servlet program that develops an HTTP request that can be processed, must inherit the HttpServlet class, and at the very least overwrite the doget () method provided in the HttpServlet class in the custom servlet class as follows:

public void doget (HttpServletRequest req,httpservletresponse resp)     
               throws Servletexception,ioexception

It is not difficult to see from this method that this method can use the request and response objects.

Don't worry, let's take an example and say it from Hello World.

Create a project test in MyEclipse, and then create a package servlet to create a servlet_01.java underneath the package. As shown in the following illustration:

Do not rush to write code after the project, we also want to import a jar package----Servlet-api.jar, this is the Servlet jar package, this package needs to download online, but this article has attached the download link to the package.

To import a jar package into a Lib file in MyEclipse

Then write the code for Servlet_01.java, as follows:

Servlet_01.java     
package Servlet;     
Import java.io.*;     
Import javax.servlet.ServletException;     
Import javax.servlet.http.*;     
public class Servlet_01 extends httpservlet{public     
    void doget (HttpServletRequest req,httpservletresponse)     
               throws servletexception,ioexception{  //Overwrite Doget () method     
        PrintWriter out=resp.getwriter ();//Prepare Output     
        Out.println ("

This code inherits HttpServlet and then overrides the Doger () method, which first obtains an output stream object from the HttpServletResponse object and then prints the HTML element.

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.