城市猎人

在一网情深的日子里,谁能说得清是苦是甜,只知道确定了就义无反顾
posts - 1, comments - 7, trackbacks - 0, articles - 89

Servlet总结

Posted on 2009-07-18 11:57 sailor 阅读(425) 评论(0)  编辑  收藏 所属分类: java
 

The Container initializes a servlet by loading the class, invoking the servlet’s no-arg constructor, and calling the servlet’s init() method.

1 The init() method (which the developer can override) is called only once in a servlet’s life, and always before the servlet can service any client requests.

2 The init() method gives the servlet access to the Serv-letConf g and ServletContext objects, which the servlet needs to get information about the servlet conf guration and the web app.

3 The Container ends a servlet’s life by calling its destroy() method.

4 Most of a servlet’s life is spent 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 Your 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 GenericServlet implements the Servlet interface.

9 Servlet classes (except those related to JSPs) are in one of two packages: javax.servlet or javax.servlet.http.

10 You can override the init() method, and you must override at least one service method (doGet(), doPost(), etc.)

The HttpServlet’s doGet() and doPost() methods take an HttpServletRequest and an HttpServletResponse.

1 The service() method determines whether doGet() or doPost() runs based on the HTTP Method (GET, POST, etc.) of the HTTP request.

2 POST requests have a body; GET requests do not, although GET requests can have request parameters appended to the request URL (sometimes called “the query string”).

3 GET requests are inherently (according to the HTTP spec) idempotent. They should be able to run multiple times without causing any side effects on the server. GET requests shouldn’t change anything on the server. But you could write a bad, non-idempotent doGet() method.

4 POST is inherently not idempotent, so it’s up to you to design and code your app in such a way that if the client sends a request twice by mistake, you can handle it.

5 If an HTML form does not explicitly say “method=POST”, the request is sent as a GET, not a POST. If you do not have a doGet() in your servlet, the request will fail.

6 You can get parameters from the request with the getParameter(“paramname”) method. The return value is always a String.

7 If you have multiple parameter values for a given param-eter name, use the getParameterValues(“paramname”) method that returns a String array.

8  You can get other things from the request object including headers, cookies, a session, the query string, and an input stream

Post与Get区别:
1. Get用于从服务端获取数据,Post用于向服务器端提交数据;
2. Get提交后,参数保留在浏览器地址栏,Post不会;
3. Get提交时,URL会带参数,Post不会。因为URL是保存到http协议头部。而Post参数保存到http协议Body中
4. Get容易产生重复提交,Post不会。

 

You use the Response to send data back to the client.

1 The most common methods you’ll call on the response object (HttpServletResponse) are setContentType() and getWriter().

2 Be careful—many developers assume the method is getPrintWriter(), but it’s getWriter().

3 The getWriter() method lets you do character I/O to write HTML (or something else) to the stream.

4 You can also use the response to set headers, send errors, and add cookies.

5 In the real world, you’ll probably use a JSP to send most HTML responses, but you may still use a response stream to send binary data

(like a JAR f le, perhaps) to the client.

6 The method you call on your response for getting a binary stream is getOutputStream().

7 The setContentType() method tells the browser how to handle the data coming in with the response. Typical content types are “text/html”, application/pdf”, and “image/jpeg”.

8 You don’t have to memorize content types (also known as MIME types).

9 You can set response headers using addHeader() or setHeader(). The difference depends on whether the header is already part of the response. If it is, setHeader() will replace the value, but addHeader will add an additional value to the existing response. If the header is not already part of the response, then setHeader() and addHeader() behave in exactly the same way.

10 If you don’t want to respond to a request, you can redirect the request to a different URL. The browser takes care of sending the new request to the URL you provide.

11 To redirect a request, call sendRedirect(aStringURL) on the response.

12 You cannot call sendRedirect() after the response is committed! In other words, if you’ve already written something to the stream, it’s too late to do a redirect.

13 A request redirect is different from a request dispatch. A request dispatch (covered more in another chapter) happens on the server, while a redirect happens on the client. A request dispatch hands the request to another component on the server, usually within the same web app. A request redirect simply tells the browser to go a different URL


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


网站导航: