Servlet简单例子

一、项目结构

二、index.jsp

<%@ page contentType="text/html; charset=utf-8" %>
<html>
<body>
    <h2>Hello World!</h2><hr/>
    <a href="aaa/bbb">Get方式请求</a><br/>
    <!-- 会到web.xml中交给url-pattern为/aaa/bbb的Servlet来处理,根据get、post的提交方式来执行doGet()、doPost()方法-->
    <form action="aaa/bbb" method="post">
        <input type="submit" value="Post方式请求"/>
    </form>
</body>
</html>

三、MyServlet.java

package com.yanguobin;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class MyServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("处理Get()请求...");
        //使servlet页面中文不会乱码,一定要放在getWriter()方法前面
        resp.setContentType("text/html; charset=utf-8");
        //添加上面这行才会解析html代码,显示Get()请求成功!的加粗模式,否则不会解析html代码,直接显示html标签
        PrintWriter out = resp.getWriter();
        out.println("<strong>Get()请求成功!</strong><br/>");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("处理Post()请求...");
        //使servlet页面中文不会乱码,一定要放在getWriter()方法前面
        resp.setContentType("text/html; charset=utf-8");
        //添加上面这行才会解析html代码,显示Get()请求成功!的加粗模式,否则不会解析html代码,直接显示html标签
        PrintWriter out = resp.getWriter();
        out.println("<strong>Post()请求成功!</strong><br/>");
    }
}

四、web.xml

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
  
  <servlet>
    <servlet-name>myservlet</servlet-name>
    <servlet-class>com.yanguobin.MyServlet</servlet-class>  <!-- 包名类名写全 -->
  </servlet>
  <servlet-mapping>
    <servlet-name>myservlet</servlet-name>
    <url-pattern>/aaa/bbb</url-pattern>
    <!-- 第一个/表示项目的根目录,或者说使当前Web工程的根目录,不能省略 -->
    <!-- myget/aaa应与前端页面中的请求地址一致,即访问地址 -->
  </servlet-mapping>
</web-app>

五、pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.yanguobin</groupId>
  <artifactId>servletDemo</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>war</packaging>

  <name>servletDemo Maven Webapp</name>
  <!-- FIXME change it to the project's website -->
  <url>http://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>1.7</maven.compiler.source>
    <maven.compiler.target>1.7</maven.compiler.target>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>4.0.1</version>
      <scope>provided</scope>
    </dependency>
  </dependencies>

  <build>
    <finalName>servletDemo</finalName>
    <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
      <plugins>
        <plugin>
          <artifactId>maven-clean-plugin</artifactId>
          <version>3.1.0</version>
        </plugin>
        <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
        <plugin>
          <artifactId>maven-resources-plugin</artifactId>
          <version>3.0.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-compiler-plugin</artifactId>
          <version>3.8.0</version>
        </plugin>
        <plugin>
          <artifactId>maven-surefire-plugin</artifactId>
          <version>2.22.1</version>
        </plugin>
        <plugin>
          <artifactId>maven-war-plugin</artifactId>
          <version>3.2.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-install-plugin</artifactId>
          <version>2.5.2</version>
        </plugin>
        <plugin>
          <artifactId>maven-deploy-plugin</artifactId>
          <version>2.8.2</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>

六、运行结果

原文地址:https://www.cnblogs.com/yanguobin/p/12017127.html