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, HttpServletResponseresponse)

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.01Transitional//EN">

<html>

<head>

<basehref="<%=basePath%>">

 

<title>My JSP 'index.jsp' startingpage</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>

packagecom.jsp_servlet_path.rqq;

 

import java.io.IOException;

importjavax.servlet.ServletException;

importjavax.servlet.http.HttpServlet;

importjavax.servlet.http.HttpServletRequest;

importjavax.servlet.http.HttpServletResponse;

 

publicclassJSP_ServletextendsHttpServlet {

 

publicvoid doGet(HttpServletRequestrequest, HttpServletResponse response)

throwsServletException,IOException {

 

doPost(request,response);

}

 

publicvoid doPost(HttpServletRequestrequest, HttpServletResponse response)

throwsServletException,IOException {

//请求转发,在服务器端,"/"代表(相对于)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;

}

}

}

packagecom.jsp_servlet_path.rqq;

 

import java.io.IOException;

import java.io.PrintWriter;

 

importjavax.servlet.ServletException;

importjavax.servlet.http.HttpServlet;

importjavax.servlet.http.HttpServletRequest;

importjavax.servlet.http.HttpServletResponse;

 

publicclassCopyOfJSP_ServletextendsHttpServlet {

 

publicvoid doGet(HttpServletRequestrequest, HttpServletResponse response)

throwsServletException,IOException {

 

doPost(request,response);

}

 

publicvoid doPost(HttpServletRequestrequest, HttpServletResponse response)

throwsServletException,IOException {

 

response.setContentType("text/html");

PrintWriterout = response.getWriter();

out.println("<!DOCTYPE HTML PUBLIC\"-//W3C//DTD HTML4.01Transitional//EN\">");

out.println("<HTML>");

out.println("<HEAD><TITLE>AServlet</TITLE></HEAD>");

out.println("<BODY>");

out.print("This is ");

out.print(this.getClass());

out.println(", usingthe POST method");

out.println("</BODY>");

out.println("</HTML>");

out.flush();

out.close();

}

}

<%@ 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.01Transitional//EN">

<html>

<head>

<basehref="<%=basePath%>">

 

<title>My JSP 'a.jsp' startingpage</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>

 

<body>

This is my a.jsp page. <br>

<ahref="b/b.jsp">b.jsp(b/b.jsp)</a><br/>

<ahref="/JSP_Servlet_Path/b/b.jsp">b.jsp(/JSP_Servlet_Path/b/b.jsp)</a><br/>

<ahref="index.jsp">index.jsp</a><br/>

<ahref="/JSP_Servlet_Path/">index.jsp(/JSP_Servlet_Path/)</a><br/>

<ahref="/JSP_Servlet_Path">index.jsp(/JSP_Servlet_Path)</a><br/>

<ahref="/JSP_Servlet_Path/index.jsp">index.jsp(/JSP_Servlet_Path/index.jsp)</a><br/>

<ahref="servlet/JSP_Servlet?i=1">JSP_Servlet(servlet/JSP_Servlet?i=1)</a>

<ahref="/JSP_Servlet_Path/servlet/JSP_Servlet?i=1">

JSP_Servlet(/JSP_Servlet_Path/servlet/JSP_Servlet?i=1)</a>

</body>

</html>

 

 

 

 

 

 

<%@ page language="java"import="java.util.*"pageEncoding="ISO-8859-1"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

 

<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML 4.01Transitional//EN">

<html>

<head>

<basehref="<%=basePath%>">

 

<title>My JSP 'b.jsp' startingpage</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>

 

<body>

This is b.jsp page. <br>

<ahref="index.jsp">index.jsp()</a><br/>

<ahref="a/a.jsp">a.jsp(a/a.jsp)</a><br/>

<ahref="a/c/c.jsp">c.jsp(a/c/c.jsp)</a><br/>

<ahref="servlet/JSP_Servlet?i=2">JSP_Servlet(servlet/JSP_Servlet?i=2)</a><br/>

<ahref="/JSP_Servlet_Path/servlet/JSP_Servlet?i=2">

JSP_Servlet(/JSP_Servlet_Path/servlet/JSP_Servlet?i=2)</a>

</body>

</html>

<%@ page language="java"import="java.util.*"pageEncoding="ISO-8859-1"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

 

<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML 4.01Transitional//EN">

<html>

<head>

<basehref="<%=basePath%>">

 

<title>My JSP 'c.jsp' startingpage</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>

 

<body>

This is c.jsp page. <br>

<ahref="a/a.jsp">a.jsp(a/a.jsp)</a><br/>

<ahref="a/c/e/e.jsp">e.jsp(a/c/e/e.jsp)</a><br/>

<ahref="servlet/JSP_Servlet?i=3">JSP_Servlet(servlet/JSP_Servlet?i=3)</a><br/>

<ahref="/JSP_Servlet_Path/servlet/JSP_Servlet?i=3">

JSP_Servlet(/JSP_Servlet_Path/servlet/JSP_Servlet?i=3)</a>

</body>

</html>

注:既然使用了baseBase就不需要使用../这样方式了!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值