• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

java后端重定向

武飞扬头像
飞羽小菜鸡
帮助1

两个问题

  1. Servlet中,重定向之后的代码是否会继续执行?
  2. 重定向是在所有代码执行完毕后跳转,还是执行到重定向代码时立即跳转?

1.重定向之后的代码会继续执行

2.当前程序所有代码执行完毕后,才会执行重定向跳转

3.重定向之后,加上return,可让之后的代码不再执行

boolean flag = true;
if (flag) {
    response.sendRedirect("url");
    return;
}

要点:重定向和请求转发的区别

  1. 重定向是客户端行为,请求转发是服务器行为
  2. 重定向是response对象调用方法,请求转发是request对象调用方法
  3. 重定向是多次请求、多次响应,请求转发只有一次请求所以可以实现request域对象中的数据共享
  4. 重定向由服务端将重定向请求返回给客户端,再由客户端发起,而请求转发直接由服务器发起,效率要高于重定向
  5. 由于重定向客户端会出现两次请求访问,而请求转发是服务端行为,服务器直接转发,所以重定向效率较低
  6. 重定向地址栏会发生变化,请求转发url地址栏不变
  7. 重定向可以访问外部资源,而请求转发只能访问内部资源

后端重定向流程图:

学新通

后端重定向默认以get方式

后端使用post方式重定向

  1.  
    public class RedirectWithPost {
  2.  
    Map<String, String> parameter = new HashMap<String, String>();
  3.  
    HttpServletResponse response;
  4.  
     
  5.  
    public RedirectWithPost(HttpServletResponse response) {
  6.  
    this.response = response;
  7.  
    }
  8.  
     
  9.  
    public void setParameter(String key, String value) {
  10.  
    this.parameter.put(key, value);
  11.  
    }
  12.  
     
  13.  
    public void sendByPost(String url) throws IOException {
  14.  
    this.response.setContentType("text/html");
  15.  
    response.setCharacterEncoding("utf-8");
  16.  
    response.setContentType("text/html;charset=utf-8");
  17.  
    PrintWriter out = this.response.getWriter();
  18.  
    out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
  19.  
    out.println("<HTML>");
  20.  
    out.println(" <HEAD>");
  21.  
    out.println(" <meta http-equiv=Content-Type content=\"text/html; charset=utf-8\">");
  22.  
    out.println(" <TITLE>loading</TITLE>");
  23.  
    out.println(" <meta http-equiv=\"Content-Type\" content=\"text/html charset=GBK\">\n");
  24.  
    out.println(" </HEAD>");
  25.  
    out.println(" <BODY>");
  26.  
    out.println("<form name=\"submitForm\" action=\"" url "\" method=\"post\">");
  27.  
    Iterator<String> it = this.parameter.keySet().iterator();
  28.  
    while (it.hasNext()) {
  29.  
    String key = it.next();
  30.  
    out.println("<input type=\"hidden\" name=\"" key "\" value=\"" this.parameter.get(key) "\"/>");
  31.  
    }
  32.  
    out.println("</from>");
  33.  
    out.println("<script>window.document.submitForm.submit();</script> ");
  34.  
    out.println(" </BODY>");
  35.  
    out.println("</HTML>");
  36.  
    out.flush();
  37.  
    out.close();
  38.  
    }
  39.  
    }
  40.  
     
学新通

后端post重定向

  1.  
    private void loginTokenApi(HttpServletResponse httpServletResponse, String username, String password) throws IOException, ServletException {
  2.  
    RedirectWithPost redirectWithPost = new RedirectWithPost(httpServletResponse);
  3.  
    //redirectUrl请求地址
  4.  
    String redirectUrl = "/loginRecordApi";
  5.  
    redirectWithPost.setParameter("username", username);
  6.  
    redirectWithPost.setParameter("password", password);
  7.  
    redirectWithPost.sendByPost(redirectUrl);
  8.  
    }

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhgakfib
系列文章
更多 icon
同类精品
更多 icon
继续加载