package com.lihaozhe.spring.mvc.util.servlet;
import com.lihaozhe.spring.mvc.servlet.TestServlet;
import com.lihaozhe.spring.mvc.util.json.BaseJacksonUtil;
import com.lihaozhe.spring.mvc.util.response.ResponseResult;
import com.lihaozhe.spring.mvc.util.response.ResultCode;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* @author 李昊哲
* @version 1.0.0 2022/7/21 上午9:06
*/
public class BaseServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 处理中文乱码
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=utf-8");
// ajax跨域
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Access-Control-Allow-Methods", "POST, GET, PUT, PATCH, OPTIONS, DELETE");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "Origin, Accept, x-auth-token,Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers, Authorization, Code-Token, Access-Token");
// response.setHeader("Access-Control-Allow-Origin", "http://192.168.18.65:5500");
response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
response.setHeader("Access-Control-Allow-Credentials", "true");
response.setHeader("XDomainRequestAllowed", "1");
System.out.println("sessionid = " + request.getSession().getId());
String requestMethod = request.getMethod();
if ("OPTIONS".equalsIgnoreCase(requestMethod)) {
// 避免非正常请求发送到后端程序
return;
}
String methodName = request.getParameter("methodName");
if (StringUtils.isEmpty(methodName)) {
ResponseResult result = ResponseResult.getInstance();
result.setCode(ResultCode.PARAM_ERROR.getCode());
result.setMsg(ResultCode.PARAM_ERROR.getMsg());
printJson(response, result);
return;
}
Class<? extends BaseServlet> aClass = this.getClass();
String contentType = request.getContentType();
try {
if (!StringUtils.isEmpty(contentType) && (contentType.contains("application/json") || contentType.contains("text/plain"))) {
// 客户端发送json格式字符串
BufferedReader reader = request.getReader();
String text = IOUtils.toString(reader);
Method method = aClass.getDeclaredMethod(methodName, HttpServletRequest.class, HttpServletResponse.class, String.class);
method.setAccessible(true);
method.invoke(this, request, response, text);
} else {
// 端发送表单数据
Method method = aClass.getDeclaredMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
method.setAccessible(true);
method.invoke(this, request, response);
}
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
throw new RuntimeException(e);
}
}
/**
* 向页面发送字符串
*
* @param response HttpServletResponse
* @param text 发送字符串
* @throws IOException
*/
public void printText(HttpServletResponse response, String text) throws IOException {
response.setContentType("application/json;charset=utf-8");
PrintWriter writer = response.getWriter();
writer.println(text);
writer.flush();
writer.close();
}
/**
* 向页面发送JSON格式字符串
*
* @param response HttpServletResponse
* @param object
* @throws IOException
*/
public void printJson(HttpServletResponse response, Object object) throws IOException {
response.setContentType("application/json;charset=utf-8");
PrintWriter writer = response.getWriter();
writer.println(BaseJacksonUtil.objectToJson(object));
writer.flush();
writer.close();
}
/**
* jsonp 向页面发送字符串
*
* @param request
* @param response
* @param text
* @throws IOException
*/
public void printText4Jsonp(HttpServletRequest request, HttpServletResponse response, String text) throws IOException {
String method = request.getParameter("callback");
String json = method + "(" + text + ")";
response.setContentType("application/json;charset=utf-8");
PrintWriter writer = response.getWriter();
writer.println(json);
writer.flush();
writer.close();
}
/**
* jsonp 向页面发送json格式字符串
*
* @param request
* @param response
* @param object
* @throws IOException
*/
public void printJson4Jsonp(HttpServletRequest request, HttpServletResponse response, Object object) throws IOException {
String method = request.getParameter("callback");
String json = method + "(" + BaseJacksonUtil.objectToJson(object) + ")";
response.setContentType("application/json;charset=utf-8");
PrintWriter writer = response.getWriter();
writer.println(json);
writer.flush();
writer.close();
}
public void forward(HttpServletRequest request, HttpServletResponse response, String path) throws ServletException, IOException {
request.getRequestDispatcher(path).forward(request, response);
return;
}
public void forward(HttpServletRequest request, HttpServletResponse response, String path, String prefix, String subfix) throws ServletException, IOException {
request.getRequestDispatcher(prefix + path + subfix).forward(request, response);
return;
}
private final static String prefix = "/WEB-INF/jsp/";
private final static String subfix = ".jsp/";
public void forwardWithPrefixAndSubfix(HttpServletRequest request, HttpServletResponse response, String path) throws ServletException, IOException {
request.getRequestDispatcher(prefix + path + subfix).forward(request, response);
return;
}
}
评论0