先看一下整体的目录结构:
//ConnServer.java
package oa.com.cn;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 短链接,判断复印机和服务器是否在线。mfp请求
*
* @author admin
*
*/
public class ConnServer extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 2987369059483122344L;
/**
* Constructor of the object.
*/
public ConnServer() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* 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 {
this.doPost(request, response);
}
/**
* 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 {
System.out.println("ok servlet 接受请求");
String str = "";
// response.addHeader("Access-Control-Allow-Origin", "*");
response.getWriter().println(str);
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException
* if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
//HttpServer.java
package oa.com.cn;
import java.io.BufferedReader;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* mfp请求 获取跳转界面
*
* @author admin
*
*/
public class HttpServer extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 137052495915525281L;
/**
* Constructor of the object.
*/
public HttpServer() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* 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 {
this.doPost(request, response);
}
/**
* 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 {
// if (true) {
StringBuffer sb = new StringBuffer();
String line;
String string2 = request.getParameter("status");
System.out.println("string2:" + string2);
BufferedReader reader = request.getReader();
while ((line = reader.readLine()) != null)
sb.append(line);
String jbstring = sb.toString();
System.out.println("jbstring:" + jbstring);
String status = "0";
String[] arr = jbstring.split("&");
for (String str : arr) {
String[] arr2 = str.split("=");
if (arr2[0] == "status" || "status".equals(arr2[0])) {
status = arr2[1];
}
}
if ("0".equals(status)) {
System.out.println("conn servlet 接受请求");
String str = "{\"result\":\"OK\",\"url\":\"http://192.168.xx.xx:8090/xeroxmfp/index5.html\"}";
// String str = "{\"result\":\"OK\",\"url\":\"http://baidu.com\"}";
System.out.println(str);
response.setHeader("Connection", "keep-alive");
response.getWriter().println(str);
} else {
response.getWriter().println("{\"result\":\"OK\"}");
}
// }
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException
* if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
//LicenseServer.java
package oa.com.cn;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* mfp请求获取授权码
*
* @author admin
*
*/
public class LicenseServer extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = -2877799836073284638L;
/**
* Constructor of the object.
*/
public LicenseServer() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* 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 {
this.doPost(request, response);
}
/**
* 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 {
System.out.println("license servlet 接受请求");
String str = "{\"result\":\"OK\",\"license\":\"8DD5-716D-15C3-xxxx\"}";
response.getWriter().println(str);
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException
* if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
//CorsFilter.java
package oa.com.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletResponse;
public class CorsFilter implements Filter {
public void init(FilterConfig filterConfig) throws ServletException {
}
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
throws IOException, ServletException {
HttpServletResponse httpResponse = (HttpServletResponse) servletResponse;
httpResponse.addHeader("Access-Control-Allow-Origin", "*");
httpResponse.addHeader("Access-Control-Allow-Headers", "*");
httpResponse.addHeader("Access-Control-Request-Headers", "*");
filterChain.doFilter(servletRequest, servletResponse);
}
public void destroy() {
}
}
<!--web.xml-->
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name></display-name>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>HttpServer</servlet-name>
<servlet-class>oa.com.cn.HttpServer</servlet-class>
</servlet>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>LicenseServer</servlet-name>
<servlet-class>oa.com.cn.LicenseServer</servlet-class>
</servlet>
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>ConnServer</servlet-name>
<servlet-class>oa.com.cn.ConnServer</servlet-class>
</servlet>
<filter>
<filter-name>CorsFilter</filter-name>
<filter-class>oa.com.filter.CorsFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CorsFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet-mapping>
<servlet-name>HttpServer</servlet-name>
<url-pattern>/conn</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>LicenseServer</servlet-name>
<url-pattern>/license</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>ConnServer</servlet-name>
<url-pattern>/ok</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
//index.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<h1>
This is my JSP page. <br>
</h1>
</body>
<script type="text/javascript">
window.onload = function() {
window.document.onclick = function() {
window.parent.postMessage("", "*");
};
};
</script>
</html>
可以打成war包运行。