package org.west.demo; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; public class ServletTest extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("nihao servlet"); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); } }
配置web
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <servlet> <servlet-name>ServletTest</servlet-name> <servlet-class>org.west.demo.ServletTest</servlet-class> </servlet> <!--========================================================================--> <servlet-mapping> <servlet-name>ServletTest</servlet-name> <url-pattern>/test01</url-pattern> </servlet-mapping> </web-app>
这样我们通过网页和服务器就可以在控制台进行交互了。
原文:https://www.cnblogs.com/xiaoqiqistudy/p/11191086.html