Servlet简单例子

一、项目结构

Servlet简单例子_第1张图片

二、index.jsp

<%@ page contentType="text/html; charset=utf-8" %>
<html>
<body>
    <h2>Hello World!h2><hr/>
    <a href="aaa/bbb">Get方式请求a><br/>
    
    <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("Get()请求成功!
"); } @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("Post()请求成功!
"); } }

四、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 Applicationdisplay-name>
  
  <servlet>
    <servlet-name>myservletservlet-name>
    <servlet-class>com.yanguobin.MyServletservlet-class>  
  servlet>
  <servlet-mapping>
    <servlet-name>myservletservlet-name>
    <url-pattern>/aaa/bbburl-pattern>
    
    
  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.0modelVersion>

  <groupId>com.yanguobingroupId>
  <artifactId>servletDemoartifactId>
  <version>1.0-SNAPSHOTversion>
  <packaging>warpackaging>

  <name>servletDemo Maven Webappname>
  
  <url>http://www.example.comurl>

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

  <dependencies>
    <dependency>
      <groupId>junitgroupId>
      <artifactId>junitartifactId>
      <version>4.11version>
      <scope>testscope>
    dependency>
    
      javax.servlet
      javax.servlet-api
      4.0.1
      provided
    
  dependencies>

  <build>
    <finalName>servletDemofinalName>
    <pluginManagement>
      <plugins>
        <plugin>
          <artifactId>maven-clean-pluginartifactId>
          <version>3.1.0version>
        plugin>
        
        <plugin>
          <artifactId>maven-resources-pluginartifactId>
          <version>3.0.2version>
        plugin>
        <plugin>
          <artifactId>maven-compiler-pluginartifactId>
          <version>3.8.0version>
        plugin>
        <plugin>
          <artifactId>maven-surefire-pluginartifactId>
          <version>2.22.1version>
        plugin>
        <plugin>
          <artifactId>maven-war-pluginartifactId>
          <version>3.2.2version>
        plugin>
        <plugin>
          <artifactId>maven-install-pluginartifactId>
          <version>2.5.2version>
        plugin>
        <plugin>
          <artifactId>maven-deploy-pluginartifactId>
          <version>2.8.2version>
        plugin>
      plugins>
    pluginManagement>
  build>
project>

六、运行结果

Servlet简单例子_第2张图片

Servlet简单例子_第3张图片

你可能感兴趣的:(Servlet简单例子)