当前位置:   article > 正文

ServletUtils - 封装的工具类_封装一个servletutils工具类

封装一个servletutils工具类
  1. package com.hanyong.utils;
  2. import org.apache.commons.lang3.StringUtils;
  3. import org.springframework.web.context.request.RequestAttributes;
  4. import org.springframework.web.context.request.RequestContextHolder;
  5. import org.springframework.web.context.request.ServletRequestAttributes;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. import javax.servlet.http.HttpSession;
  9. import java.io.IOException;
  10. /**
  11. * 客户端工具类
  12. *
  13. * @Author: hanYong
  14. * @CreateTime: 2019
  15. */
  16. public class ServletUtils {
  17. /**
  18. * 请求格式为json
  19. */
  20. private static final String APPLICATION_JSON = "application/json";
  21. /**
  22. * 请求格式为xml
  23. */
  24. private static final String XML_HTTP_REQUEST = "XMLHttpRequest";
  25. /**
  26. * json后缀
  27. */
  28. private static final String JSON_SUFFIX = ".json";
  29. /**
  30. * xml后缀
  31. */
  32. private static final String XML_SUFFIX = ".xml";
  33. /**
  34. * 索引越界
  35. */
  36. private static final int INDEX_NOT_FOUND = -1;
  37. /**
  38. * 获取String参数
  39. *
  40. * @param name 参数名
  41. * @return String 参数值
  42. */
  43. public static String getParameter(String name) {
  44. return getRequest().getParameter(name);
  45. }
  46. /**
  47. * 获取String参数
  48. *
  49. * @param name 参数名
  50. * @param defaultValue 默认值
  51. * @return String 参数值
  52. */
  53. public static String getParameter(String name, String defaultValue) {
  54. return toStr(getRequest().getParameter(name), defaultValue);
  55. }
  56. /**
  57. * 获取Integer参数
  58. *
  59. * @param name 参数名
  60. * @return Integer 参数值
  61. */
  62. public static Integer getParameterToInt(String name) {
  63. return toInt(getRequest().getParameter(name), null);
  64. }
  65. /**
  66. * 获取Integer参数
  67. *
  68. * @param name 参数名
  69. * @param defaultValue 默认值
  70. * @return Integer 参数值
  71. */
  72. public static Integer getParameterToInt(String name, Integer defaultValue) {
  73. return toInt(getRequest().getParameter(name), defaultValue);
  74. }
  75. /**
  76. * 获取request
  77. *
  78. * @return HttpServletRequest
  79. */
  80. public static HttpServletRequest getRequest() {
  81. return getRequestAttributes().getRequest();
  82. }
  83. /**
  84. * 获取response
  85. *
  86. * @return HttpServletResponse
  87. */
  88. public static HttpServletResponse getResponse() {
  89. return getRequestAttributes().getResponse();
  90. }
  91. /**
  92. * 获取session
  93. *
  94. * @return HttpSession
  95. */
  96. public static HttpSession getSession() {
  97. return getRequest().getSession();
  98. }
  99. /**
  100. * 获取Attributes
  101. *
  102. * @return ServletRequestAttributes
  103. */
  104. public static ServletRequestAttributes getRequestAttributes() {
  105. RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
  106. return (ServletRequestAttributes) attributes;
  107. }
  108. /**
  109. * 将字符串渲染到客户端
  110. *
  111. * @param response 渲染对象
  112. * @param string 待渲染的字符串
  113. * @return null
  114. */
  115. public static String renderString(HttpServletResponse response, String string) {
  116. try {
  117. response.setContentType(APPLICATION_JSON);
  118. response.setCharacterEncoding("utf-8");
  119. response.getWriter().print(string);
  120. } catch (IOException e) {
  121. e.printStackTrace();
  122. }
  123. return null;
  124. }
  125. /**
  126. * 是否是Ajax异步请求
  127. *
  128. * @param request 客户端请求
  129. * @return boolean
  130. */
  131. public static boolean isAjaxRequest(HttpServletRequest request) {
  132. String accept = request.getHeader("accept");
  133. if (accept != null && accept.contains(APPLICATION_JSON)) {
  134. return true;
  135. }
  136. String xRequestedWith = request.getHeader("X-Requested-With");
  137. if (xRequestedWith != null && xRequestedWith.contains(XML_HTTP_REQUEST)) {
  138. return true;
  139. }
  140. String uri = request.getRequestURI();
  141. if (inStringIgnoreCase(uri, JSON_SUFFIX, XML_SUFFIX)) {
  142. return true;
  143. }
  144. String ajax = request.getParameter("__ajax");
  145. return inStringIgnoreCase(ajax, "json", "xml");
  146. }
  147. /**
  148. * 转换为int<br>
  149. * 如果给定的值为空,或者转换失败,返回默认值<br>
  150. * 转换失败不会报错
  151. *
  152. * @param value 被转换的值
  153. * @param defaultValue 转换错误时的默认值
  154. * @return Integer
  155. */
  156. public static Integer toInt(Object value, Integer defaultValue) {
  157. if (value == null) {
  158. return defaultValue;
  159. }
  160. if (value instanceof Integer) {
  161. return (Integer) value;
  162. }
  163. if (value instanceof Number) {
  164. return ((Number) value).intValue();
  165. }
  166. final String valueStr = toStr(value, null);
  167. if (StringUtils.isEmpty(valueStr)) {
  168. return defaultValue;
  169. }
  170. try {
  171. return Integer.parseInt(valueStr.trim());
  172. } catch (Exception e) {
  173. return defaultValue;
  174. }
  175. }
  176. /**
  177. * 转换为字符串<br>
  178. * 如果给定的值为null,或者转换失败,返回默认值<br>
  179. * 转换失败不会报错
  180. *
  181. * @param value 被转换的值
  182. * @param defaultValue 转换错误时的默认值
  183. * @return String
  184. */
  185. public static String toStr(Object value, String defaultValue) {
  186. if (null == value) {
  187. return defaultValue;
  188. }
  189. if (value instanceof String) {
  190. return (String) value;
  191. }
  192. return value.toString();
  193. }
  194. /**
  195. * 是否包含字符串(不区分大小写)
  196. *
  197. * @param str 验证字符串
  198. * @param searchStrings 字符串组
  199. * @return boolean
  200. */
  201. public static boolean inStringIgnoreCase(String str, String... searchStrings) {
  202. if (str != null && searchStrings != null) {
  203. for (String s : searchStrings) {
  204. if (str.equalsIgnoreCase(StringUtils.trim(s))) {
  205. return true;
  206. }
  207. }
  208. }
  209. return false;
  210. }
  211. }

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/187012
推荐阅读
相关标签
  

闽ICP备14008679号