搭建jetty http框架 <一>

来源:互联网 发布:java调用com组件 编辑:程序博客网 时间:2024/05/16 18:11

文档参照

jetty搭建http服务器


http://blog.csdn.net/super_ninja/article/details/39692901

基于Spring MVC的简单HelloWorld实例


http://blog.csdn.net/techbirds_bao/article/details/8444486


1 首先架构jetty,将相关包导入后

public class ServerMain {    private static final String CONFIG_LOCATION = "com.easysoft.cn.config";    private static final String MAPPING_URL = "/*";    private static final String DEFAULT_PROFILE = "dev";    public static void main(String[] args) throws Exception {        Server server = new Server(8090);        ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);        context.setContextPath("/MyServer");   //这里是请求的上下文,比如http://localhost:8090/MyServer        server.setHandler(context);        WebApplicationContext applicationContext=getContext();//        context.addServlet(new ServletHolder(new HelloWorld()), "/helloWorld");   //添加servlet,第一是具体的servlet,后面是请求的别名,在http请求中的路径//        context.addServlet(new ServletHolder(new HelloWorld("chan")), "/HellworldWithParams");//        context.addServlet(new ServletHolder(new DispatcherServlet( applicationContext)), MAPPING_URL);//        context.addEventListener(new ContextLoaderListener(applicationContext));//        context.setResourceBase(new ClassPathResource("webapp").getURI().toString());        server.start();        server.join();    }

2 新建servlet

public class HelloWorld extends HttpServlet {    /**     * serialVersionUID     */    private static final long serialVersionUID = 2271797150647771294L;    private String msg = "hello world~~~~~~";    public HelloWorld() {    }    public HelloWorld(String msg){        this.msg = msg;    }    @Override    protected void doGet(HttpServletRequest req, HttpServletResponse resp)            throws ServletException, IOException {        doPost(req, resp);    }    @Override    protected void doPost(HttpServletRequest req, HttpServletResponse resp)            throws ServletException, IOException {        String userName = req.getParameter("userName");        String password = req.getParameter("password");        resp.setCharacterEncoding("UTF-8");        resp.setContentType("text/html");        resp.setStatus(HttpServletResponse.SC_OK);        PrintWriter pWriter = resp.getWriter();        pWriter.println("<h1>" + msg + "</h1>");        pWriter.println("测试中文信息:" + req.getSession(true).getId());        pWriter.println("<h3>用户信息:" + userName + "</h3>");        pWriter.println("<h3>用户密码:" + password + "</h3>");    }}


完了之后,在浏览器中输入http://localhost:8090/MyServer/helloWorld?userName=zhangsan&password=123,得到的结果如下:  


  1. hello world~~~~~~  
  2.   
  3. 测试中文信息:haybcp922h6x1v8bh9xeazx96  
  4. 用户信息:zhangsan  
  5.   
  6. 用户密码:123 
测试jetty安装完毕 可以连接通jetty和servlet


原创粉丝点击