java web中servlet详解_Servlet3.0新特性WebServlet(Annotation Servlet)详解

属性:

Name

Type

Required

Description

value

or

urlPatterns

String[]

Required

Specify one or more URL patterns of the servlet. Either of attribute can be used, but not both.

name

String

Optional

Name of the servlet

displayName

String

Optional

Display name of the servlet

description

String

Optional

Description of the servlet

asyncSupported

boolean

Optional

Specify whether the servlet supports asynchronous operation mode. Default is false.

initParams

WebInitParam[]

Optional

Specify one or more initialization parameters of the servlet. Each parameter is specified by @WebInitParam annotation type.

loadOnStartup

int

Optional

Specify load-on-startup order of the servlet.

smallIcon

String

Optional

Specify name of the small icon of the servlet.

largeIcon

String

Optional

Specify name of the large icon of the servlet.

OneServlet:

package com.what21.servlet.webservlet;

import java.io.IOException;

import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

@WebServlet("/one")

public class OneServlet extends HttpServlet {

private static final long serialVersionUID = 6313903606540274020L;

@Override

protected void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.getWriter().print("

OneServlet doGet()

");

}

}

访问地址:  http://127.0.0.1:8080/web/one

TwoServlet:

package com.what21.servlet.webservlet;

import java.io.IOException;

import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

@WebServlet("*.two")

public class TwoServlet extends HttpServlet {

private static final long serialVersionUID = 1584128088126928923L;

@Override

protected void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.getWriter().print("

TwoServlet doGet()

");

}

}

访问地址:  http://127.0.0.1:8080/web/1.two , http://127.0.0.1:8080/web/aaaa.two

ThreeServlet:

package com.what21.servlet.webservlet;

import java.io.IOException;

import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

@WebServlet(urlPatterns = {"/three1", "/three2"})

public class ThreeServlet extends HttpServlet {

private static final long serialVersionUID = -3862534325602779508L;

@Override

protected void service(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.getWriter().print("

ThreeServlet service()

");

}

}

访问地址:  http://127.0.0.1:8080/web/three1 , http://127.0.0.1:8080/web/three2

FourServlet:

package com.what21.servlet.webservlet;

import java.io.IOException;

import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

@WebServlet(

name = "MyFourServlet",

description = "This is a annotated servlet",

urlPatterns = "/four"

)

public class FourServlet extends HttpServlet {

private static final long serialVersionUID = -7132030251230555778L;

@Override

protected void service(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.getWriter().print("

FourServlet service()

");

}

}

访问地址:  http://127.0.0.1:8080/web/four

FiveServlet:

package com.what21.servlet.webservlet;

import java.io.IOException;

import javax.servlet.ServletConfig;

import javax.servlet.ServletException;

import javax.servlet.annotation.WebInitParam;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

@WebServlet(

urlPatterns = "/five",

initParams =

{

@WebInitParam(name = "name", value = "username"),

@WebInitParam(name = "value", value = "password")

},

loadOnStartup = 1,

asyncSupported = true

)

public class FiveServlet extends HttpServlet {

private static final long serialVersionUID = -2574757602020384180L;

@Override

public void init(ServletConfig config) {

String name = config.getInitParameter("name");

String value = config.getInitParameter("value");

System.out.println("name = " + name);

System.out.println("value = " + value);

System.out.println("init().....");

}

@Override

protected void service(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

response.getWriter().print("

FiveServlet service()

");

}

}

访问地址:  http://127.0.0.1:8080/web/five

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值