• getRequestURI,getRequestURL的区别,获取各种路径的方法


    getRequestURI,getRequestURL的区别

    test1.jsp=======================

    <a href ="test.jsp?p=fuck">跳转到test2</a>

    test2.jsp=======================

    <%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

    out.println("basePath:"+basePath);
    out.println("<br/>");
    out.println("getContextPath:"+request.getContextPath());
    out.println("<br/>");
    out.println("getServletPath:"+request.getServletPath());
    out.println("<br/>");
    out.println("getRequestURI:"+request.getRequestURI());
    out.println("<br/>");
    out.println("getRequestURL:"+request.getRequestURL());
    out.println("<br/>");
    out.println("getRealPath:"+request.getRealPath("/"));
    out.println("<br/>");
    out.println("getServletContext().getRealPath:"+getServletContext().getRealPath("/"));
    out.println("<br/>");
    out.println("getQueryString:"+request.getQueryString());

    %>

    显示结果:

    basePath:http://localhost:8080/test/

    getContextPath:/test
    getServletPath:/test.jsp
    getRequestURI:/test/test.jsp
    getRequestURL:http://localhost:8080/test/test.jsp
    getRealPath:D:Tomcat 6.0webapps est
    getServletContext().getRealPath:D:Tomcat 6.0webapps est
    getQueryString:p=fuck

    在一些应用中,未登录用户请求了必须登录的资源时,提示用户登录,此时要记住用户访问的当前页面的URL,当他登录成功后根据记住的URL跳回用户最后访问的页面:

    String lastAccessUrl = request.getRequestURL() + "?" + request.getQueryString();

  • 相关阅读:
    linux笔记
    restful课程凌杂知识点
    Django中间件执行流程
    restful知识点之二restframework视图
    restful知识点之一CBV
    可变长参数
    函数的重点内容
    文件的高级应用及修改的两种方式
    文件的三种打开方式及with管理文件上下文
    字符编码
  • 原文地址:https://www.cnblogs.com/shaohz2014/p/3803904.html
Copyright © 2020-2023  润新知