zoukankan      html  css  js  c++  java
  • 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
    }

    }

    守护
  • 相关阅读:
    MS SQL 数据库状态为SUSPECT(可疑)的处理方法
    根據名稱查找文件,并移動
    deploy war to weblogic via script(shell)
    maven打包时包含所有依赖的jar
    随笔
    如何在Oracle SQL Developer對Procedure除錯
    git常用命令和記錄
    英語筆記
    IDEA設置
    mysql常用命令和记录
  • 原文地址:https://www.cnblogs.com/suhe/p/7505352.html
Copyright © 2011-2022 走看看