javaweb-14:Response重定向

4、实现重定向

javaweb-14:Response重定向

B一个web资源收到客户端A请求后,B他会通知A客户端去访问另一个web资源C,这个过程叫重定向

常见场景:

  • 用户登录
void sendRedirect(String var1) throws IOException;

测试:

@Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       /*
        resp.setHeader("Location","/response_01_war/image");
        resp.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
        */
        resp.sendRedirect("/response_01_war/image");//重定向
    }

面试题:请你聊聊重定向和转发的区别?

相同点:

  • 页面都会实现跳转

不同点:

  • 请求转发的时候,url不会发生变化
  • 重定向时候,url地址栏会发生变化

javaweb-14:Response重定向

代码show

RequestTest.java

package com.gongyi.servlet;

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 RequestTest extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("进入到了这个方法");
        //处理请求
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        System.out.println(username+":"+password);
        //重定向时候一定要注意路径问题,否则404
        resp.sendRedirect("/response_01_war/success.jsp");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}

web.xml

<?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"
         metadata-complete="true">
    <servlet>
        <servlet-name>downFile</servlet-name>
        <servlet-class>com.gongyi.servlet.FileServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>downFile</servlet-name>
        <url-pattern>/downFile</url-pattern>
    </servlet-mapping>

    <servlet>
        <servlet-name>imageServlet</servlet-name>
        <servlet-class>com.gongyi.servlet.ImageServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>imageServlet</servlet-name>
        <url-pattern>/image</url-pattern>
    </servlet-mapping>

    <servlet>
        <servlet-name>RedirectServlet</servlet-name>
        <servlet-class>com.gongyi.servlet.RedirectServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>RedirectServlet</servlet-name>
        <url-pattern>/redirect</url-pattern>
    </servlet-mapping>

    <servlet>
        <servlet-name>RequestTest</servlet-name>
        <servlet-class>com.gongyi.servlet.RequestTest</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>RequestTest</servlet-name>
        <url-pattern>/login</url-pattern>
    </servlet-mapping>
</web-app>

index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<body>
<%-- 这里提交的路径,需要寻找到项目的路径   --%>
<%-- ${pageContext.request.contextPath} 代表当前的项目   --%>
    <form action="${pageContext.request.contextPath}/login" method="get">
        用户名:<input type="text" name="username"><br>
        密码:<input type="password" name="password"><br>
        <input type="submit">
    </form>
</body>
</html>

success.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
    <head>
        <title>Success</title>
    </head>
    <body>
        <h1>Success</h1>
    </body>
</html>

彩蛋

1.idea的console 乱码

tomcat配置vm参数:

javaweb-14:Response重定向

-Dfile.encoding=UTF-8

2.jsp中文乱码

代码第一行添加

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
上一篇:RedisProtocol-redis协议翻译


下一篇:Servlet-解决response响应中文乱码