ServletUtils
package com.ruoyi.common.utils; import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import com.ruoyi.common.core.text.Convert; /**
* 客户端工具类
*
* @author ruoyi
*/
public class ServletUtils
{
/**
* 获取String参数
*/
public static String getParameter(String name)
{
return getRequest().getParameter(name);
} /**
* 获取String参数
*/
public static String getParameter(String name, String defaultValue)
{
return Convert.toStr(getRequest().getParameter(name), defaultValue);
} /**
* 获取Integer参数
*/
public static Integer getParameterToInt(String name)
{
return Convert.toInt(getRequest().getParameter(name));
} /**
* 获取Integer参数
*/
public static Integer getParameterToInt(String name, Integer defaultValue)
{
return Convert.toInt(getRequest().getParameter(name), defaultValue);
} /**
* 获取request
*/
public static HttpServletRequest getRequest()
{
return getRequestAttributes().getRequest();
} /**
* 获取response
*/
public static HttpServletResponse getResponse()
{
return getRequestAttributes().getResponse();
} /**
* 获取session
*/
public static HttpSession getSession()
{
return getRequest().getSession();
} public static ServletRequestAttributes getRequestAttributes()
{
RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
return (ServletRequestAttributes) attributes;
} /**
* 将字符串渲染到客户端
*
* @param response 渲染对象
* @param string 待渲染的字符串
* @return null
*/
public static String renderString(HttpServletResponse response, String string)
{
try
{
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
response.getWriter().print(string);
}
catch (IOException e)
{
e.printStackTrace();
}
return null;
} /**
* 是否是Ajax异步请求
*
* @param request
*/
public static boolean isAjaxRequest(HttpServletRequest request)
{
String accept = request.getHeader("accept");
if (accept != null && accept.indexOf("application/json") != -1)
{
return true;
} String xRequestedWith = request.getHeader("X-Requested-With");
if (xRequestedWith != null && xRequestedWith.indexOf("XMLHttpRequest") != -1)
{
return true;
} String uri = request.getRequestURI();
if (StringUtils.inStringIgnoreCase(uri, ".json", ".xml"))
{
return true;
} String ajax = request.getParameter("__ajax");
if (StringUtils.inStringIgnoreCase(ajax, "json", "xml"))
{
return true;
}
return false;
}
}
ServletUtils的更多相关文章
- ServletUtils取值(限foton)
1.String query = ServletUtils.getStringValue(request, "query", null); 2.boolean reload = S ...
- 浏览器下载/导出文件 及jQuery表单提交
1 比如以下按钮, 用于导出文件,如EXCEL文件. <li> <button class="whiteBg btn2" onclick="doExp( ...
- Java Code Examples for javax.servlet.http.Part
http://www.programcreek.com/java-api-examples/index.php?api=javax.servlet.http.Part The following ar ...
- 动态jsp页面转PDF输出到页面
最近工作中遇到不少问题.总结一下.这段代码主要功能是将一个生成JSP页面转发成PDF输出到页面 需要利用ITEXT String html = ServletUtils.forward(request ...
- SSH综合练习-仓库管理系统-第二天
SSH综合练习-仓库管理系统-第二天 今天的主要内容: 货物入库 页面信息自动补全回显功能:(学习目标:练习Ajax交互) 根据货物简记码来自动查询回显已有货物(Ajax回显) 根据货物名来自动查询补 ...
- SSH综合练习-第1天
SSH综合练习-仓库管理系统-第一天 综合练习的整体目的: 整合应用 Struts2 .Hibernate.Spring .Mysql . jQuery Ajax.java基础知识 熟悉企业SSH 基 ...
- 一个简单的Servlet工具
以前老师在项目中用过一个Sevlet的工具,就是在请求Servlet的时候带一个参数,该参数决定要执行Servlet中的方法, public class ServletUtils extends Ht ...
- 搭建servlet+jsp环境
c3p0: <?xml version="1.0" encoding="UTF-8"?><c3p0-config> <named- ...
- [转]Jboss基础
查看JBoss控制台JMX-Console //可以通过这个页面进行对 JBOSS 的各服务的配置和管理. http://localhost:8080/jmx-console/ 查看发布的WebSer ...
随机推荐
- PHP日期函数
在正式学习日期函数前大家得了解几个概念: 1.时区 2.世界时 3.unix时间戳 时区 这个概念,之前大家听说过很多.我们来啰嗦两句,我们现实生活中使用的实区,在电脑里面也是一样有规定的.1884年 ...
- docker-compose 安装 mongodb
1. 修改 docker-compose.yml version: "2.1" services: php7.1: build: ./php image: php7.1-ext p ...
- mybatis环境搭建(eclipse,idea)
基于java配置SSM,eclipse 新建maven,web项目 .... 项目结构: jar包 pom.xml spring和DispatcherServlet上下文 public class D ...
- JVM源码分析之自定义类加载器如何拉长YGC
概述 本文重点讲述毕玄大师在其公众号上发的一个GC问题一个jstack/jmap等不能用的case,对于毕大师那篇文章,题目上没有提到GC的那个问题,不过进入到文章里可以看到,既然文章提到了jstac ...
- 腾讯电话面试总结(IEG后台开发)
1 Java面向对象:设计window画板的类框架.假设现在只有 直线.矩形.椭圆,怎么设计 2 Linux shell命令 定时怎么做 3 平时有问题经常访问那些网站 4 假设你现在是web网站 ...
- CodeForces - 748F Santa Clauses and a Soccer Championship
题意:有k对队伍,每对队伍之间将举行两次比赛,两支队伍各主办一次.住宿的地方要求在两支队伍家乡的最短路的结点上或者在两支队伍的家乡.问在选择住宿处最少的情况下,怎么组成这k对队伍? 分析: 1.因为n ...
- C基础 带你手写 redis ae 事件驱动模型
引言 - 整体认识 redis ae 事件驱动模型, 网上聊得很多. 但当你仔细看完一篇又一篇之后, 可能你看的很舒服, 但对于 作者为什么要这么写, 出发点, 好处, 缺点 ... 可能还是好模糊, ...
- 【十日冲刺计划】第一日 星遇Sprint1计划会议成果
——小组成员 赵剑峰 张傲 周龙海 Sprint 计划会议1:定出 Sprint 目标和既定产品Backlog. 会议进程(1小时) • 首先对sprint目标进行总体介绍,概括星遇的backlog, ...
- 池ThreadPoolExecutor使用简介
public static void main(String[] args) { //guava 创建线程池 //https://blog.csdn.net/chinabestchina/articl ...
- ipa提取png图片,windows下显示黑色
原因:ipa提取的png图片经过xcode特殊格式处理,在windows下查看会显示黑色 方法1:通过“ ipin.py文件”转化 过程: 1.安装python,需2.7版本,本文使用2.7.5 ...