从404错误认识到服务器跳转与客户端跳转 两者相对路径的区别

写了一个用户进入首页时,利用servlet过滤器检查用户登入的验证。从中发现sendRedriect和forward关于路径的细节问题。

当输入任意字符,点击"login"按钮时,登入index.jsp页面显示"欢迎登入"。

1.客户端跳转: response.sendRedirect("index.jsp")与response.sendRedirect("/index.jsp");分别实验。

我们知道这里面的两个参数都是相对地址,区别是相对的路径不同:

①:response.sendRedirect("index.jsp");是相对于当前页面(login.jsp)。

当前页面的url地址是:http://localhost:8080/AJAX_option/login.jsp,

因此括号里面的url地址是:http://localhost:8080/AJAX_option/index.jsp

②:response.sendRedirect("/index.jsp");是相对于服务器地址

因此括号里面的url地址是:http://localhost:8080/index.jsp

(之前因为这个错误,一直找不到原因)

 

2.服务器端跳转:RequestDispatcher rd=request.getRequestDispatcher("index.jsp");与RequestDispatcher rd=request.getRequestDispatcher("/index.jsp");分别实验。

这两个参数也是相对地址,区别是相对的路径不同。(注意也客户端跳转的区别

①:RequestDispatcher rd=request.getRequestDispatcher("index.jsp");也是相对于当前页面(login.jsp)

当前页面的url地址是:http://localhost:8080/AJAX_option/login.jsp,

因此括号里面的url地址是:http://localhost:8080/AJAX_option/index.jsp

②:RequestDispatcher rd=request.getRequestDispatcher("/index.jsp");是相对于当前应用程序(AJAX_option)的地址(注意:这与sendRedirect(“/....”)相对的地址内容不同)

因此括号里面的url地址是: http://localhost:8080/AJAX_option/index.jsp

因为当前应用程序的地址下没有其它的目录了,所以有"/"和没有"/"其实并没有差别。但是,如果当前应用程序下还有其它目录的话,比如要分前后台,有admin的目录。此时,要访问admin下的index.jsp就要用"/"的参数,RequestDispatcher rd=request.getRequestDispatcher("/admin/index.jsp");

表示http://localhost:8080/AJAX_option/admin/index.jsp

 

总结:sendRedirect("...")和getRequestDispatcher("...")当里面的参数不带"/"时,都表示相对于当前页面的地址

带有"/"时,两者相对的地址不同。sendRedirect(“/...”)表示相对于服务器地址。getRequestDispatcher("/...")表示相对于应用程序下的地址

 

login.jsp

<%@ page language="java" contentType="text/html; charset=GB18030"
	pageEncoding="GB18030"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>Insert title here</title>
</head>
<body>
	<%
		String login = request.getParameter("login");
		String userName=request.getParameter("username");
		
		if (login!=null && login.equals("action")) {
			if (userName.equals("")) {
				out.print("请任意输入用户名后进入首页");
				return;
			} else {
				session.setAttribute("username",userName);
				//response.sendRedirect("index.jsp");   //客户端跳转   (加了"/"是相对于服务器地址,并不是相对应web应用程序的地址)
				RequestDispatcher rd=request.getRequestDispatcher("/index.jsp");   //(加"/"或不加"/"都可以)
				rd.forward(request, response);     //服务器端跳转
			}
		}else{
		%>
		
	
	<form action="login.jsp" method="post">
		<input type="hidden" name="login" value="action"> 
		用户名:<input type="text" name="username"> 
		<input type="submit" value="login">
	</form>
	<%
	} 
	%>
</body>
</html>


实现Filter接口过滤器:ControlFilter.java

package Filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class ControlFilter implements Filter {


	@Override
	public void destroy() {
		
	}

	@Override
	public void doFilter(ServletRequest req, ServletResponse resp,
			FilterChain chain) throws IOException, ServletException {
		System.out.println("start");
		HttpServletRequest httpReq=(HttpServletRequest)req;
		HttpServletResponse httpResp=(HttpServletResponse)resp;
		HttpSession session=httpReq.getSession();
		if(session.getAttribute("username")==null){
			httpResp.sendRedirect("login.jsp");
		}
			chain.doFilter(req, resp);
			System.out.println(session.getAttribute("username"));
		
	}

	@Override
	public void init(FilterConfig config) throws ServletException {
		
	}

}

index.jsp就简单显示:欢迎登入

web.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app>

     <filter>
        <filter-name>loginFilter</filter-name>
        <filter-class>Filter.ControlFilter</filter-class>
    </filter>


    <filter-mapping>
      <filter-name>loginFilter</filter-name>
      <url-pattern>/index.jsp</url-pattern>
    </filter-mapping> 
<web-app>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值