request.getRequestURL()和request.getRequestURI()的区别

本文详细解释了HTTP请求中各种路径的区别,包括getRequestURL(), getRequestURI(), getContextPath()和getServletPath()的含义与应用场景,帮助读者理解服务器如何解析请求路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

request.getRequestURL() 返回全路径
request.getRequestURI() 返回除去host(域名或者ip)部分的路径
request.getContextPath() 返回工程名部分,如果工程映射为/,此处返回则为空
request.getServletPath() 返回除去host和工程名部分的路径
 

例如:

request.getRequestURL() http://localhost:8080/jqueryLearn/resources/request.jsp 
request.getRequestURI() /jqueryLearn/resources/request.jsp
request.getContextPath()/jqueryLearn 
request.getServletPath()/resources/request.jsp 

https://blog.youkuaiyun.com/kaikaixinxiyiwannian/article/details/78732410

package sfw.xmut.guomao.aspect; import sfw.xmut.guomao.entity.OperationLog; import sfw.xmut.guomao.mapper.OperationLogMapper; import sfw.xmut.guomao.utils.ServletUtil; import jakarta.servlet.http.HttpServletRequest; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.springframework.stereotype.Component; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import java.time.LocalDateTime; @Aspect @Component public class OperationLogAspect { private final OperationLogMapper logMapper; public OperationLogAspect(OperationLogMapper logMapper) { this.logMapper = logMapper; } @Around("execution(* sfw.xmut.guomao.controller.*.*(..))") public Object around(ProceedingJoinPoint joinPoint) throws Throwable { ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes(); HttpServletRequest request = attributes.getRequest(); if (request.getRequestURI().endsWith("/operationLog/page")) { return joinPoint.proceed(); } long startTime = System.currentTimeMillis(); Object result = joinPoint.proceed(); long spendTime = System.currentTimeMillis() - startTime; OperationLog log = new OperationLog(); log.setUserName("admin"); // TODO: Get from JWT token log.setOperationType(request.getRequestURI()); log.setOperationMethod(request.getMethod()); log.setRequestUrl(request.getRequestURL().toString()); log.setOperationIp(ServletUtil.getClientIP(request)); log.setOperationStatus("成功"); log.setLastOperationTime(LocalDateTime.now()); logMapper.insert(log); return result; } }log.setUserName("admin"); // TODO: Get from JWT token 这一行代码怎么把他改成不管哪个用户登录这个管理系统日志上面都可以更新,不要像这个admin锁住固定住不管谁登录都是显示他的名称
最新发布
06-19
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值