当前位置:编程学习 > JAVA >>

Jetty入门

这是一个JavaProject

 

 

首先是待纳入Jetty管理的LoginServlet.java


[java]
package com.jadyer.servlet; 
 
import java.io.IOException; 
import java.io.PrintWriter; 
 
import javax.servlet.ServletException; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
 
public class LoginServlet extends HttpServlet { 
    private static final long serialVersionUID = 2827360195836107171L; 
 
    @Override 
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
        System.out.println("收到请求参数[" + request.getParameter("username") + "]"); 
        response.setContentType("text/html; charset=UTF-8"); 
        PrintWriter out = response.getWriter(); 
        out.print("<h2>这是我的嵌入式Jetty测试结果</h2>"); 
        out.flush(); 
        out.close(); 
    } 

package com.jadyer.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoginServlet extends HttpServlet {
 private static final long serialVersionUID = 2827360195836107171L;

 @Override
 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  System.out.println("收到请求参数[" + request.getParameter("username") + "]");
  response.setContentType("text/html; charset=UTF-8");
  PrintWriter out = response.getWriter();
  out.print("<h2>这是我的嵌入式Jetty测试结果</h2>");
  out.flush();
  out.close();
 }
}
下面这个也是待纳入Jetty管理的UserServlet.java


[java]
package com.jadyer.servlet; 
 
import java.io.IOException; 
import java.io.PrintWriter; 
 
import javax.servlet.ServletException; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
 
public class UserServlet extends HttpServlet { 
    private static final long serialVersionUID = 2827360195836107171L; 
 
    @Override 
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
        System.out.println("收到请求参数[" + request.getParameter("username") + "]"); 
        response.setContentType("text/html; charset=UTF-8"); 
        PrintWriter out = response.getWriter(); 
        out.print("<h2>这是我的嵌入式Jetty测试结果-->用户访问Servlet成功</h2>"); 
        out.flush(); 
        out.close(); 
    } 

package com.jadyer.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class UserServlet extends HttpServlet {
 private static final long serialVersionUID = 2827360195836107171L;

 @Override
 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  System.out.println("收到请求参数[" + request.getParameter("username") + "]");
  response.setContentType("text/html; charset=UTF-8");
  PrintWriter out = response.getWriter();
  out.print("<h2>这是我的嵌入式Jetty测试结果-->用户访问Servlet成功</h2>");
  out.flush();
  out.close();
 }
}
最后是自己编写的Jetty启动类MyServer.java(内含Jetty嵌入式的简单用法)


[java] 
package com.jadyer.jetty; 
 
import java.util.HashMap; 
import java.util.Map; 
 
import javax.servlet.Servlet; 
 
import org.eclipse.jetty.server.Server; 
import org.eclipse.jetty.servlet.ServletContextHandler; 
import org.eclipse.jetty.webapp.WebAppContext; 
 
import com.jadyer.servlet.LoginServlet; 
import com.jadyer.servlet.UserServlet; 
 
/**
 * @see --------------------------------------------------------------------------------------------------------------
 * @see Jetty简介
 * @see Jetty是一个嵌入式的Web服务器
 * @see Jetty官网:http://www.eclipse.org/jetty/
 * @see Jetty下载:http://download.eclipse.org/jetty/
 * @see --------------------------------------------------------------------------------------------------------------
 * @see Jetty与Tomcat比较(取自网络)
 * @see Jetty的架构要比Tomcat简单一些,它是基于Handler来实现的,它可以通过Handler来进行扩展
 * @see Tomcat的架构是基于容器设计的,扩展Tomcat时需要了解Tomcat的整体设计结果,故不易扩展
 * @see --------------------------------------------------------------------------------------------------------------
 * @see Jetty使用
 * @see 0)版本选择:由于Jetty9需要JDK7的支持,所以我们这里使用jetty-distribution-8.1.10.v20130312.zip
 * @see 1)修改端口:修改\\JETTY_HOME\\etc\\jetty.xml第40行default="8080"即可
 * @see 2)非嵌入式的项目发布(有两种方式)
 * @see   非嵌入式的启动方式为命令行下执行该命令-->D:\Develop\jetty-distribution-8.1.10.v20130312>java -jar start.jar
 * @see   第一种:项目打成war包放到\\JETTY_HOME\\webapps\\下即可,访问地址为http://127.0.0.1:8080/warName(war名字大小写严格相同)
 * @see   第二种:类似于Tomcat的<Context path="" docBase="">的方式,即在\\JETTY_HOME\\contexts\\目录下新增一个名字随意的xml文件
 * @see         文件格式与该目录下的javadoc.xml相同,其中主要修改以下两处
 * @see         <Set name="contextPath">/testBbb</Set>
 * @see         <Set name="resourceBase">F:/Tool/Code/JavaSE/loginManager/WebRoot</Set>
 * @see 3)嵌入式的项目发布
 * @see   也就是把Jetty提供的jar加入到项目中(可以是Java项目或Web项目),然后编写通过一个main()启动Jetty,所用到的jar如下
 * @see   JETTY_HOME中的lib目录,和

补充:软件开发 , Java ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,