【Servlet】Request请求对象 && Response响应对象 && 资源路径问题

Request&&Response简介

  • 在Servlet中,Request对象和Response对象是两个重要的接口,它们用于处理客户端发来的请求和向客户端发送响应。

Request对象

Request:获取请求数据

Request继承体系



Request获取请求数据


Request使用通用方式获取请求参数


Request请求参数中中文乱码问题


Request完成请求转发


Response对象

Response:设置响应数据

Response继承体系


Response设置响应数据


Response完成重定向


Response响应字符数据

设置字符数据的响应体

Response响应字节数据

设置字节数据的响应体

资源路径问题


动态获取虚拟目录

tomcat配置虚拟路径
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.tomcat.maven</groupId>
        <artifactId>tomcat7-maven-plugin</artifactId>
        <version>2.1</version>

        <configuration>
          <path>/virtual-path</path>
        </configuration>
      </plugin>
    </plugins>
  </build>
获取虚拟路径
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取虚拟路径
        String contextPath = req.getContextPath();
        // 配置资源路径
        resp.sendRedirect(contextPath + "/resp2");
    }
posted @ 2024-01-21 19:50  沙汀鱼  阅读(4)  评论(0编辑  收藏  举报