java跨域访问问题记录
Posted 守护者
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java跨域访问问题记录相关的知识,希望对你有一定的参考价值。
跨域访问记录
package com.alphasta.zsbs.servlet;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import com.alphasta.zsbs.util.Resources;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.yx.frame.jdbc.Sql;
import com.yx.frame.jdbc.SqlEntry;
import com.yx.frame.jdbc.Table;
/**
* 更新用户信息
* @author D
*
*/
public class UpdateUserInfoServlet extends HttpServlet {
private Logger log = Logger.getLogger(UpdateUserInfoServlet.class);
/**
*
*/
private static final long serialVersionUID = 3869311915733581508L;
/**
* Constructor of the object.
*/
public UpdateUserInfoServlet() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html; charset=utf-8");
PrintWriter out = response.getWriter();
out.println("不支持GET请求");
out.flush();
out.close();
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to
* post.
*
* @param request
* the request send by the client to the server
* @param response
* the response send by the server to the client
* @throws ServletException
* if an error occurred
* @throws IOException
* if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setHeader("Access-Control-Allow-Credentials","true");//中间测试过程一直有问题,加了这个就好了
response.setHeader("Access-Control-Allow-Headers", "origin, x-csrftoken, content-type, accept, x-auth-code, X-App-Id, X-Token");
response.setHeader("Access-Control-Allow-Method", "POST, GET, OPTIONS");
response.setHeader("Access-Control-Allow-Origin", "http://XXXX");//注意加自己地址
response.setContentType("application/json; charset=utf-8");
String userJson = "";
//逻辑代码部分
log.info("update post --"+userJson);
OutputStream re_out = response.getOutputStream();
re_out.write(userJson.getBytes("UTF-8"));
re_out.flush();
log.info("update post finish --"+userJson+"--"+userJson.getBytes("UTF-8"));
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException
* if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
以上是关于java跨域访问问题记录的主要内容,如果未能解决你的问题,请参考以下文章
2019-03-26 SpringBoot项目部署遇到跨域问题,记录一下解决历程