"." -- 代表目前所在的目录,相对路径。 如:<a href="./abc">文本</a> 或 <a href="abc">
".." -- 代表上一层目录,相对路径。 如:<a href="../abc">文本</a>
"../../" -- 代表的是上一层目录的上一层目录,相对路径。 如:<img src="../../abc" />
"/" -- 代表根目录,绝对路径。 如:<a href="/abc">文本</a> 或 <img src="/abc" />
"D:/abc/" -- 代表根目录,绝对路径。
服务器端地址(tomcat解析)
服务器端的绝对地址指的是相对于你的web应用的地址(http://localhost:8080/yourwebapp/),是由tomcat解析。在jsp和servlet中路径前的"/"代表webapp的根目录。
其用到的地方有:forward、url-pattern
servlet中request.getRequestDispatcher(address);假设你要forward到项目根目录下的user目录下a.jsp应该这么写:
request.getRequestDispatcher("/user/a.jsp").forward(request, response);
这个"/"当前的web应用yourwebapp根目录,其绝对URL地址就是:
http://localhost:8080/yourwebapp/user/a.jsp。
然而sendRedirect中redirect是客户端的行为,所以应该是以浏览器解析的方式来书写url;
/yourwebapp/user/a.jsp
虽然看了这么多,也发现自己的路径肯定没有问题,但是css样式就是加载不出来,探究了好久,终于发现,是自己的过滤器写错了,
package filter;
import javax.servlet.*;
import java.io.IOException;
public class EncodingFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
servletRequest.setCharacterEncoding("utf-8");
servletResponse.setCharacterEncoding("utf-8");
servletResponse.setContentType("text/html;charset=utf-8");
filterChain.doFilter(servletRequest,servletResponse);
}
@Override
public void destroy() {
}
}
我把所有的文件都过滤成 text/html所以会出现这样的错误
package filter;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class EncodingFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
servletRequest.setCharacterEncoding("utf-8");
servletResponse.setCharacterEncoding("utf-8");
HttpServletRequest req=(HttpServletRequest)servletRequest;
HttpServletResponse resp=(HttpServletResponse)servletResponse;
req.setCharacterEncoding("UTF-8");
//返回页面设置
String url = req.getRequestURI();
System.out.println("url:" + url);
if (url.indexOf(".css") > 0 || url.indexOf(".js") > 0 || url.indexOf(".png") > 0) {
filterChain.doFilter(req, resp);
return;
}
servletResponse.setContentType("text/html;charset=utf-8");
filterChain.doFilter(servletRequest,servletResponse);
}
@Override
public void destroy() {
}
}
改成这样,也不对劲。。。还是没有加载出css我服了
又单独加载了一个过滤器
package filter;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class EncodingFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
servletRequest.setCharacterEncoding("utf-8");
servletResponse.setCharacterEncoding("utf-8");
HttpServletRequest req=(HttpServletRequest)servletRequest;
HttpServletResponse resp=(HttpServletResponse)servletResponse;
//返回页面设置
String url = req.getRequestURI();
System.out.println("url:" + url);
if (url.indexOf(".css") > 0 || url.indexOf(".js") > 0 || url.indexOf(".png") > 0) {
filterChain.doFilter(req, resp);
System.out.println("------------");
return;
}
filterChain.doFilter(req,resp);
}
@Override
public void destroy() {
}
}
还是没有用
不知道为啥重建了一个项目,代码原封不动的放进去,好了,我真的服了这个玄学