• 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 
    暗夜之中,才见繁星;危机之下,暗藏转机;事在人为,为者常成。
  • 相关阅读:
    Leetcode: Word Ladder II
    Leetcode: Triangle
    Leetcode: Best Time to Buy and Sell Stock II
    Leetcode: Best Time to Buy and Sell Stock
    Leetcode: Pascal's Triangle II
    Leetcode: Pascal's Triangle
    Leetcode: Path Sum II
    Leetcode: Convert Sorted Array to Binary Search Tree
    Leetcode: Merge Sorted Array
    Leetcode: Word Search
  • 原文地址:https://www.cnblogs.com/zenghansen/p/4059106.html
Copyright © 2020-2023  润新知