Introduction to JavaServer-Side Technologies-Servlets and JSP.ppt
《Introduction to JavaServer-Side Technologies-Servlets and JSP.ppt》由会员分享,可在线阅读,更多相关《Introduction to JavaServer-Side Technologies-Servlets and JSP.ppt(87页珍藏版)》请在麦多课文档分享上搜索。
1、1,Introduction to Java Server-Side Technologies: Servlets and JSP,Sun tutorial to servlets,Sun JSP Tutorial,2,Introduction to Servlets,3,What is a Servlet?,Servlets are Java programs that can be run dynamically from a Web Server Servlets are a server-side technology A Servlet is an intermediating la
2、yer between an HTTP request of a client and the data stored on the Web server,4,A Java Servlet,Web browser,Web server,Servlet,5,An Example,In the following example, the local server calls the Servlet TimeServlet with an argument supplied by the user,This example, as well as all the examples in this
3、lecture can be found at http:/inferno:5000/ (accessible only from CS!),6,Reload / Refresh Servlet Result,Trying to refresh the content created by a servlet will lead to fetching a new content from the server This is not the case with static resources Response headers of a static (as opposed to a ser
4、vlet generated) resource contain Etag, Last-Modified While trying to refresh a resource Cache-Contol: max-age=0 is sent and that means the server/proxies will try to revalidate the resource Only in the static case the resource could be revalidated against some values the client holds So in the stati
5、c case the client sends the Etag value attached to the If-None-Match header, and the Last-Modified value is sent in If-Modified-Since,Clear the cache, open and then reload /dbi/Time.html Open and reload /dbi/init Compare the headers sent and received.,7,What can Servlets do?,Read data sent by the us
6、er (e.g., form data) Look up other information about the request in the HTTP request (e.g. authentication data, cookies, etc.) Generate the result (may do this by talking to a database, file system, etc.) Format the result as a document (e.g., convert it into HTML format) Set the appropriate HTTP re
7、sponse parameters (e.g. cookies, content-type, etc.) Send the resulting document to the user,8,Supporting Servlets,To run Servlets, the Web server must support them Apache Tomcat Also functions as a module for other Apache servers Sun Java System Web Server and Java System Application Server IBMs We
8、bSphere Application Server BEAs Weblogic Application Server Macromedias Jrun an engine that can be added to Microsofts IIS, Apaches Web servers and more. Oracle Application Server ,9,Creating a Simple Servlet,Read more about the Servlet Interface,10,The Servlet Interface,Java provides the interface
9、Servlet Specific Servlets implement this interface Whenever the Web server is asked to invoke a specific Servlet, it activates the method service() of an instance of this Servlet,11,HTTP Request Methods,POST - application data sent in the request body GET - application data sent in the URL HEAD - cl
10、ient sees only header of response PUT - place documents directly on server DELETE - opposite of PUT TRACE - debugging aid OPTIONS - list communication options,12,Servlet Hierarchy,YourOwnServlet,HttpServlet,Generic Servlet,Servlet,service(ServletRequest, ServletResponse),doGet(HttpServletRequest , H
11、ttpServletResponse) doPost(HttpServletRequest HttpServletResponse) doPut doTrace ,Called by the servlet container to allow the servlet to respond to any request method,Called by the servlet container to allow the servlet to respond to a specific request method,A generic, protocol-independent class,
12、implementing Servlet,13,Class HttpServlet,Class HttpServlet handles requests and responses of HTTP protocol The service() method of HttpServlet checks the request method and calls the appropriate HttpServlet method: doGet, doPost, doPut, doDelete, doTrace, doOptions or doHead This class is abstract,
13、Read more about the HttpServlet Class,That is, a class that can be sub-classed but not instantiated. This class methods however are not abstract,14,Creating a Servlet,Extend the class HTTPServlet Implement doGet or doPost (or both; also maybe others) Both methods get: HttpServletRequest: methods for
14、 getting form (query) data, HTTP request headers, etc. HttpServletResponse: methods for setting HTTP status codes, HTTP response headers, and get an output stream used for sending data to the client Many times, we implement doPost by calling doGet, or vice-versa,You could also run: CheckRequestServl
15、et /dbi/empty ,Check the result of an empty implementation http:/localhost/dbi/empty,15,import java.io.*; import javax.servlet.*; import javax.servlet.http.*;public class TextHelloWorld extends HttpServlet public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IO
16、Exception PrintWriter out = res.getWriter();out.println(“Hello World“);public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException doGet(req, res); ,HelloWorld.java,16,Returning HTML,By default, no content type is given with a response In order to generat
17、e HTML: Tell the browser you are sending HTML, by setting the Content-Type header (response.setContentType() Modify the printed text to create a legal HTML page You should set all headers before writing the document content. Can you guess why?,Download LiveHttpHeaders Extension,As you can check usin
18、g LiveHttpHeaders plug-in,17,public class HelloWorld extends HttpServlet public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException, IOException PrintWriter out = response.getWriter();out.println(“Hello Worldn“);out.println(“);out.println(“ + new java.util.Date
19、() + “n“);out.println(“Hello Worldn“); ,HelloWorld.java,Content type wasnt set, but the browser will understand )dont rely on this in a real product / the project),18,Configuring the Server,helloHelloWorldhello/hello,myApp/WEB-INF/web.xml,http:/inferno:5000/dbi/hello,More on this in when we Tomcat i
20、n depth,19,Getting Information From the Request,20,An HTTP Request Example,GET /default.asp HTTP/1.0 Accept: image/gif, image/x-xbitmap, image/jpeg, image/png, */* Accept-Language: en Connection: Keep-Alive Host: magni.grainger.uiuc.edu User-Agent: Mozilla/4.04 en (WinNT; I ;Nav) Cookie:SITESERVER=I
21、D=8dac8e0455f4890da220ada8b76f; ASPSESSIONIDGGQGGGAF=JLKHAEICGAHEPPMJKMLDEM Accept-Charset: iso-8859-1,*,utf-8,21,Getting HTTP Data,Values of the HTTP request can be accessed through the HttpServletRequest object Get the value of the header hdr using getHeader(“hdr“) of the request argument Get all
22、header names: getHeaderNames() Methods for specific request information: getCookies, getContentLength, getContentType, getMethod, getProtocol, etc.,Read more about the HttpRequest Interface,22,public class ShowRequestHeaders extends HttpServlet public void doGet(HttpServletRequest request,HttpServle
23、tResponseresponse) throws ServletException, IOException response.setContentType(“text/html“);PrintWriter out = response.getWriter();String title = “Servlet Example: Showing Request Headers“;out.println(“ + title + “n“ + “ + title+ “n“+ “Request Method: “+request.getMethod()+“+ “Request URI: “+reques
24、t.getRequestURI()+“+ “ServletPath: “+request.getServletPath()+“+ “Request Protocol: “+request.getProtocol()+“+ “n“+ “Header NameHeader Value“);,23,Enumeration headerNames = request.getHeaderNames();while (headerNames.hasMoreElements() String headerName = (String) headerNames.nextElement();out.printl
- 1.请仔细阅读文档,确保文档完整性,对于不预览、不比对内容而直接下载带来的问题本站不予受理。
- 2.下载的文档,不会出现我们的网址水印。
- 3、该文档所得收入(下载+内容+预览)归上传者、原创作者;如果您是本文档原作者,请点此认领!既往收益都归您。
下载文档到电脑,查找使用更方便
2000 积分 0人已下载
下载 | 加入VIP,交流精品资源 |
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- INTRODUCTIONTOJAVASERVERSIDETECHNOLOGIESSERVLETSANDJSPPPT

链接地址:http://www.mydoc123.com/p-376673.html