写类:
1 package com.wang.servlet; 2 3 import javax.servlet.RequestDispatcher; 4 import javax.servlet.ServletContext; 5 import javax.servlet.ServletException; 6 import javax.servlet.http.HttpServlet; 7 import javax.servlet.http.HttpServletRequest; 8 import javax.servlet.http.HttpServletResponse; 9 import java.io.IOException; 10 11 public class ServletDemo04 extends HttpServlet { 12 @Override 13 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 14 ServletContext context = this.getServletContext(); 15 System.out.println("进入了ServletDemo04"); 16 RequestDispatcher requestDispatcher = context.getRequestDispatcher("/gp"); 17 requestDispatcher.forward(req,resp); 18 } 19 20 @Override 21 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 22 doGet(req, resp); 23 } 24 }
1 <!--注册和映射--> 2 <servlet> 3 <servlet-name>sd4</servlet-name> 4 <servlet-class>com.wang.servlet.ServletDemo04</servlet-class> 5 </servlet> 6 <servlet-mapping> 7 <servlet-name>sd4</servlet-name> 8 <url-pattern>/sd4</url-pattern> 9 </servlet-mapping>
原文:https://www.cnblogs.com/WZ-BeiHang/p/12562803.html