JSP&Servlet路径问题
如果带WebRoot,那么js、css、img都应该放到WebRoot目录下,否则访问会有问题。千万不要放在WEB-INF下,因为WEB-INF下的内容只有服务器转发可以访问到,出于安全考虑。
如果不带有WebRoot目录,那么可以放在WEB-INF外面(建立的文件夹中)。
一、JSP、Servlet中的相对路径
a) 在JSP中
“/”代表站点(服务器)根目录http://127.0.0.1/
b) 在Servlet中
“/”代表Web应用的根目录http://127.0.0.1/JSP_Servlet_Path/
request.getRequestDispatcher("/a/a.jsp").forward(request, response);
相对路径/a/a.jsp中/相对于web应用的根目录,所以相当于请求跳转到绝对路径
http://127.0.0.1/JSP_Servlet_Path/a/a.jsp
response.sendRedirect("/JSP_Servlet_Path/b/b.jsp");
因为重定向中的方法是传递给浏览器,用于重新发送请求的,而在浏览器端“/”代表,相对于站点根目录http://127.0.0.1/,所以这里必须要加上/JSP_Servlet_Path,这样浏览器重新请求的地址为:http://127.0.0.1/JSP_Servlet_Path/b/b.jsp
package com.jsp_servlet_path.rqq;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class JSP_Servlet extends HttpServlet {
@Override
public voiddoGet(HttpServletRequest request, HttpServletResponse response)
throwsServletException, IOException {
doPost(request,response);
}
@Override
public voiddoPost(HttpServletRequest request, HttpServletResponse response)
throwsServletException, IOException {
System.out.println(request.getContextPath());
//请求转发,在服务器端,"/"代表(相对于)web应用http://localhost:8000/JSP_Servlet_Path/
//所以请求转发的绝对路径为http://localhost:8000/JSP_Servlet_Path/a/a.jsp
// request.getRequestDispatcher("/a/a.jsp").forward(request,response);
//请求重定向,将方法中的地址参数传递给浏览器,让浏览器重新发送请求,
//"/"代表(相对于)服务器站点http://localhost:8000/
//所以相当于浏览器重新请求了绝对路径http://localhost:8000/JSP_Servlet_Path/b/b.jsp
response.sendRedirect("/JSP_Servlet_Path/b/b.jsp");
//response.sendRedirect(request.getContextPath()+ "/b/b.jsp");
}
}
二、JSP中加入basePath
<%
String path = request.getContextPath();
String basePath= request.getScheme()+"://"
+request.getServerName()+":"
+request.getServerPort()+path+"/";
%>
<head>
<base href="<%=basePath%>">
注:相当于所有的href相对路径前面都加入了:
http://localhost:8000/JSP_Servlet_Path/
三、JSP与Servlet相互访问
<?xmlversion="1.0"encoding="UTF-8"?>
<web-appversion="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>JSP_Servlet</servlet-name>
<servlet-class>com.jsp_servlet_path.rqq.JSP_Servlet</servlet-class>
</servlet>
<servlet>
<servlet-name>CopyOfJSP_Servlet</servlet-name>
<servlet-class>com.jsp_servlet_path.rqq.CopyOfJSP_Servlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>JSP_Servlet</servlet-name>
<url-pattern>/servlet/JSP_Servlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>CopyOfJSP_Servlet</servlet-name>
<url-pattern>/servlet/CopyOfJSP_Servlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
<%@ page language="java"import="java.util.*"pageEncoding="GBK"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPEHTMLPUBLIC "-//W3C//DTDHTML 4.01 Transitional//EN">
<html>
<head>
<basehref="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<metahttp-equiv="pragma"content="no-cache">
<metahttp-equiv="cache-control"content="no-cache">
<metahttp-equiv="expires"content="0">
<metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">
<metahttp-equiv="description"content="This is my page">
<!--
<linkrel="stylesheet" type="text/css"href="styles.css">
-->
</head>
<bodybackground="/JSP_Servlet_Path/img/main1.jpg">
This is index.jsp page. <br>
<%--href中隐含了basePath,因为<basehref="<%=basePath%>">--%>
<ahref="a/a.jsp">a.jsp(a/a.jsp)</a><br/>
<ahref="/JSP_Servlet_Path/a/a.jsp">a.jsp(/JSP_Servlet_Path/a/a.jsp)</a><br/>
<ahref="a/c/c.jsp">c.jsp(a/c/c.jsp)</a><br/>
<ahref="a/c/e/e.jsp">e.jsp(a/c/e/e.jsp)</a><br/>
<imgsrc="/JSP_Servlet_Path/img/2012317253390833.jpg"><br/>
<imgsrc="a/main1.jpg"><br/>
<ahref="servlet/JSP_Servlet?i=0">JSP_Servlet(servlet/JSP_Servlet)</a>
<a href="/JSP_Servlet_Path/servlet/JSP_Servlet?i=0">
JSP_Servlet(/JSP_Servlet_Path/servlet/JSP_Servlet)</a><br/>
<ahref="servlet/JSP_Servlet?i=100">请求JSP_Servlet,并转发给CopyOfJSP_Servlet</a>
</body>
</html>
package com.jsp_servlet_path.rqq;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class JSP_Servlet extends HttpServlet {
@Override
public voiddoGet(HttpServletRequest request, HttpServletResponse response)
throwsServletException, IOException {
doPost(request,response);
}
@Override
public voiddoPost(HttpServletRequest request, HttpServletResponse response)
throwsServletException, IOException {
System.out.println(request.getContextPath());
//请求转发,在服务器端,"/"代表(相对于)web应用http://localhost:8000/JSP_Servlet_Path/
//所以请求转发的绝对路径为http://localhost:8000/JSP_Servlet_Path/a/a.jsp
// request.getRequestDispatcher("/a/a.jsp").forward(request,response);
//请求重定向,将方法中的地址参数传递给浏览器,让浏览器重新发送请求,
//"/"代表(相对于)服务器站点http://localhost:8000/
//所以相当于浏览器重新请求了绝对路径http://localhost:8000/JSP_Servlet_Path/b/b.jsp
response.sendRedirect("/JSP_Servlet_Path/b/b.jsp");
//response.sendRedirect(request.getContextPath()+ "/b/b.jsp");
Stringstr_i = request.getParameter("i");
int i =new Integer(str_i);
switch(i) {
case 0:
request.getRequestDispatcher("/index.jsp").forward(request,response);
System.out.println("index.jsp");
break;
case 1:
request.getRequestDispatcher("/a/a.jsp").forward(request, response);
System.out.println("a.jsp");
break;
case 2:
request.getRequestDispatcher("/b/b.jsp").forward(request,response);
System.out.println("b.jsp");
break;
case 3:
request.getRequestDispatcher("/a/c/c.jsp").forward(request,response);
System.out.println("c.jsp");
break;
case 100:
request.getRequestDispatcher("/servlet/CopyOfJSP_Servlet").forward(request,response);
System.out.println("forward to CopyOfJSP_Servlet");
break;
default:
System.out.println("default");
return;
}
}
}
转载于:https://my.oschina.net/fairy1674/blog/549314
上一篇: Zend Framework 1.7.0 正式发布了
下一篇: 第九章:异常处理
推荐阅读
-
JSP使用UTF-8链接MYSQL数据库(UTF8)乱码以及连接失败问题以及更_MySQL
-
colspan分两列排版问题解决
-
打包上传AppStore出现问题:Missing iOS Distribution signing identity for ArtPollo Network
-
PHP6.0安装有关问题,解压后在C:/php/中找不到php.ini-recommended这个文件
-
接受的项目代码问题
-
php unlink 删除有关问题
-
PHP IE中下载附件问题解决方法_PHP教程
-
Mysql 错误问题汇总(不断更新中)_MySQL
-
Mysql 导入SQL脚本乱码的有关问题
-
iOS WKWebView加载HTML字体大小问题