标签:
在项目中,往往要手动设置一个Jetty服务器进行各种参数处理,比如之前在游戏公司,用的就是游戏服内部搭建Jetty服务器,然后方便外部访问.
主要用到这几块.
本身就是Web应用了,还用Jetty干嘛,当然,我这只是做个示例,以后做app或者平台级应用都可以用Jetty搭建外部访问Servlet.
首先,我们设置WebServer,并且设置在监听器里,使得WEB服务器启动的时候可以加载Jetty服务器,
这里是WebServer代码:
设置参数,启动,初始化,这些内容.
- package com.dc.servlet;
-
- import java.io.IOException;
- import java.io.PrintWriter;
- import java.util.Date;
-
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
-
- import com.google.gson.Gson;
- import com.road.entity.info.UserInfo;
- import com.road.pitaya.component.LanguageComponent;
- import com.road.pitaya.database.DataOption;
-
- public abstract class AbstractServlet extends HttpServlet
- {
- private static final long serialVersionUID = 2421477169746085074L;
- @SuppressWarnings("unused")
- private Logger LOGGER = LoggerFactory.getLogger(AbstractServlet.class);
-
- protected final Gson gson = new Gson();
-
- protected String requestIP = null;
- @Override
- public void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException
- {
- doPost(request, response);
- }
-
- @Override
- protected void doPost(HttpServletRequest request,
- HttpServletResponse response) throws ServletException, IOException
- {
- String result = null;
- try
- {
- requestIP = com.dc.util.ServletUtil.getRequestIP(request);
- result = execute(request, response);
- }
- catch (Exception e)
- {
- e.printStackTrace();
- }
- response.setCharacterEncoding("UTF-8");
-
- response.setContentType("text/html");
- response.setStatus(HttpServletResponse.SC_OK);
-
- PrintWriter out = response.getWriter();
- out.print(result);
- out.flush();
- out.close();
- }
-
- public abstract String execute(HttpServletRequest request,
- HttpServletResponse response) throws ServletException, IOException;
-
-
- protected UserInfo newUserInfo(String site, String iuid, String pssd, Date createTime)
- {
- UserInfo userInfo = new UserInfo();
- userInfo.setOp(DataOption.INSERT);
- userInfo.setSite(site);
- userInfo.setIuid(iuid);
- userInfo.setPssd(pssd);
- userInfo.setCreateTime(createTime);
- userInfo.setServerId(0);
- return userInfo;
- }
-
- protected String formatTime(long millis)
- {
- int m = (int)(millis/1000);
- int ss = m%60;
- int mm = m/60%60;
- int hh = m/3600%24;
- int dd = m/86400;
- String time = "";
- if(dd > 0)
- time = LanguageComponent.getResource("Login.ServerStatusdd", dd, hh, mm, ss);
- else if(hh > 0)
- time = LanguageComponent.getResource("Login.ServerStatushh", hh, mm, ss);
- else if(mm > 0)
- time = LanguageComponent.getResource("Login.ServerStatusmm", mm, ss);
- else if(ss > 0)
- time = LanguageComponent.getResource("Login.ServerStatusss", ss);
- return time;
- }
-
-
- }
中控接口继承用
然后就是我们测试的Servlet
- package com.dc.servlet;
-
- import java.io.IOException;
-
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
-
- import com.road.pitaya.web.WebHandleAnnotation;
-
- @WebHandleAnnotation(cmdName = "/doSomething", description = "测试接口.")
- public class DoSomethingServlet extends AbstractServlet{
-
-
- private static final long serialVersionUID = -7574673387670968292L;
-
- @Override
- public String execute(HttpServletRequest request,
- HttpServletResponse response) throws ServletException, IOException {
- System.out.println(111111111);
- return null;
- }
-
- }
对于webResources可以看到有个配置文件:
名字叫:crossdomain.xml
从名字可以看出来,这个是跨域策略,方便跨域访问.
- <?xml version="1.0"?>
- <cross-domain-policy>
- <allow-access-from domain="*" />
- </cross-domain-policy>
内容就这些,只是作为实现.
准备就做完了,我们启动tomcat,
Jetty正常启动.
我们之前设置的端口是7039
我们在doSomething设置一个断点,然后我们访问localhost:7039/doSomething 可以看到,断点进去,
控制台输出,于是,我们一个简单的Jetty服务器就搭建完成了,以后可以根据这个Jetty服务器进行其他各项操作.
如何用Web服务器搭建自己的Jetty服务器
标签:
原文地址:http://www.cnblogs.com/miercler/p/5396386.html