关于springMVC和传统servlet框架从jsp页面向后台请求url问题
来源:互联网 发布:ge 段小缨 知乎 编辑:程序博客网 时间:2024/06/14 00:08
经过实验 发现
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%>" />
- 关于springMVC和传统servlet框架从jsp页面向后台请求url问题
- springmvc Jsp页面向后台传递中文参数乱码问题
- Jsp页面通过href拼接url向后台传中文乱码问题
- jsp页面向后台请求数据的方式
- jsp 向后台servlet传值
- 关于jsp/html页面向后台传输参数时会出现乱码问题
- jsp页面form向后台提交file、text以及后台的获取和中文的处理
- 关于postman向后台传参数问题
- 向后台发送请求
- 前台页面以url形式向后台传递数组
- JSP页面向后台传递数据乱码问题解决方案
- jsp页面实现异步向后台传输数据
- 从页面向后台(struts)post数据,中文乱码问题解决
- javascript利用url向后台传递数据中文乱码问题
- SpringMVC 中向后台传入json数据的问题
- SpringMVC解决前台向后台传输的乱码问题
- 从jsp页面提交数据到servlet页面问题
- Angular向后台发送请求
- 异步加载图片--狸菇凉_
- linux 常用端口
- Python:成员资格运算符(in)
- 基础培训教程
- POJ2253 Frogger
- 关于springMVC和传统servlet框架从jsp页面向后台请求url问题
- HTTP2与HTTP1.1的区别
- C++作业5
- RPG Map Editor 基本刷地图篇
- SqlSession的工具类
- Java wait()和notify()的使用
- 测试滑动界面 NGUi的滑动列表的点击item展开,显示详情
- Sqrt(x)
- 考试篇(5.2) NSE4 题库 18. 认证 ❀ 飞塔 (Fortinet) 网络安全专家