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>

六、運行結果


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM