整合营销服务商

电脑端+手机端+微信端=数据同步管理

免费咨询热线:

《Servlet》第25节:HttpServletR

《Servlet》第25节:HttpServletRequest请求转发的功能介绍

ervlet程序中,提供了请求转发和请求重定向两个功能,其中请求转发是指:将一个HTTP请求转发给另一个HTTP请求进行处理,这个过程中,还是只有一次HTTP请求,只是在这一次HTTP请求中间,进行了转发的动作;请求重定向是指:浏览器向服务器发起一次HTTP请求,然后服务器返回给客户端一个重定向状态码以及重定向的地址,浏览器会再次向服务器发起一次HTTP请求,访问重定向之后的地址。下面具体介绍一下请求转发的功能。

1.1、请求转发的流程

请求的转发,是指:在同一次HTTP请求中,服务器将当前这个Servlet程序转发给了另外一个Servlet程序进行处理,最终响应结果是由转发之后的那个Servlet程序返回给客户端的。

转发的特点:

  1. 转发过程中,只涉及一次HTTP请求。
  2. 转发是发生在服务器内部的,也就是在Servlet容器中进行转发的。
  3. 转发过程中,可以共享Request请求对象中的数据信息。
  4. 转发时候,浏览器地址栏不会发生变化,还是显示的转发之前的地址。

转发的过程如下图所示:

1.2、转发案例代码

要实现请求的转发,那就需要从HttpServletRequest请求对象中,调用getRequestDispatcher()方法,获取到请求转发器对象RequestDispatcher,并且指定转发后的地址,最后调用forward()方法,完成请求的转发。

创建FirstServlet程序:

package com.gitcode.servlet;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @version 1.0.0
 * @Date: 2024/2/12 19:41
 * @Author ZhuYouBin
 * @Description:
 */
public class FirstServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置请求数据
        request.setAttribute("username", "这是FirstServlet中保存的数据!");
        // 获取请求转发器,指定转发的地址
        RequestDispatcher requestDispatcher=request.getRequestDispatcher("/secondServlet");
        // 调用 forward() 方法进行转发
        requestDispatcher.forward(request, response);
    }
}

创建SecondServlet程序:

package com.gitcode.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @version 1.0.0
 * @Date: 2024/2/12 19:41
 * @Author ZhuYouBin
 * @Description:
 */
public class SecondServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        // 由 FirstServlet 转发过来
        PrintWriter writer=response.getWriter();
        // 响应数据
        Object username=request.getAttribute("username");
        writer.println("<h3>请求转发,获取前一个请求数据:username=" + username + "</h3>");
        // 关闭流
        writer.close();
    }
}

启动程序,浏览器访问【/servlet/firstServlet】地址,此时可以看到界面返回的是SecondServlet程序中返回的内容。

1.3、转发路径的写法

在进行请求转发的时候,需要调用getRequestDispatcher()方法,获取请求转发器对象,这个方法需要接收转发后的路径地址,那么这个路径地址要如何设置呢???

  1. 情况一:路径是绝对路径,即:路径是采用【/】斜杠开头,那么请求转发器,会直接使用这个路径进行转发。
  2. 情况二:路径是相对路径,即:路径不是采用【/】斜杠开头,那么请求转发器,会获取到当前Servlet程序的路径地址,并且拼接上转发后的路径地址。

getRequestDispatcher()方法源代码如下所示:

public RequestDispatcher getRequestDispatcher(String path) {
    Context context=this.getContext();
    if (context==null) {
        return null;
    } else if (path==null) {
        return null;
    } else {
        int fragmentPos=path.indexOf(35);
        if (fragmentPos > -1) {
            log.warn(sm.getString("request.fragmentInDispatchPath", new Object[]{path}));
            path=path.substring(0, fragmentPos);
        }

        if (path.startsWith("/")) {
            return context.getServletContext().getRequestDispatcher(path);
        } else {
            String servletPath=(String)this.getAttribute("javax.servlet.include.servlet_path");
            if (servletPath==null) {
                servletPath=this.getServletPath();
            }

            String pathInfo=this.getPathInfo();
            String requestPath=null;
            if (pathInfo==null) {
                requestPath=servletPath;
            } else {
                requestPath=servletPath + pathInfo;
            }

            int pos=requestPath.lastIndexOf(47);
            String relative=null;
            if (context.getDispatchersUseEncodedPaths()) {
                if (pos >=0) {
                    relative=URLEncoder.DEFAULT.encode(requestPath.substring(0, pos + 1), StandardCharsets.UTF_8) + path;
                } else {
                    relative=URLEncoder.DEFAULT.encode(requestPath, StandardCharsets.UTF_8) + path;
                }
            } else if (pos >=0) {
                relative=requestPath.substring(0, pos + 1) + path;
            } else {
                relative=requestPath + path;
            }

            return context.getServletContext().getRequestDispatcher(relative);
        }
    }
}

以上,就是HttpServletRequest请求的转发。

今天就到这里,未完待续~~

理了23个跟传统历史文化有关的网站,千万不要错过~

1.故宫博物院藏品总目:{url:https%3A//www.urlshare.cn/umirror_url_check?_wv=1&srctype=touch&apptype=android&loginuin=3021168867&plateform=mobileqq&url=https%253A%252F%252Fzm-digicol.dpm.org.cn%252F&src_uin=2941347079&src_scene=7035&cli_scene=getDetail,text:https%3A//zm-digicol.dpm.org.cn/}

2.汉典古籍:{url:https%3A//www.urlshare.cn/umirror_url_check?_wv=1&srctype=touch&apptype=android&loginuin=3021168867&plateform=mobileqq&url=http%253A%252F%252Ft.cn%252FGt1aM&src_uin=2941347079&src_scene=7035&cli_scene=getDetail,text:http%3A//t.cn/Gt1aM}

3.中国古籍网:{url:https%3A//www.urlshare.cn/umirror_url_check?_wv=1&srctype=touch&apptype=android&loginuin=3021168867&plateform=mobileqq&url=https%253A%252F%252Fc.pc.qq.com%252Fmiddlem.html%253Fpfurl%253Dhttp%25253A%25252F%25252Ft.cn%25252FzOqoclW%2526gjsublevel%253D2804%2526pfuin%253D83781672%2526pfto%253Dmqq.qzone%2526type%253D0%2526gjlevel%253D15&src_uin=2941347079&src_scene=7035&cli_scene=getDetail,text:https%3A//c.pc.qq.com/middlem.html?pfurl=http%253A%252F%252Ft.cn%252FzOqoclW&gjsublevel=2804&pfuin=83781672&pfto=mqq.qzone&type=0&gjlevel=15}

4.中国历史地图集:{url:https%3A//www.urlshare.cn/umirror_url_check?_wv=1&srctype=touch&apptype=android&loginuin=3021168867&plateform=mobileqq&url=http%253A%252F%252Ft.cn%252FzOZ8DRT&src_uin=2941347079&src_scene=7035&cli_scene=getDetail,text:http%3A//t.cn/zOZ8DRT}

5.中国传统颜色网站:{url:https%3A//www.urlshare.cn/umirror_url_check?_wv=1&srctype=touch&apptype=android&loginuin=3021168867&plateform=mobileqq&url=http%253A%252F%252Fzhongguose.com%252F&src_uin=2941347079&src_scene=7035&cli_scene=getDetail,text:http%3A//zhongguose.com/}

6.历代诗人地域分布:{url:https%3A//www.urlshare.cn/umirror_url_check?_wv=1&srctype=touch&apptype=android&loginuin=3021168867&plateform=mobileqq&url=http%253A%252F%252Ft.cn%252FESIy0mp&src_uin=2941347079&src_scene=7035&cli_scene=getDetail,text:http%3A//t.cn/ESIy0mp}

7.中国历代人物图像数据库:{url:https%3A//www.urlshare.cn/umirror_url_check?_wv=1&srctype=touch&apptype=android&loginuin=3021168867&plateform=mobileqq&url=http%253A%252F%252Ft.cn%252FzOZns18&src_uin=2941347079&src_scene=7035&cli_scene=getDetail,text:http%3A//t.cn/zOZns18}

8.中国地方志数据库:{url:https%3A//www.urlshare.cn/umirror_url_check?_wv=1&srctype=touch&apptype=android&loginuin=3021168867&plateform=mobileqq&url=http%253A%252F%252Flcd.ccnu.edu.cn%252F%2523%252Findex&src_uin=2941347079&src_scene=7035&cli_scene=getDetail#/index,text:http%3A//lcd.ccnu.edu.cn/#/index}

9.国学导航:{url:https%3A//www.urlshare.cn/umirror_url_check?_wv=1&srctype=touch&apptype=android&loginuin=3021168867&plateform=mobileqq&url=https%253A%252F%252Fc.pc.qq.com%252Fmiddlem.html%253Fpfurl%253Dhttp%25253A%25252F%25252Ft.cn%25252FRyhWagD%2526gjsublevel%253D2804%2526pfuin%253D83781672%2526pfto%253Dmqq.qzone%2526type%253D0%2526gjlevel%253D15&src_uin=2941347079&src_scene=7035&cli_scene=getDetail,text:https%3A//c.pc.qq.com/middlem.html?pfurl=http%253A%252F%252Ft.cn%252FRyhWagD&gjsublevel=2804&pfuin=83781672&pfto=mqq.qzone&type=0&gjlevel=15}

10.古籍馆:{url:https%3A//www.urlshare.cn/umirror_url_check?_wv=1&srctype=touch&apptype=android&loginuin=3021168867&plateform=mobileqq&url=https%253A%252F%252Fwww.gujiguan.com%252F&src_uin=2941347079&src_scene=7035&cli_scene=getDetail,text:https%3A//www.gujiguan.com/}

11.中医古籍全文数据库:{url:https%3A//www.urlshare.cn/umirror_url_check?_wv=1&srctype=touch&apptype=android&loginuin=3021168867&plateform=mobileqq&url=http%253A%252F%252Ft.cn%252FRLXw2ko&src_uin=2941347079&src_scene=7035&cli_scene=getDetail,text:http%3A//t.cn/RLXw2ko}

12.全历史:{url:https%3A//www.urlshare.cn/umirror_url_check?_wv=1&srctype=touch&apptype=android&loginuin=3021168867&plateform=mobileqq&url=https%253A%252F%252Fwww.allhistory.com%252F&src_uin=2941347079&src_scene=7035&cli_scene=getDetail,text:https%3A//www.allhistory.com/}

13.国学大师:{url:https%3A//www.urlshare.cn/umirror_url_check?_wv=1&srctype=touch&apptype=android&loginuin=3021168867&plateform=mobileqq&url=http%253A%252F%252Fwww.guoxh&src_uin=2941347079&src_scene=7035&cli_scene=getDetail,text:http%3A//www.guoxh}ttp://t.cn/A6q84dGA

14.中国京剧戏考:{url:https%3A//www.urlshare.cn/umirror_url_check?_wv=1&srctype=touch&apptype=android&loginuin=3021168867&plateform=mobileqq&url=http%253A%252F%252Ft.cn%252FA6q84dGA&src_uin=2941347079&src_scene=7035&cli_scene=getDetail,text:http%3A//t.cn/A6q84dGA}

15.书法空间:{url:https%3A//www.urlshare.cn/umirror_url_check?_wv=1&srctype=touch&apptype=android&loginuin=3021168867&plateform=mobileqq&url=http%253A%252F%252Ft.cn%252FRxQPAqc&src_uin=2941347079&src_scene=7035&cli_scene=getDetail,text:http%3A//t.cn/RxQPAqc}

16.发现中国:{url:https%3A//www.urlshare.cn/umirror_url_check?_wv=1&srctype=touch&apptype=android&loginuin=3021168867&plateform=mobileqq&url=http%253A%252F%252Ft.cn%252FRYI2Iaj&src_uin=2941347079&src_scene=7035&cli_scene=getDetail,text:http%3A//t.cn/RYI2Iaj}

17.中国历史学习网:{url:https%3A//www.urlshare.cn/umirror_url_check?_wv=1&srctype=touch&apptype=android&loginuin=3021168867&plateform=mobileqq&url=http%253A%252F%252Ft.cn%252FA6ICUI2U&src_uin=2941347079&src_scene=7035&cli_scene=getDetail,text:http%3A//t.cn/A6ICUI2U}

18.读典籍:{url:https%3A//www.urlshare.cn/umirror_url_check?_wv=1&srctype=touch&apptype=android&loginuin=3021168867&plateform=mobileqq&url=http%253A%252F%252Ft.cn%252FA655he6S&src_uin=2941347079&src_scene=7035&cli_scene=getDetail,text:http%3A//t.cn/A655he6S}

19.古今文字集成:{url:https%3A//www.urlshare.cn/umirror_url_check?_wv=1&srctype=touch&apptype=android&loginuin=3021168867&plateform=mobileqq&url=https%253A%252F%252Fc.pc.qq.com%252Fmiddlem.html%253Fpfurl%253Dhttp%25253A%25252F%25252Ft.cn%25252FRLdkvFV%2526gjsublevel%253D2804%2526pfuin%253D83781672%2526pfto%253Dmqq.qzone%2526type%253D0%2526gjlevel%253D15&src_uin=2941347079&src_scene=7035&cli_scene=getDetail,text:https%3A//c.pc.qq.com/middlem.html?pfurl=http%253A%252F%252Ft.cn%252FRLdkvFV&gjsublevel=2804&pfuin=83781672&pfto=mqq.qzone&type=0&gjlevel=15}

20.历史剧里看历史:{url:https%3A//www.urlshare.cn/umirror_url_check?_wv=1&srctype=touch&apptype=android&loginuin=3021168867&plateform=mobileqq&url=http%253A%252F%252Ft.cn%252F8kIgi9a&src_uin=2941347079&src_scene=7035&cli_scene=getDetail,text:http%3A//t.cn/8kIgi9a}

21.成语查询:{url:https%3A//www.urlshare.cn/umirror_url_check?_wv=1&srctype=touch&apptype=android&loginuin=3021168867&plateform=mobileqq&url=http%253A%252F%252Ft.cn%252FhRDRm&src_uin=2941347079&src_scene=7035&cli_scene=getDetail,text:http%3A//t.cn/hRDRm}

22.搜韵:{url:https%3A//www.urlshare.cn/umirror_url_check?_wv=1&srctype=touch&apptype=android&loginuin=3021168867&plateform=mobileqq&url=https%253A%252F%252Fwww.sou-yun.cn%252F&src_uin=2941347079&src_scene=7035&cli_scene=getDetail,text:https%3A//www.sou-yun.cn/}

23.唐宋文学编年地图 :{url:https%3A//www.urlshare.cn/umirror_url_check?_wv=1&srctype=touch&apptype=android&loginuin=3021168867&plateform=mobileqq&url=https%253A%252F%252Fsou-yun.cn%252FMPoetLifeMap.aspx&src_uin=2941347079&src_scene=7035&cli_scene=getDetail,text:https%3A//sou-yun.cn/MPoetLifeMap.aspx}

「整理不易 路过的伙伴们点个赞 喜欢的关注收藏转发」

一次线上nginx转发https

先简单的介绍下http和https,HTTP(HyperText Transfer Protocol:超文本传输协议)是一种用于分布式、协作式和超媒体信息系统的应用层协议。 简单来说就是一种发布和接收 HTML 页面的方法,被用于在 Web 浏览器和网站服务器之间传递信息,HTTPS是安全的HTTP。HTTPS 主要由两部分组成:HTTP + SSL / TLS,也就是在 HTTP 上面又加了一层处理加密信息的模块。服务端和客户端的信息传输都会通过 TLS 进行加密,所以传输的数据都是加密后的数据。具体的HTTPS原理解析请参考我的另一篇文章一文读懂HTTPS的实现原理

      • PS:ssl.zip是我事先使用Openssl生成好的。
      • PPS:业务相关不具备普适性。

Web访问地址由http修改为https

  • 解压此ssl.zip压缩包,放在nginx目录下,如图:


  • 修改nginx.conf配置文件

  • 重启nginx生效。