---
title: "手工编写第一个Servlet"
author: "cakin24"
date: "2017-10-11T20:06:21Z"
tags: []
categories: ["编程语言"]
slug: "servlet"
draft: false
lastmod: "2017-10-11T20:06:21Z"
id: 2395944
url: "https://cakin24.iteye.com/blog/2395944"
---

一 步骤

1、继承HttpServlet

2、重写doGet()或者doPost()方法

3、在web.xml中注册Servlet

 

二 HttpServlet类继承图

![](http://dl2.iteye.com/upload/attachment/0127/3295/dbeeda36-dce5-38ac-99b6-aa8421c6a6ae.png)  
 

 

三 实例

1、创建MyFirstServletDemo web project.

![](http://dl2.iteye.com/upload/attachment/0127/3297/8b200075-9828-3f5e-8986-6d0f50931e96.png)  
 

2、编辑index.jsp

&lt;%@ page language="java" import="java.util.\*"  contentType="text/html; charset=utf-8"%&gt;

&lt;%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%&gt;

 

&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"&gt;

&lt;html&gt;

  &lt;head&gt;

    &lt;base href="&lt;%=basePath%&gt;"&gt;

   

    &lt;title&gt;My JSP 'index.jsp' starting page&lt;/title&gt;

        &lt;meta http-equiv="pragma" content="no-cache"&gt;

        &lt;meta http-equiv="cache-control" content="no-cache"&gt;

        &lt;meta http-equiv="expires" content="0"&gt;   

        &lt;meta http-equiv="keywords" content="keyword1,keyword2,keyword3"&gt;

        &lt;meta http-equiv="description" content="This is my page"&gt;

        &lt;!--

        &lt;link rel="stylesheet" type="text/css" href="styles.css"&gt;

        --&gt;

  &lt;/head&gt;

 

  &lt;body&gt;

    &lt;h1&gt;第一个Servlet小例子&lt;/h1&gt;

    &lt;a href="servlet/HelloServlet"&gt;Get方式请求HelloServlet&lt;/a&gt;

    &lt;form action="servlet/HelloServlet" method="post"&gt;

                &lt;input type="submit" value="Post方式请求HelloServlet"&gt;

    &lt;/form&gt;

  &lt;/body&gt;

&lt;/html&gt;

3、编写HelloServlet

package servlet;

 

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;

 

//继承于HttpServlet

public class HelloServlet extends HttpServlet {

 

        @Override

        protected void doGet(HttpServletRequest request, HttpServletResponse response)

                        throws ServletException, IOException {

                // TODO Auto-generated method stub

                System.out.println("处理get（）请求...");

                PrintWriter out = response.getWriter();

                response.setContentType("text/html; charset=utf-8");

                out.println("&lt;strong&gt;Hello Servlet!&lt;/strong&gt;&lt;br&gt;");

        }

 

        @Override

        protected void doPost(HttpServletRequest request, HttpServletResponse response)

                        throws ServletException, IOException {

                System.out.println("处理post（）请求...");

                PrintWriter out = response.getWriter();

                response.setContentType("text/html; charset=utf-8");

                out.println("&lt;strong&gt;Hello Servlet!&lt;/strong&gt;&lt;br&gt;");

        }

 

}

4、编写web.xml

&lt;?xml version="1.0" encoding="UTF-8"?&gt;

&lt;web-app version="2.5"

        xmlns="[http://java.sun.com/xml/ns/javaee"](http://java.sun.com/xml/ns/javaee%22);

        xmlns:xsi="[http://www.w3.org/2001/XMLSchema-instance"](http://www.w3.org/2001/XMLSchema-instance%22);

        xsi:schemaLocation="[http://java.sun.com/xml/ns/javaee](http://java.sun.com/xml/ns/javaee)

[http://java.sun.com/xml/ns/javaee/web-app\_2\_5.xsd"](http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd%22);&gt;

  &lt;display-name&gt;&lt;/display-name&gt;  

  &lt;welcome-file-list&gt;

    &lt;welcome-file&gt;index.jsp&lt;/welcome-file&gt;

  &lt;/welcome-file-list&gt;

  &lt;servlet&gt;

        &lt;servlet-name&gt;HelloServlet&lt;/servlet-name&gt;

        &lt;servlet-class&gt;servlet.HelloServlet&lt;/servlet-class&gt;

  &lt;/servlet&gt;

  &lt;servlet-mapping&gt;

         &lt;servlet-name&gt;HelloServlet&lt;/servlet-name&gt;

         &lt;url-pattern&gt;/servlet/HelloServlet&lt;/url-pattern&gt;

  &lt;/servlet-mapping&gt;

&lt;/web-app&gt;

 

四 运行结果

 

![](http://dl2.iteye.com/upload/attachment/0127/3299/e9405491-1692-32e0-a1a0-829bc7a09bd9.png)