xmlns:
xsi=”http:
//www.w3.org/2001/XMLSchema-instance”
xsi:
schemaLocation=”web-app_2_4.xsd”
version=”2.4”>
WelcometoTomcat
HelloWorldServlet
HelloWorldServlet
com.iss.test.HelloWorldServet
HelloWorldServlet
/hello
运行
从客户端访问服务器上的应用,比如通过浏览器访问,服务器给客户端返回“Hello,world!
”的信息。
第三节Servlet相关类和接口
HttpServlet类
一个抽象类,可以从该类派生出一个子类来实现一个HttpServlet,接受来自Web站点的请求,并将处理后的响应结果发回Web站点。
在HttpServlet的子类中,必须至少重载下表中所列的其中一种方法。
HttpServlet子类通常重载的方法
方法名
用途
doGet
如果Servlet支持HTTPGET请求,用于HTTPGET请求
doPost
如果Servlet支持HTTPPOST请求,用于HTTPPOST请求
doPut
如果Servlet支持HTTPPUT请求,用于HTTPPUT请求
doDelete
如果Servlet支持HTTPDELETE请求,用于HTTPDELETE请求
init和destroy
如果需要管理Servlet生命周期内所持有资源,可以重载这两个方法
通常,不重载service方法,对于上表中的每一种HTTP请求,service方法通过分派它们到相应的Handle线程(doXXX方法)来处理这些标准的HTTP请求。
通常也不重载doOptions和doTrace方法,service方法通过分派它们到doTrace和doOptions来支持HTTP1.1TRACE和OPTIONS。
Servlet通常运行在多线程的服务器中,因此,所编写的Servlet代码必须能够处理并行请求和对数据资源的同步访问。
doGet
protectedvoiddoGet(HttpServletRequestreq,HttpServletResponseresp)
throwsServletException,IOException
用于获取服务器信息,并将其作为响应返回给客户端。
当经由web浏览器,或者通过HTML、Jsp直接访问Servlet的URL时,一般使用Get调用。
doPost
protectedvoiddoPost(HttpServletRequestreq,HttpServletResponseresp)
throwsServletException,IOException
用于客户端把数据传送到服务器端。
使用它的好处是可以隐藏发送给服务器的任何数据。
适合于发送大量的数据。
doPut
protectedvoiddoPut(HttpServletRequestreq,HttpServletResponseresp)
throwsServletException,IOException
Put的调用和Post相似,它允许客户端把真正的文件存放在服务器上,而不仅仅是传送数据。
doDelete
protectedvoiddoDelete(HttpServletRequestreq,HttpServletResponseresp)
throwsServletException,IOException
它的调用和Post相似,它允许客户端删除服务器端的文件或者web页面。
doTrace
protectedvoiddoTrace(HttpServletRequestreq,HttpServletResponseresp)
throwsServletException,IOException
由容器调用以使此Servlet能够处理Trace请求。
这个方法主要用于测试,它是不可以覆盖的方法。
doHead
protectedvoiddoHead(HttpServletRequestreq,HttpServletResponseresp)
throwsServletException,IOException
它用于处理客户端的Head调用,并且返回一个response。
当客户端只需要知道响应的Header时,它就发出一个Header请求。
doOptions
protectedvoiddoOptions(HttpServletRequestreq,HttpServletResponseresp)
throwsServletException,IOException
它用于处理客户端的Options调用,通过这个调用,客户端可以获得此Servlet支持的方法。
HttpServletRequest接口
该接口扩展了javax.servlet.servletRequest接口,用于提供更多的功能给HttpServlet。
Servlet引擎实现该接口,创建HttpServletRequest对象,将来自客户浏览器的信息传递给HttpServlet的service方法。
getDateHeader
publiclonggetDateHeader(Stringname)
得到一个特定请求的header的、描述Date对象的长整形值。
Header是大小写敏感的。
如果请求中没有该name的header,则方法返回-1,如果方法不能转换日期,则抛IllegalArgumentException异常。
getHeader
publicStringgetHeader(Stringname)
得到有关请求header的值,为一个字符串,如果指定名字的header没有随请求一起发送,该方法返回null,name是大小写敏感的。
getCookies
publicCookie[]getCookies()
得到一个Cookie对象数组,含有所有发送请求的浏览器的Cookie,如果浏览器没有发送任何Cookie,则返回null。
getHeaderNames
publicEnumerationgetHeaderNames()
得到请求中的所有header名。
getRequestURI
publicStringgetRequestURI()
得到HTTP请求的第一行中从协议名到查询字符串之间的URL部分。
getSession
publicHttpSessiongetSession(Booleancreate)
publicHttpSessiongetSession()
得到与请求相联系的当前HttpSession,如果需要,则为请求创建一个新的任务。
HttpServletResponse接口
该接口扩展了OutputStream类,因此,可以使用OutputStream类中声明的方法,例如,getOutputStream()方法。
addCookie
publicvoidaddCookie(Cookiecookie)
增加指定的cookie到响应。
setHeader
publicvoidsetHeader(Stringname,Stringvalue)
使用给定的名字和值设置响应头。
ServletConfig接口
该接口定义了一个Servlet配置对象,Servlet引擎可以用来传递信息给Servlet以便初始化Servlet。
配置信息含有初始化参数,这些参数以“名字/值”的形式成对出现,描述关于服务器的Servlet信息。
getServletContext
publicServletContextgetServletContext()
得到服务器传递给Servlet的一个ServletContext对象。
getInitParameter
publicStringgetInitParameter(Stringname)
得到一个含有参数name指定的初始化参数的值,如果参数不存在则为null。
getInitParameterNames
publicEnumerationgetInitParameterNames()
得到Servlet的初始化参数的名字,返回值是一个Enumeration对象。
ServletContext接口
该接口定义了一套Servlet与Servlet引擎通信的方法,例如:
得到MIME类型的文件、确定运行在服务器上的其他Servlet或写Servlet日志文件。
Servlet引擎通过返回一个ServetContext对象来与Servlet交流,Servlet使用ServletConfig接口的getServletContext方法得到ServletContext对象。
getContext
publicServletContextgetContext(Stringuripath)
得到服务器上特定URL的一个ServletContext对象,该方法允许Servlet访问特定URL上的资源,并得到来自该URL的RequestDispatcher对象。
getMimeType
publicStringgetMimeType(Stringfile)
得到指定文件的MIME类型,如果不知道MIMI类型则为null。
MIMI类型由Servlet引擎的配置决定,MIME类通常为“text/html”和“image/gif”。
getRealPath
publicStringgetRealPath(Stringpath)
得到参数path中对应于虚拟路径的真实路径。
虚拟路径含有一个Servlet名,后跟Servlet文件名。
getServerInfo
publicStringgetServerInfo()
得到正在允许的Servlet引擎的名字和版本。
getAttribute
publicObjectgetAttribute(Stringname)
得到Servlet引擎的参数name所指定的属性。
getAttributeNames
publicEnumerationgetAttributeNames()
得到一个含有属性名的Enumeration对象,这些属性在Servlet上下文中有效。
setAttribute
publicvoidsetAttribute(Stringname,Objectobject)
设置Servlet上下文中属性的名字,如果指定的名字name已使用,该方法将覆盖老的名字,并将名字捆绑到新的属性中。
removeAttribute
publicvoidremoveAttribute(Stringname)
从Servlet上下文中移走给定名字name的属性名。
ServletInputStream类
该类是一个抽象类,它提供了从客户机请求读取二进制数据的一个输入流,对于一些协议,例如HTTPPOST和PUT,可以使用ServletInputStream对象读取和发送数据。
readLine
publicintreadLine(byte[]b,intoff,intlen)throwsIOException
读输入流,一次一行。
如果读到输入流的尾部,则返回-1。
ServletOutputStream类
该类是一个抽象类,它提供了一个输入流,用于向客户机发送数据。
print
publicvoidprint(Strings)throwsIOException
publicvoidprint(booleanb)throwsIOException
publicvoidprint(charc)throwsIOException
publicvoidprint(inti)throwsIOException
publicvoidprint(longl)throwsIOException
publicvoidpr