Você está na página 1de 2

About

Bookstore

RSS

Email

Followus

Becomeafan

JavaExamples
LearnJavaProgrammingbyExamples

Home

1.JavaSEAPI
Servlet

2.JavaEEAPI

3.Frameworks

4.OtherLibraries

5.DesignPatterns

6.IDE/Tools

Searchthissite...
January21,20142:47pm

Youarehere:Home

HowdoIdefineaservletwith@WebServletannotation?

HowdoIdefineaservletwith@WebServlet annotation?
PostedbyWayanSaryada onMay24,2012

AnnotationsisonenewfeatureintroducesintheServlet3.0Specification.Previouslytodeclareservlets,listenersorfilterswemustdoit intheweb.xmlfile.Now,withthenewannotationsfeaturewecanjustannotateservletclassesusingthe@WebServletannotation. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 package org.kodejava.example.servlet; importjavax.servlet.ServletException; importjavax.servlet.annotation.WebInitParam; importjavax.servlet.annotation.WebServlet; importjavax.servlet.http.HttpServlet; importjavax.servlet.http.HttpServletRequest; importjavax.servlet.http.HttpServletResponse; importjava.io.IOException; importjava.io.PrintWriter; @WebServlet( name = "HelloAnnotationServlet", urlPatterns = {"/hello", "/helloanno"}, asyncSupported = false, initParams = { @WebInitParam(name = "name", value = "admin"), @WebInitParam(name = "param1", value = "value1"), @WebInitParam(name = "param2", value = "value2") } ) public class HelloAnnotationServlet extends HttpServlet { @Override protected void doGet(HttpServletRequestrequest, HttpServletResponseresponse) throwsServletException, IOException { response.setContentType("text/html"); PrintWriterout = response.getWriter(); out.write("<html><head><title>WebServletAnnotation</title></head>"); out.write("<body>"); out.write("<h1>ServletHelloAnnotation</h1>"); out.write("<hr/>"); out.write("Welcome" + getServletConfig().getInitParameter("name")); out.write("</body></html>"); out.close(); } }

Find us on Facebook

Learn by Examples
Like

119 people like Learn by Examples.

Facebook social plugin

Afteryouvedeploytheservletyoullbeabletoaccessiteitherusingthe/helloor/helloannourl. Thetablebelowgiveabriefinformationabouttheattributesacceptedbythe@WebServletannotationandtheirpurposes. AttributeName Description Theservletname,thisattributeisoptional. Theservletdescriptionanditisanoptionalattribute. Theservletdisplayname,thisattributeisoptional. Anarrayofurlpatternsuseforaccessingtheservlet,thisattributeisrequiredandshouldatleastregisteroneurl pattern.

name description displayName urlPatterns

asyncSupported Specifieswhethertheservletsupportsasynchronousprocessingornot,thevaluecanbetrueorfalse. initParams


Anarrayof@WebInitParam,thatcanbeusedtopassservletconfigurationparameters.Thisattributeisoptional. loadOnStartup Anintegervaluethatindicatesservletinitializationorder,thisattributeisoptional. smallIcon Asmalliconimagefortheservlet,thisattributeisoptional.

largeIcon

Alargeiconimagefortheservlet,thisattributeisoptional.

RelatedPosts

HowdoIdefineafilterusing@WebFilterannotation? HowdoIdeleteacookieinServlet? HowdoIobtainServletContextofanotherapplication? HowdoIreadcookieinServlet? HowdoIcreateaHelloWorldServlet? HowdoIgetclientIPandhostnameinServlet? HowdoIgetservletrequestheadersinformation?

WorkfromHome
RecentProjects

Generated with www.html-to-pdf.net

Filedin:Servlet

Tags: annotation,servlet

BasichandmobileRecognitionusingandroid, opencv...

Page 1 / 2

AboutWayanSaryada

HowdoIgetclientIPandhostnameinServlet? HowdoIgetservletrequestheadersinformation?

WorkfromHome
RecentProjects Filedin:Servlet Tags: annotation,servlet BasichandmobileRecognitionusingandroid, opencv...

AboutWayanSaryada
Iamaprogrammer,arunner,anopenwaterdiverandcurrentlylivingintheislandofBali,Indonesia. ViewallpostsbyWayanSaryada

$30250

View

Weneedaseriousprogrammerwhocancode Java&JavascriptinLiferayopentobidding<...

$30250

View

LeaveaReply
Name(Required)

TexasHold'emPokerJavaGameSourceCode Repostopentobidding...

$1,5003,000
Findmoreprojects Mail(willnotbepublished)(Required) Website

View

PostaProject
Poweredby

SubmitComment

Pages
About Bookstore

RecentPosts
HowdoIcreateasimplemail clientprograminSwing? Whatarethesystem propertiesusedforsending email? HowdoIsendanHTML email? HowdoIreadlastncharacters fromafile? JavaSEInstallationTutorial

RecentComments
PierreonHowdoIsendemail usingGmailviaTLS? PreethionHowdoImove focusfromJTextAreausing TABkey? AmitonHowdoIgetoperating systemtemporarydirectory/ folder? SrinonHowtomonitorfileor directorychanges? SrinonHowtomonitorfileor directorychanges?

JavaResources
JavaLanguageSpecification JavaSETechnicalDocumentation JavaTutorials JavaEETechnicalDocumentation JavaEE7Tutorials

AffiliateLinks
ShopTechTitlesonoreilly.com 50%offhostingplansfromGoDaddy!

2014JavaExamples.Allrightsreserved.

Generated with www.html-to-pdf.net

Page 2 / 2

Você também pode gostar