HttpServletResponse工具类和HttpServletRequest工具类,前台参数接收方式和后台返回(JSON)数据格式
2021-04-23 19:27
标签:提示信息 coding org null names values eal super map
* A key can appear more than once in the query string with different
* values. However, the key appears only once in the hashtable, with its
* value being an array of strings containing the multiple values sent by
* the query string.
*
*
* The keys and values in the hashtable are stored in their decoded form, so
* any + characters are converted to spaces, and characters sent in
* hexadecimal notation (like %xx) are converted to ASCII characters.
*
* @param s
* a string containing the query to be parsed
*
* @return a HttpServletResponse工具类和HttpServletRequest工具类,前台参数接收方式和后台返回(JSON)数据格式 标签:提示信息 coding org null names values eal super map 原文地址:https://www.cnblogs.com/pxblog/p/12238509.htmlRequestUtils.java 操作类
package cn.utils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.util.UrlPathHelper;
import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;
/**
* HttpServletRequest 工具类
*/
public class RequestUtils {
private static final Logger log = LoggerFactory
.getLogger(RequestUtils.class);
/**
* HTTP POST请求
*/
public static final String POST = "POST";
/**
* UTF-8编码
*/
public static final String UTF8 = "UTF-8";
/**
* 获取QueryString的参数,并使用URLDecoder以UTF-8格式转码。如果请求是以post方法提交的,
* 那么将通过HttpServletRequest#getParameter获取。
*
* @param request
* web请求
* @param name
* 参数名称
* @return
*/
public static String getQueryParam(HttpServletRequest request, String name) {
if (StringUtils.isBlank(name)) {
return null;
}
if (request.getMethod().equalsIgnoreCase(POST)) {
return request.getParameter(name);
}
String s = request.getQueryString();
if (StringUtils.isBlank(s)) {
return null;
}
try {
s = URLDecoder.decode(s, UTF8);
} catch (UnsupportedEncodingException e) {
log.error("encoding " + UTF8 + " not support?", e);
}
String[] values = parseQueryString(s).get(name);
if (values != null && values.length > 0) {
return values[values.length - 1];
} else {
return null;
}
}
@SuppressWarnings("unchecked")
public static Map
HashTable
object with key-value pairs. The query string
* should be in the form of a string packaged by the GET or POST method,
* that is, it should have key-value pairs in the form key=value,
* with each pair separated from the next by a & character.
*
* HashTable
object built from the parsed key-value
* pairs
*
* @exception IllegalArgumentException
* if the query string is invalid
*
*/
public static MapResponseUtils.java 操作类
package cn.utils;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* HttpServletResponse 工具类
*/
public final class ResponseUtils {
public static final Logger log = LoggerFactory
.getLogger(ResponseUtils.class);
/**
* 发送文本。使用UTF-8编码。
*
* @param response
* HttpServletResponse
* @param text
* 发送的字符串
*/
public static void renderText(HttpServletResponse response, String text) {
render(response, "text/plain;charset=UTF-8", text);
}
/**
* 发送json。使用UTF-8编码。
*
* @param response
* HttpServletResponse
* @param text
* 发送的字符串
*/
public static void renderJson(HttpServletResponse response, String text) {
render(response, "application/json;charset=UTF-8", text);
}
/**
* 发送xml。使用UTF-8编码。
*
* @param response
* HttpServletResponse
* @param text
* 发送的字符串
*/
public static void renderXml(HttpServletResponse response, String text) {
render(response, "text/xml;charset=UTF-8", text);
}
/**
* 发送内容。使用UTF-8编码。
*
* @param response
* @param contentType
* @param text
*/
public static void render(HttpServletResponse response, String contentType,
String text) {
response.setContentType(contentType);
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
try {
response.getWriter().write(text);
} catch (IOException e) {
log.error(e.getMessage(), e);
}
}
public static void renderApiJson(HttpServletResponse response,
HttpServletRequest request,ApiResponse apiResult) {
//js跨域请求
String callback = request.getParameter("callback");
if(StringUtils.isNotBlank(callback)){
ResponseUtils.renderJson(response,callback+"(" + apiResult.toString() + ")" );
}else{
ResponseUtils.renderJson(response, apiResult.toString());
}
}
}
ApiResponse.java
package cn.utils;
public class ApiResponse {
public ApiResponse(String body, String message, String status) {
super();
this.body = body;
this.message = message;
this.status = status;
}
/**
* 返回信息主体
*/
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
/**
* API调用提示信息
*/
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
/**
* API接口调用状态
*/
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
@Override
public String toString() {
return "{\"body\":" + body + ", \"message\":" + message + ",\"status\":" + status + "}";
}
private String body;
private String message;
private String status;
}
上一篇:php解决跨域的代码
下一篇:CSS的精灵技术
文章标题:HttpServletResponse工具类和HttpServletRequest工具类,前台参数接收方式和后台返回(JSON)数据格式
文章链接:http://soscw.com/index.php/essay/78622.html