zoukankan      html  css  js  c++  java
  • HelloWorld之jetty运行

    jetty是一个轻便的嵌入式servlet容器。其启动运行非常简单。eclipse下运行jetty容器有如下几步, 

    一、建一个普通的java工程 
    二、把jetty需要的包导入工程分别是jetty-6.1.15.rc3.jar,jetty-util-6.1.15.rc3.jar,servlet-api-2.5-20081211.jar,core-3.1.1.jar,jsp-2.1-glassfish-9.1.1.B51.25.p1.jar,jsp-2.1-jetty-6.1.15.rc3.jar,jsp-api-2.1-glassfish-9.1.1.B51.25.p1.jar(本人下载的是jetty-6.1.15.rc3.zip,后边是个jar是为了使工程支持jsp文件而加入的) 
    三、建立web目录,一个名字叫做web的文件夹里面有WEB-INF文件夹和web.xml文件 
    web.xml文件内容如下: 
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    </web-app>

    四、编写一个java类代码如下 
    Java代码 
     
    package test;
     
    import org.mortbay.jetty.Connector;
    import org.mortbay.jetty.Server;
    import org.mortbay.jetty.nio.SelectChannelConnector;
    import org.mortbay.jetty.webapp.WebAppContext;
     
    public class Bootstart {
    public static void main(String[] args) throws Exception {
    Server service = new Server();
    Connector connector = new SelectChannelConnector();
    //设置端口
    connector.setPort(8080);
    //设置host地址
    connector.setHost("127.0.0.1");
    service.setConnectors(new Connector[] { connector });
     
    //设置根路径
    WebAppContext context = new WebAppContext("web", "/web");
    service.addHandler(context);
    service.setStopAtShutdown(true);
    service.setSendServerVersion(true);
     
    //启动服务
    service.start();
    service.join();
    }
    }

    五、测试的jsp文件index.jsp 
    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Insert title here</title>
    </head>
    <body>
    <%
    request.setAttribute("name","sun");
    System.out.println("My name is:" + request.getAttribute("name"));
    %>
    </body>
    </html>

    六、运行main方法并测试连接http://127.0.0.1:8080/web 
    暗夜之中,才见繁星;危机之下,暗藏转机;事在人为,为者常成。
  • 相关阅读:
    eclipse的优化 gc.log
    一次使用Eclipse Memory Analyzer分析Tomcat内存溢出
    JVM系列三:JVM参数设置、分析
    热加载
    彻底理解JAVA动态代理
    Linux下查看Web服务器当前的并发连接数和TCP连接状态
    个人博客 V0.0.3 版本 ...
    HTML5本地存储——IndexedDB(一:基本使用)
    如何在Blog中加入Google Analytics
    webpack中实现按需加载
  • 原文地址:https://www.cnblogs.com/zenghansen/p/4059106.html
Copyright © 2011-2022 走看看