`
Kayonlife
  • 浏览: 22405 次
  • 性别: Icon_minigender_1
  • 来自: 无锡
社区版块
存档分类
最新评论

Servlets & JSP Series 4 - Being a Servlet

 
阅读更多

 Servlets & JSP Series 4 - Being a Servlet

 

  • Container’s overall role in one servlet’s life: 1.User clicks a link that has a URL to a servlet to generate a request; 2.The Container “sees” that the request is for a servlet, so the container creates two objects: a.HttpServletResponse b.HttpServletRequest; 3.The Container finds the correct servlet based on the URL in the request, creates or allocates a thread for that request, and calls the servlet’s service() method, passing the request and response object as argument; 4.The service() method figures out which servlet method to call based on the HTTP Method(GET,POST,etc.) sent by the client; 5.The servlet uses the response object to write out the response to the client, the response goes back through the container; 6.The service() method completes, so the thread either dies or returens to a container-managed thread pool, the request  and response object references fall out of scope, so these objects are toast, then The client gets the response.
  • The servlet interface says that all servlets have these five method: 1.service(ServletRequest, ServletResponse); 2.init(ServletConfig); 3.destory(); 4.getServletConfig(); 5.getServletInfo().
  • GenericServlet is an abstract class that implements most of the basic servlet methods you’ll need, including those from the Servlet interface, you will probably NEVER extend this class yourself, most of your servlet’s “servlet behavior” comes from this class: 1.service(ServletRequest, ServletResponse); 2.init(ServletConfig); 3.init(); 4.destroy(); 5.getServletConfig(); 6.getServletInfo(); 7.getInitParameter(String); 8.getInitParameterNames(); 9.getServletContext(); 10.log(String); 11.log(String, Throwable).
  • HttpServlet is also a abstract class, it implements the service() method to reflect the HTTPness of the servelt-the service() method doesn’t take just ANY old servelt request and response, but an HTTP-specific request and response: 1.service(HttpSerevletRequest, HttpServletResponse); 2.service(ServletRequest, ServletResponse); 3.doGet(); 4.doPost(); 5.doHead(); 6.doPut; 7.doTrace; 8.doDelete; 9.getModified(HttpServletRequest).
  • Most of servletness is handled by superclass methods, all we do is override the HTTP methods we need. The container runs multiple threads to process multiple requests to a single servlet.
  • Normally there aren’t multiple instances of any servlet classs, each request runs in a separate thread. Servlet is always loaded and initialized before it can sevice its first client request.
  • The constructor of the servlet makes only an object, not a servlet.
  • The HttpServletRequest methods are about HTTP things like cookies, headers, and sessions. HttpServletRequest interface adds the methods that relate to the HTTP protocol. Same thing with the response, the HttpServletResponse adds methods you care about when you are using HTTP things like errors, cookies, and headers.
  • Eight methods in HTTP 1.1: GET; POST; HEAD; TRACE; PUT; DELETE; OPTIONS; CONNECT.
  • The difference between GET and POST: 1.post has a body which put the request parameters (The request parameters is in the URL when the method is GET); 2.the size of the parameter data is also different; 3.parameters in GET is visable; 4 GET is idempotent, POST is not.
  • The HTTP 1.1 spec declares GET, HEAD, and PUT as idempotent, even though you CAN write a non-idemtotent doGET method yourself(but shouldn’t). POST is not considered idempotent by the HTTP 1.1 spec.
  • An HTTP GET is just for getting things, and it’s not supposed to change anything on the server, so a GET is idempotent, it can be executed more than once without any bad side effects; POST is not idempotent-the data submitted in the body of a POST might be destined for a transaction that can’t be reversed.
  • Servlet lifecycle: 1.The Container initializes a servlet by loading the class, invoking the servlet’s no-arg constructor, and calling the servlet’s init() method; 2.The init() method is called only once is a servlet’s life, and always before the servlet can service any client requests; 3.The init() method gives the servlet access to the ServletConfig and ServletContext objects, which the servlet needs to get information about the servlet configuration and the web app; 4.The Container ends a servlet’s running a service() method for a client request; 5.Every request to a servlet runs in a separate thread, there is only one instance of any particular servlet class; 6.Servlet will almost always extend javax.servlet.http.HttpServlet, from which it inherits an implementation of the service() method that takes an HttpServletRequest and an HttpServletResponse; 7.HttpServlet extends javax.servlet.GenericServlet-an abstract class that implements most of the basic servlet methods; 8.GevericServlet implements the Servlet interface; 9.Servlet classes are in one of two packages: javax.servlet or javax.servlet.http; 10.we can override the init() method, and you must override at lease one service method(doGet(), doPost(), etc.).
  • Most of the time, you we use the Response just to send data back to the client, we call two methods on the response: sertContentType() and getWriter(), after that we are simply doing I/O to write HTML to the stream, but we can also use the response to set other headers, send errors, and add cookies.
  • PrintWriter is used for printing data to a character stream, although we can still write character data to an OutputStread, this is the stream that’s designed to handle character data; OutputStream is used for writing anything else.
  • If you have ever used java.io, you can just remember: println() to a PrintWriter; write() to an ServletOutputStream. Make sure you remember that the method names for getting the sream or the writer both drop the first word in the returned type: ServletOutputStream-response.getOutputStream(); PrintWriter-response.getWriter().
  • The difference between a redirect and a request dispatch: redirect makes the client do the work while request dispatch makes something else on the server do the work.
  • When a servlet does a redirect, it’s like asking the client to call someone else instead, in this case the client is the browser, not the user, the browser makes the new call on the suer’s behalf, after the originally-requested servlet says, “Sorry, call this guy instead…”, then the user sees the new URL in the browser.
  • When a servlet does a request dispatch, it’s like asking a co-worker to take over working with a client, the co-worker ends up responding to the client, but the client doesn’t care as long as someone responds, the user never knows someone else took over, because the URL in the browser bar doesn’t change.

 

1
1
分享到:
评论

相关推荐

    Head First Servlets&JSP;-第2版-高清扫描版-带详细书签

    Head First Servlets&JSP;-第2版-高清扫描版-带详细书签 高清扫描版,书签比较详细,和目录一样

    jstl&standard&jsp-api&servlet-api.jar

    4. **Servlet API**:Servlet API是Java Servlet规范的一部分,它定义了服务器端程序(Servlet)与Web服务器之间的接口。Servlets是Java编写的小型程序,用于扩展Web服务器的功能,处理HTTP请求并生成动态内容。...

    Head First Servlets & JSP 学习笔记

    以上只是《Head First Servlets & JSP》一书中的部分核心知识点,实际内容还包括过滤器、监听器、MVC设计模式、JSTL等更广泛的主题,旨在帮助读者全面理解和掌握Servlet和JSP技术。通过深入学习,开发者能够构建高效...

    Head First Servlets & JSP(完好高清中文版)2.pdf

    Head First Servlets & JSP(完好高清中文版)2.pdf 深入浅出 Servlets & JSP(完好高清中文版)2.pdf

    深入浅出Servlets&JSP.

    深入浅出Servlets&JSP,感兴趣的人看一下吧

    Head First Servlets & JSP(完好高清中文版).part3

    Head First Servlets & JSP 完好高清中文版 part3 共6部分 Head First系列的书绝对是初学者的首选!风格生动有趣,讲解由浅入深。 发现网上流传的中文版存在有几处部分页面缺失的问题,花一个下午进行了修复。

    Head First Servlets & JSP, Second Edition

    《Head First Servlets & JSP, Second Edition》是一本针对初学者的优秀教材,它深入浅出地介绍了Servlet和JSP这两个Java Web开发的核心技术。Servlet是Java平台上的服务器端编程接口,而JSP(JavaServer Pages)则...

    Head First Servlets and JSP 中文版 第2版 PDF电子书下载 带书签目录 完整版.zip

    《Head First Servlets and JSP》是学习Java服务器端编程的经典教材,中文版的第二版为读者提供了深入浅出的学习路径,特别适合初学者和有经验的开发者进行自我提升。这本书详细介绍了Servlets和JSP(JavaServer ...

    JAVA - TUTOR SERVLETS & JSP

    Java Servlets 和 JSP(JavaServer Pages)是Java在Web开发中的两个核心技术,它们用于构建动态、交互式的网页应用程序。本教程将深入讲解这两个概念及其优势、安装配置、基本使用方法,以及如何处理请求和响应。 1...

    Head First Servlets & JSP(完好高清中文版)

    《Head First Servlets&JSP》应了最新的学习理论,能将知识直接送到你的大脑里。你会通过不寻常的方式同Servlet和JSP打交道,可以学得更深入、更快,而且更重要的是,你能真正地学以致用。你可以看看为什么那么多...

    Head First Servlets & JSP(完好高清中文版)1.pdf

    Head First Servlets & JSP(完好高清中文版)1.pdf 深入浅出 Servlets & JSP(完好高清中文版)1.pdf

    Head_First_Servlets_&_JSP_习题

    《Head First Servlets & JSP》是一本深受程序员喜爱的学习指南,主要涵盖了Servlets和JSP(JavaServer Pages)这两个核心的Java Web开发技术。Servlets是Java平台上的服务器端编程模型,而JSP则是用于创建动态网页...

    Head First Servlets & JSP(完好高清中文版).part1

    Head First Servlets & JSP 完好高清中文版 part1 共6部分 Head First系列的书绝对是初学者的首选!风格生动有趣,讲解由浅入深。 发现网上流传的中文版存在有几处部分页面缺失的问题,花一个下午进行了修复。

    head first servlets & JSP(3)

    head first servlets & JSP(3)

    head first servlets & JSP(2)

    head first servlets & JSP(2)

    Core Servlets & JSP_cn

    8. **MVC(Model-View-Controller)模式**:Servlet和JSP常用于实现MVC架构,Servlet作为控制器处理业务逻辑,JSP作为视图展示数据,模型通常由JavaBean或其他业务对象组成。 9. **Servlet 3.0及以上版本的新特性**...

    Jave开发指南Servlets&JSP

    《Java开发指南Servlets&JSP》是一本深入讲解Java Web开发的重要书籍,主要涵盖了Servlets和JSP(JavaServer Pages)这两个核心技术。在Java Web开发领域,Servlets和JSP是构建动态网站和Web应用程序的基础,它们为...

    java-servlets-jsp-tutorial-en.rar_This Is It

    Java企业版教程——Servlets与JSP详解 Java企业版(Java Enterprise Edition,简称Java EE)是Oracle公司推出的一种用于构建分布式、多层的企业级应用的框架。它为开发人员提供了丰富的API和服务器平台,以支持高效...

    Head First Servlets & JSP 高清中文版 part2

    Head First Servlets & JSP 高清中文版 part2 共5部分

Global site tag (gtag.js) - Google Analytics