第一个servlet程序

费了很大一股劲;

Servlet.java

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

public class ServletTest extends HttpServlet {

    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        response.setHeader("Content-Type", " text/html; charset=utf-8");
        response.getWriter().println("庞静赢是个傻逼");
    }
}
View Code

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>test</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
 <servlet>
     <servlet-name> ServletTest</servlet-name>
     <servlet-class>ServletTest</servlet-class>
 </servlet>

 <servlet-mapping>
       <!-- 映射Servlet -->
       <servlet-name>ServletTest</servlet-name>
       <!--<servlet-name>与上面<Servlet>标签的<servlet-name>元素相对应,不可以随便起名  -->
       <url-pattern>/ServletTest</url-pattern>
       <!-- 上面一句话用于映射访问URL -->
   </servlet-mapping>
</web-app>
View Code

加油!

原文地址:https://www.cnblogs.com/helloworld2019/p/10841605.html