关于springMVC和传统servlet框架从jsp页面向后台请求url问题

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zmemorys/article/details/71124069

经过实验  发现

servlet和springmvc向后台发送请求都是根据request.getServletPath()来获取请求的

如果jsp中没有<base href="<%=basePath%>">则向后台的请求会自动解析到项目根目录下 加上以后会自动解析到文件的当前目录下
举个例子


如上图项目(两个jsp内容一样)

jsp内容

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="login.action" method="post">
用户名<input type="text" name="userName">
密码<input type="text" name="password">
<input type="submit" value="submit">
</form>
</body>
</html>
Test是个servlet代码如下

package com;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class Test
 */
public class Test extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Test() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("request.getServletPath()="+request.getServletPath());
		System.out.println("request.getContextPath()="+request.getContextPath());
		
	}

}
1.此时浏览器输入http://localhost:8080/TestServlet/page/NewFile.jsp    
控制台输出

request.getServletPath()=/page/login.action
request.getContextPath()=/TestServlet

2.浏览器输入http://localhost:8080/TestServlet/page/a/NewFile.jsp    
控制台输出
request.getServletPath()=/page/a/login.action
request.getContextPath()=/TestServlet

-----------------------------------------------------------------------------------------------------------------------------------------------------------------

如果在两个jsp中加入basepath
jsp代码如下

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>

<head>
<base href="<%=basePath%>" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="login.action" method="post">
用户名<input type="text" name="userName">
密码<input type="text" name="password">
<input type="submit" value="submit">
</form>
</body>
</html>
1.此时浏览器输入http://localhost:8080/TestServlet/page/NewFile.jsp    

控制台输出
request.getServletPath()=/login.action
request.getContextPath()=/TestServlet


2.浏览器输入http://localhost:8080/TestServlet/page/a/NewFile.jsp    
控制台输出
request.getServletPath()=/login.action
request.getContextPath()=/TestServlet




通过以上实验可以看出request.getServletPath()在jsp中加入basepath的时候会直接获取到请求   如果不加会从项目根目录开始获取请求路径

在springMVC中是一样的道理 springMVC实际也是通过request.getServletPath()获取请求的





springMVC和servlet都是根据这种获取的请求并解析  所以当用springmvc发送请求报404的时候后台报no mapping时可能会是这个原因(请求路径不对应)



ps:这个basePath代码
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<base href="<%=basePath%>" />










猜你喜欢

转载自blog.csdn.net/zmemorys/article/details/71124069