在写javaweb项目的时候,总会遇到路径书写的问题,现在将其作个总结。
在javaweb中需要书写路径的地方主要有这四大类:
客服端路径
超链接
表单
重定向
服务器端路径
转发
包含
资源获取路径
servletContext获取资源
ClassLoader获取资源
Class获取资源
路径
现分别作介绍
其构建的javaweb如下:
1客服端路径
A超链接
pageEncoding="UTF-8"%>
html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
页面A这是绝对地址超链接
这是以"/"开头的相对地址超链接
这是不以"/"开头的相对地址超链接
B表单
pageEncoding="UTF-8"%>
html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
Insert title hereusername:
username:
username:
username:
username:
username:
C重定向
package cn.ccnu.path;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/*
* 重定向有三种路径书写方式
* 1.绝对路径
* 2.以"/"开头的相对路径
* 3.不以"/"开头的相对路径
*/
public class RedirectServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.sendRedirect("http://localhost:8080/javaee/jsp/b.jsp");
/*
* 2.以"/"开头的相对路径
* 此时,/代表整个web工程的路径,即http://localhost:8080/
*/
// response.sendRedirect("/javaee/jsp/b.jsp");
/*
* 3.不以"/"开头的相对路径
* 此时是相对于当前资源的相对路径
* 当前资源路径为:http://localhost:8080/javaee/RedirectServlet
* 即表示:RedirectServlet在路径http://localhost:8080/javaee之下
* 而b.jsp在http://localhost:8080/javaee/jsp/b.jsp
* 所以最终地址写为:jsp/b.jsp
*/
// response.