ServletsServersApachePortsPorts IICGI ScriptsSlide 7Servlets vs. CGI scriptsTomcatSlide 10What does a servlet do?Important servlet methods, IHTTP requestsImportant servlet methods, IILess important requestsA “Hello World” servlet (from the Tomcat installation documentation)The superclassThe doGet methodParameters to doGet and doPostdoGet and doPostUsing the HttpServletResponseUsing the PrintWriterInput to a servletGetting the parametersEnumeration reviewExample of input parametersJava review: Data from StringsWhat’s left?The EndJan 15, 2019Servlets2ServersA server is a computer that responds to requests from a clientTypical requests: provide a web page, upload or download a file, send emailA server is also the software that responds to these requests; a client could be the browser or other software making these requestsTypically, your little computer is the client, and someone else’s big computer is the serverHowever, any computer can be a serverIt is not unusual to have server software and client software running on the same computer3ApacheApache is a very popular server66% of the web sites on the Internet use ApacheApache is:Full-featured and extensibleEfficientRobustSecure (at least, more secure than other servers)Up to date with current standardsOpen sourceFreeWhy use anything else?4PortsA port is a connection between a server and a clientPorts are identified by positive integersA port is a software notion, not a hardware notion, so there may be very many of themA service is associated with a specific portTypical port numbers:21—FTP, File Transfer Protocol22—SSH, Secure Shell25—SMTP, Simple Mail Transfer Protocol53—DNS, Domain Name Service80—HTTP, Hypertext Transfer Protocol8080—HTTP (used for testing HTTP)7648, 7649—CU-SeeMe27960—Quake IIIThese are the ports of most interest to us5Ports IIMy UPenn Web page is:http://www.cis.upenn.edu/~matuszekBut it is also:http://www.cis.upenn.edu:80/~matuszekThe http: at the beginning signifies a particular protocol (communication language), the Hypertext Transfer ProtocolThe :80 specifies a portBy default, the Web server listens to port 80The Web server could listen to any port it choseThis could lead to problems if the port was in use by some other serverFor testing servlets, we typically have the server listen to port 8080In the second URL above, I explicitly sent my request to port 80If I had sent it to some other port, say, 99, my request would either go unheard, or would (probably) not be understood6CGI ScriptsCGI stands for “Common Gateway Interface”Client sends a request to serverServer starts a CGI scriptScript computes a result for server and quitsAnother client sends a requestclientserverclientscriptServer starts the CGI script againEtc.scriptServer returns response to clientscript7ServletsA servlet is like an applet, but on the server sideClient sends a request to serverServer starts a servletServlet computes a result for server and does not quitAnother client sends a requestclientserverclientservletServer calls the servlet againEtc.Server returns response to client8Servlets vs. CGI scriptsAdvantages:Running a servlet doesn’t require creating a separate process each timeA servlet stays in memory, so it doesn’t have to be reloaded each timeThere is only one instance handling multiple requests, not a separate instance for every requestUntrusted servlets can be run in a “sandbox”Disadvantage:Servlets must be in Java (CGI scripts can be in any language)9TomcatTomcat is the Servlet Engine than handles servlet requests for ApacheTomcat is a “helper application” for ApacheIt’s best to think of Tomcat as a “servlet container”Apache can handle many types of web servicesApache can be installed without TomcatTomcat can be installed without ApacheIt’s easier to install Tomcat standalone than as part of ApacheBy itself, Tomcat can handle web pages, servlets, and JSPApache and Tomcat are open source (and therefore free)10ServletsA servlet is any class that implements the javax.servlet.Servlet interface In practice, most servlets extend the javax.servlet.http.HttpServlet classSome servlets extend javax.servlet.GenericServlet instead Servlets, like applets, usually lack a main method, but must implement or override certain other methods11What does a servlet do?1. Read any data sent by the user2. Look up any information about the request that is embedded in the HTTP request3. Generate the results4. Format the results inside a document5. Set the appropriate HTTP parameters6. Send the document back to the clientFrom: Core Servlets and JavaServerPages, by Marty Hall12Important servlet methods, IWhen a servlet is first started up, its init(ServletConfig config) method is called init should perform any necessary initializationsinit is called only once, and does not need to be thread-safeEvery servlet request results in a call toservice(ServletRequest request, ServletResponse response)service calls another method depending on the type of service requestedUsually you would override the called methods of interest, not service itselfservice handles multiple simultaneous requests, so it and the methods it calls must be thread safeWhen the servlet is shut down, destroy() is calleddestroy is called only once, but must be thread safe (because other threads may still be running)13HTTP requestsWhen a request is submitted from a Web page, it is almost always a GET or a POST requestThe HTTP <form> tag has an attribute action, whose value can be "get" or "post"The "get" action results in the form information being put after a ? in the URLExample:http://www.google.com/search?hl=en&ie=UTF-8&oe=UTF-8&q=servletsThe & separates the various parametersOnly a limited amount of information can be sent this way"post" can send large amounts of informationThe information is in the body of the HTTP request14Important servlet methods, IIThe service method dispatches (sends to another method) the following kinds of requests: DELETE, GET, HEAD, OPTIONS, POST, PUT, and TRACEA GET request is dispatched todoGet(HttpServletRequest request, HttpServletResponse response)A POST request is dispatched todoPost(HttpServletRequest request, HttpServletResponse response)These are the two methods
View Full Document