-
构建1个web项目,导入依赖
-
编写TestServlet
| @WebServlet(name = "testServlet",urlPatterns = {"/test"}) |
| public class TestServlet extends HttpServlet { |
| |
| @Override |
| protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
| resp.setContentType("text/html;charset=utf-8"); |
| PrintWriter printWriter = resp.getWriter(); |
| printWriter.write("<div> TestServlet doGet </div>"); |
| } |
| |
| } |
-
配置端口

-
测试

-
请求转发
-
新建1个admin.jsp
-
编写DispatchServlet
| @WebServlet("/forward") |
| public class DispatchServlet extends HttpServlet { |
| |
| @Override |
| protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
| System.out.println("DispatchServlet doGet"); |
| req.getRequestDispatcher("/WEB-INF/admin.jsp").forward(req,resp); |
| } |
| |
| } |
| # 访问http://localhost:8080/index.jsp可以访问到页面 |
| # 访问http://localhost:8080/admin.jsp无法访问页面 |
| # 访问http://localhost:8080/WEB-INF/admin.jsp无法访问页面 |
| # 访问http://localhost:8080/forward可以访问页面admin.jsp |
| |
| # servlet内部转发 |
| @Override |
| protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
| System.out.println("DispatchServlet doGet"); |
| req.getRequestDispatcher("request").forward(req,resp); |
| } |
| http://localhost:8080/forward?userName=jack&age=11&sport=ball&sport=sleep |
| DispatchServlet doGet |
| 应用上下文路径 getContextPath= |
| 客户端发出请求时的完整URL getRequestURL=http://localhost:8080/request |
| 请求行中的资源名部分 getRequestURI=/request |
| 请求行中的参数部分 getQueryString=userName=jack&age=11&sport=ball&sport=sleep |
| 发出请求的客户机的IP地址 getRemoteAddr=0:0:0:0:0:0:0:1 |
| 客户机发请求使用的网络端口号 getRemotePort=51171 |
| 获取请求头 getHeader(Accept)=text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9 |
| 客户端请求参数 getParameter=jack |
| 客户端请求参数列表,多个值 getParameterValues=[Ljava.lang.String;@57d4dac |
| 客户端请求参数封装成的map类型 getParameterMap=org.apache.catalina.util.ParameterMap@68a7bf4d |
| # DispatchServlet中编写入下 |
| @Override |
| protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
| System.out.println("DispatchServlet doGet"); |
| req.setAttribute("name","xdclass"); |
| req.getRequestDispatcher("/index.jsp").forward(req,resp); |
| } |
| |
| |
| http://localhost:8080/index.jsp |
| |
| http://localhost:8080/forward |


| public class User { |
| private int id; |
| private String name; |
| private String host; |
| |
| public int getId() { |
| return id; |
| } |
| |
| public void setId(int id) { |
| this.id = id; |
| } |
| |
| public String getName() { |
| return name; |
| } |
| |
| public void setName(String name) { |
| this.name = name; |
| } |
| |
| public String getHost() { |
| return host; |
| } |
| |
| public void setHost(String host) { |
| this.host = host; |
| } |
| |
| } |
| @Override |
| protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
| System.out.println("DispatchServlet doGet"); |
| User user = new User(); |
| user.setHost("https://xdclass.net"); |
| user.setId(1); |
| user.setName("老王"); |
| req.setAttribute("user",user); |
| req.getRequestDispatcher("/index.jsp").forward(req,resp); |
| } |
| |
| name = ${user.name} |
| id= ${user.id} |
| host= ${user.host} |
| http://localhost:8080/forward |

重定向
,是一个外部的转发,有2次请求,无法获取到领域对象中的值
| @Override |
| protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { |
| System.out.println("DispatchServlet doGet"); |
| req.setAttribute("name","jack"); |
| resp.sendRedirect("/index.jsp"); |
| } |
- 测试

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术