野兽

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
}

}

posted @ 2017-09-11 15:51  android-susu  阅读(306)  评论(0编辑  收藏  举报