在Jsp中调用静态资源,路径配置问题,jsp获取路径的一些方法

在Jsp中调用图片、JS脚本等,针对取得的路径有两种调用方式:

1、放入Body中生成绝对路径(不建议)

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title>image调用</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">   
</head>
<body>
    <h1>图片访问</h1>
    <div>         
        <img alt="图片" src="<%=basePath%>/image/a.png">
    </div>
</body>
</html>

2、在Head中指定,Body中使用相对路径(建议)

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title>image调用</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">    
    <!-- base需要放到head中 -->    
    <base href=" <%=basePath%>">   
</head>
<body>
    <h1>图片访问</h1>
    <div>      
        <img alt="图片" src="image/a.png">
    </div>
</body>
</html>

转载自:https://www.cnblogs.com/maocs/p/4256302.html

 

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.0\webapps\test\ 
getServletContext().getRealPath:D:\Tomcat 6.0\webapps\test\ 
getQueryString:p=fuck

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

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

 以上转自:http://www.cnblogs.com/JemBai/archive/2010/11/10/1873764.html

posted @ 2018-01-16 15:20  陈无问  阅读(847)  评论(0编辑  收藏  举报