servletpath缺失参数
时间: 2023-12-05 20:06:15 浏览: 18
servletpath缺失参数通常表示在使用Servlet时没有传递必要的参数,导致Servlet无法正常执行。在Servlet中,可以通过request.getParameter()方法获取传递的参数值,如果没有传递参数或者参数名称错误,将会导致该错误的出现。
解决该问题的方法是检查调用Servlet的代码是否正确传递了必要的参数,并且参数名称是否正确。如果确保传递的参数正确无误,还需要检查Servlet代码是否正确处理了参数值。
相关问题
pageContext.request.contextPath和pageContext.request.servletPath区别
pageContext.request.contextPath是在JSP页面中使用的,它返回当前Web应用的上下文路径。例如,如果你的Web应用部署在服务器的根路径下,那么contextPath就是空字符串;如果部署在某个子路径下,那么contextPath就是这个子路径。而pageContext.request.servletPath返回的是当前请求的Servlet路径,不包括上下文路径。
举个例子来说,假设你的Web应用部署在服务器的根路径下,而你访问的URL是:http://localhost:8080/myapp/example.jsp,其中myapp是上下文路径,example.jsp是Servlet路径。那么,pageContext.request.contextPath将返回"/myapp",而pageContext.request.servletPath将返回"/example.jsp"。
总结起来,contextPath代表Web应用的上下文路径,servletPath代表当前请求的Servlet路径。
优化这段代码: public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { if (handler instanceof HandlerMethod) { HandlerMethod handlerMethod = (HandlerMethod) handler; Method method = handlerMethod.getMethod(); RepeatSubmit annotation = method.getAnnotation(RepeatSubmit.class); if (annotation != null) { if (this.isRepeatSubmit(request, annotation)) { AjaxResult ajaxResult = AjaxResult.error(annotation.message()); ServletUtils.renderString(response, JSON.toJSONString(ajaxResult)); return false; } } } boolean flag = false; TokenService tokenService = SpringUtils.getBean(TokenService.class); LoginAppUser loginUser = tokenService.getLoginUser(request); if (loginUser != null && loginUser.getGuest() != null && loginUser.getGuest().getGuestNum() != null) { tokenService.verifyToken(loginUser); String guestNum = loginUser.getGuest().getGuestNum(); HttpSession httpSession = request.getSession(); RedisCache redisUtil = SpringUtils.getBean(RedisCache.class); Guest guest = redisUtil.getCacheMapValue(RedisConstants.GUEST_MAP, guestNum); SysGuest sysGuest = JSON.parseObject(JSON.toJSONString(guest), SysGuest.class); if (sysGuest == null) { flag = false; httpSession.removeAttribute(Constants.SERVER_TYPE_APP); } else { if (httpSession.getAttribute(Constants.SERVER_TYPE_APP) == null) { httpSession.setAttribute(Constants.SERVER_TYPE_APP , sysGuest); } return true; } } //获取网名 也就是/login String servletPath = request.getServletPath(); if(servletPath.contains("/app/card/isCard") || servletPath.contains("/app/bank/signingBankCard") || servletPath.contains("/app/bank/sendMessage") || servletPath.contains("/app/bank/sendSysMessage") || servletPath.contains("/app/bank/login") || servletPath.contains("/app/card/guestIdAndPhone") || servletPath.contains("/app/family/isDevice") || servletPath.contains("/websocket/") || servletPath.contains("/upload/") || servletPath.contains("/app/deviceSocket/toSocket")) { flag = true; } if (!flag) { noLogin(response); } return flag; }
首先,可以将比较长的 if 语句拆分成多个小的 if 语句,这样可以使代码更易读。例如,可以将 `if (loginUser != null && loginUser.getGuest() != null && loginUser.getGuest().getGuestNum() != null)` 拆分成三个 if 语句。
其次,可以使用常量或者枚举类型来表示一些常用的字符串,例如 Constants.SERVER_TYPE_APP 和 RedisConstants.GUEST_MAP。
最后,可以使用 try-with-resources 语句来自动关闭资源,例如 redisUtil。这样可以避免因为忘记关闭资源而导致的内存泄漏问题。
以下是优化后的代码:
```
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
if (handler instanceof HandlerMethod) {
HandlerMethod handlerMethod = (HandlerMethod) handler;
Method method = handlerMethod.getMethod();
RepeatSubmit annotation = method.getAnnotation(RepeatSubmit.class);
if (annotation != null) {
if (this.isRepeatSubmit(request, annotation)) {
AjaxResult ajaxResult = AjaxResult.error(annotation.message());
ServletUtils.renderString(response, JSON.toJSONString(ajaxResult));
return false;
}
}
}
boolean flag = false;
TokenService tokenService = SpringUtils.getBean(TokenService.class);
LoginAppUser loginUser = tokenService.getLoginUser(request);
if (loginUser != null) {
Guest guest = loginUser.getGuest();
if (guest != null) {
String guestNum = guest.getGuestNum();
tokenService.verifyToken(loginUser);
HttpSession httpSession = request.getSession();
RedisCache redisUtil = SpringUtils.getBean(RedisCache.class);
try (redisUtil) {
Guest cacheGuest = redisUtil.getCacheMapValue(RedisConstants.GUEST_MAP, guestNum);
SysGuest sysGuest = JSON.parseObject(JSON.toJSONString(cacheGuest), SysGuest.class);
if (sysGuest != null) {
if (httpSession.getAttribute(Constants.SERVER_TYPE_APP) == null) {
httpSession.setAttribute(Constants.SERVER_TYPE_APP, sysGuest);
}
return true;
}
}
}
}
String servletPath = request.getServletPath();
if (servletPath.contains("/app/card/isCard")
|| servletPath.contains("/app/bank/signingBankCard")
|| servletPath.contains("/app/bank/sendMessage")
|| servletPath.contains("/app/bank/sendSysMessage")
|| servletPath.contains("/app/bank/login")
|| servletPath.contains("/app/card/guestIdAndPhone")
|| servletPath.contains("/app/family/isDevice")
|| servletPath.contains("/websocket/")
|| servletPath.contains("/upload/")
|| servletPath.contains("/app/deviceSocket/toSocket")) {
flag = true;
}
if (!flag) {
noLogin(response);
}
return flag;
}
```
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)