step-by-step构造Jersey Web Application

本文介绍如何使用Jersey和Eclipse构建基本的Web应用程序。通过具体步骤演示了从创建项目到部署运行的全过程,包括配置Apache Tomcat、添加Jersey库、编写控制器类等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这是一个Hello World例子如何使用Jersey构造一个Web Application
使用工具
1. Apache Tomcat 7.x
1. Eclipse Java EE IDE for Web Developers; Version: Neon.2 Release
2. Jersey库
(比较方便的构造Jersey应用是使用maven生产框架,但是由于国内访问maven库的限制,不得采用直接下载Jersey库的办法)


Step 1: 下载和安装Apache Tomcat 7.x(不细说了)
Step 3: 下载Jersey库(https://jersey.java.net/download.html),包括

- Jersey JAX-RS 2.0 RI bundle

- Jersey MVC

把所有用到的库拷贝到WEB-INF/lib目录下面:



Step 2: 使用Eclipse创建一个"Dynamic Web Project"
2.1 创建Eclipse工程

        

        选择缺省module,并且选中缺省web.xml


        2.2 创建Application类

package com.jersey.sample.application;
 
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.mvc.jsp.JspMvcFeature;
 
public class MyApplication extends ResourceConfig{
    public MyApplication(){
        packages("com.jersey.sample.controller");
        register(JspMvcFeature.class);
        property(JspMvcFeature.TEMPLATE_BASE_PATH, "/WEB-INF/jsp");
    }
}

        2.3 创建Controller类

package com.jersey.sample.controller;
 
import java.util.HashMap;
 
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
 
import org.glassfish.jersey.server.mvc.Viewable;
 
@Path("/hello")
public class HelloController {
    @GET
    public Viewable sayHello(@QueryParam("name") @DefaultValue("World") String name) {
        HashMap<String, Object> model = new HashMap<String, Object>();
        model.put("name", name);             
        return new Viewable("/hello", model);
    }


    @GET  
    @Path("/{param}")    
    @Produces("text/plain;charset=UTF-8")  
    public String sayHelloToUTF8(@PathParam("param") String username) {  
        return "Hello " + username;
    }
}


        2.4 创建WEB-INF/jsp/hello.jsp

<%@page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
  <head>   
  </head>
  <body>
    Hello, <c:out value="${it.name}" /> 
  </body>
</html>


        2.5 编辑web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>JerseySample</display-name>
  
  <!-- Jersey -->
  <filter>
    <filter-name>MyFilter</filter-name>
    <filter-class>org.glassfish.jersey.servlet.ServletContainer</filter-class>
    <init-param>
      <param-name>javax.ws.rs.Application</param-name>
      <param-value>com.jersey.sample.application.MyApplication</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>MyFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>    
    
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>


3. 运行

- http://localhost:8080/JerseySample/hello

Hello, World 

- http://localhost:8080/JerseySample/hello?name=aaaa

Hello, aaaa

- http://localhost:8080/JerseySample/hello/bbb

Hello bbb


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值