willim

常用链接

统计

好友列表

链接

最新评论

SERVLET学习笔记(一)

打算这个星期把SERVLET和JSP复习下,我认为基础对一个人很重要,所以会经常回过头来看看.
1.HTTP协议
每个做web应用都应该熟悉HTTP协议吧,如果不熟悉大家找些资料去学学.
GET: 参数直接作为一个query string放到URL后面传递的.可以是text
POST:参数是作为封装一个请求信息体里的.可以是text和binary
servlet里有多个方法用来对应处理http协议的请求方法
doGet() : link,和直接在浏览器中输入URL
doPost():  通过form表单显示声明method="post",否则默认为get
doDelete(): 针对要删除服务器的某些资源的请求
doTrace(): 调试服务器连接的http方式
doOptions() : The OPTIONS request determines which HTTP methods the server supports and returns an appropriate header. For example, if a servlet overrides doGet, this method returns the following header:
Allow: GET, HEAD, TRACE, OPTIONS
doPut() : 针对要向服务器放入新的文件的请求
doHead(): 针对只要response的Header信息的请求

当client来一个请求时,doService(HttpServletRequest request, HttpServletResponse response) throws ServletException ,IOException接受这个,并选择相应的请求选择相应的方法进行处理

2.HttpServletResquest介绍
常用的方法:String getParameter(String name)
Enumeration getParameterNames()
String[] getParameterValues(String name)
String   getHeader(String name)
int getIntHeader(String name)
long getDateHeader(String name)
Enumeration getHeaderNames()
String[] getHeaderValus(String name)
Cookies[] getCookies()

3.HttpServletResponse介绍
void setContentType(String type)
setHeader(String name, String value)
setIntHeader(String name, int value)
setDateHeader(String name, long value)
addHeader(String name, String name)
addIntHeader(String name, int value)
addDateHeader(String name ,long value)
boolean containsHeader(String name)

PrintWriter getWriter()  //返回字符流
OutputStream getOutputStream()  //返回字节流

void sendRedirect(String URL) //重定向到某个页面

void sendError(int sc) //发生错误向客户端发送状态码
void sendError(int sc, String message)

void addCookie(Cookie cookie) //增加一个cookie

Cookie(String name, String value) //  cookie的构造方法

4.SERVLET的生命周期
servlet loading  ---> servlet instantiation ---> call init() ---> call doService handle request ---> call destroy()

public void init(ServletConfig config) throws ServletException
如果override这个方法必须先调用super.init(config)

public void init() throws ServletException

public void service() throws ServletException, IOException

public void destroy() throws ServletException

posted on 2006-12-25 22:30 willlim 阅读(419) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航: