我有一个与在云服务器上运行的tomcat托管有关的问题。
我有一个由(目前(组成的Web应用程序:
•文件夹中的13个HTML文件
•一个名为" CSS"的子文件夹中的单独的CSS文件
•位于名为" JS"的子文件夹中的单独的JavaScript(JS(文件,其中包含许多库函数,包括CORS请求函数
•战争文件中包装的Java Servlet
•web.xml文件
•mySQL数据库
我已经使用NetBean(Willfly作为Web服务器(,MySQL和Firefox浏览器在笔记本电脑上测试了此应用程序,并且一切都很好。
然后,i然后在云服务器上设置了tomcat,重复了数据库,并安装了WAR和Web.xml文件,并使用Tomcat Control面板设置上下文路径。
我已经在此处和其他地点咨询过论坛,并检查了云托管提供商的论坛。我认为我正在竭尽所能启用CORS请求,但是我在某个地方犯了一些微妙的错误,或者我忽略了某些问题,而我目前正在陷入僵局。
JavaScript代码
我的JavaScript CORS函数(在JS库文件中(看起来如下:
function createCORSRequest(method, url) {
var xhr = new XMLHttpRequest();
// xhr.withCredentials = true; // do I even need to do this – and -
xhr.withCredentials = "true"; // which is correct – string or logical?
if ("withCredentials" in xhr) {
// Check if the XMLHttpRequest object has a "withCredentials" property.
// "withCredentials" only exists on XMLHTTPRequest2 objects.
xhr.open(method, url, true);
xhr.setRequestHeader("Content-Type", "application/json"); // is this correct?
} else if (typeof XDomainRequest != "undefined") {
// Otherwise, check if XDomainRequest.
// XDomainRequest only exists in IE, and is IE's way of making CORS requests.
xhr = new XDomainRequest();
xhr.open(method, url);
xhr.setRequestHeader("Content-Type", "application/json"); // is this correct?
} else {
// Otherwise, CORS is not supported by the browser.
xhr = null;
}
return xhr;
}
另外,在同一文件中的其他地方,我定义了该功能各种调用所需的常数:
var PARAM_CMD = "&cmd=";
…
var CMD_GET_MAP = 310;
…
var PARAM_TBL = "&tbl=";
…
var PARAMS_GET_HASHMAP = PARAM_CMD + CMD_GET_MAP + PARAM_TBL;
…
var HASHMAP_STATES = 1130;
…
var urlname = "http://TestSite.com/TestServer/TestServer?autoReconnect=true&useSSL=false";
以下是此代码的典型调用:
function populateUSStatesBox() {
var fullURL = urlname + PARAMS_GET_HASHMAP + HASHMAP_STATES;
var xmlhttp = createCORSRequest("GET", fullURL);
// handle changes to the request state
xmlhttp.onreadystatechange = function () {
if ((xmlhttp.readyState === XMLHttpRequest.DONE) && (xmlhttp.status === HTTP_RESP_OK)) {
var StatesMap = JSON.parse(xmlhttp.responseText);
setupComboBox ("cboUSStates", StatesMap);
}
}
// send the http GET request with the command parameters sent in the header
xmlhttp.send();
}
Java代码
在服务器端,在Servlet的Doget代码开始时,我根据我在各个论坛上看到的帖子设置响应标头值。我实际上只发送[大部分]获取请求和一些帖子(但目前没有任何看台,更新,删除或选项(。我知道" Access-Control-Allow-Origin"的"*"值对生产很危险,但是我刚刚打开了测试。
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
.
.
.
// response.addHeader("Access-Control-Allow-Origin",
request.getHeader("Origin")); // is this correct?
response.addHeader("Access-Control-Allow-Origin", "*");
response.addHeader("Access-Control-Allow-Methods", "GET, POST, PUT, UPDATE, OPTIONS, DELETE");
response.addHeader("Access-Control-Max-Age", "3600");
response.addHeader("Access-Control-Allow-Headers",
"Content-Type, Authorization, Content-Length, X-Requested-With");
response.addHeader("Access-Control-Allow-Credentials", "true");
response.addHeader("Cache-Control", "private, max-age=0, must-revalidate");
response.addHeader("Pragma", "public");
.
.
.
}
.
.
.
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
doGet(request, response);
}
包含servlet的Java文件称为testServer.java;软件包名称是testserver。
配置信息
tomcat目录结构(Linux/CentOS(为:
var
lib
tomcat
webapps
TestServer
TestServer.war
WEB-INF
web.xml
META-INF
context.xml
context.xml包含以下内容:
<?xml version="1.0" encoding="UTF-8"?>
<Context path="/TestServer"/>
Web.xml文件具有以下内容:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<servlet>
<servlet-name>TestServer</servlet-name>
<servlet-class> TestServer. TestServer </servlet-class>
<display-name>Test System</display-name>
<init-param>
<param-name>loginName</param-name>
<param-value>#####</param-value> Note: I’ve obscured the LoginName
</init-param>
<init-param>
<param-name>connectionPWD</param-name>
<param-value>#####</param-value> Note: I’ve obscured the connectionPWD
</init-param>
<init-param>
<param-name>dbURL</param-name>
<!--param-value>localhost/</param-value-->
<!--param-value>jdbc:mysql://localhost:3306/</param-value-->
<param-value>jdbc:mysql://127.0.0.1:3306/</param-value>
</init-param>
<init-param>
<param-name>dbName</param-name>
<param-value>#####</param-value> Note: I’ve obscured the dbName
</init-param>
<init-param>
<param-name>debugging</param-name>
<param-value>on</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>TestServer</servlet-name>
<url-pattern>/ TestServer</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>
结果
当我尝试将其中一个网页加载到本地系统上时,我会在Firefox控制台中收到以下错误(也发生在Chrome和Opera中(:
交叉原始请求阻塞:相同的原始策略在http://testsite.com/testserver/testserver/testserver?autoreconnect = true&amessl = false&cmd = 310&amp;(原因:CORS标题"访问控制">
如果正确执行的代码,我的Web浏览器中的一个选择框之一将显示状态列表。但是,当然,由于CORS错误,我什么都没有人口。任何帮助将不胜感激!
我之前遇到了这个问题,从本质上讲,从javascript代码中,您不允许创建一个请求,其中contentType设置为应用程序/json to back end end servlet。
可以从JavaScript/网站接受的内容类型以供要求:
- 应用程序/x-www-form-urlencoded
- Multipart/form-data
- 文字/普通
因此,简而言之
以下是有关它的更多信息https://developer.mozilla.org/en-us/docs/web/http/headers/access-control-allatro-hallawal-headers#directives
请注意,始终允许某些标头:接受,接受语言,内容语言,内容类型(但仅使用哑剧 其分析值的类型(忽略参数( Application/X-WWW-Form-urlenCoded,Multipart/form-data或 文字/平原(。这些称为简单的标题,您不需要 明确指定它们。