* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Download Lecture Notes
Survey
Document related concepts
Transcript
MC365 Application Servers: Servlets Today We Will Cover: • What a servlet is • The HTTPServlet and some of its more important methods • How to configure the application server to use your servlets • Some example servlets • Using properties files • Some common errors and how to debug servlets What is a Servlet? • Sun describes a servlet in this way: – “A servlet is a Java programming language class used to extend the capabilities of servers that host applications access via a request-response programming model. Although servlets can respond to any type of request, they are commonly used to extend the applications hosted by Web servers. For such applications, Java Servlet technology defines HTTPspecific servlet classes. “ • The javax.servlet and javax.servlet.http packages provide interfaces and classes for writing servlets. All servlets must implement the Servlet interface, which defines life-cycle methods. HttpServlet Class • When implementing a generic service, you can use or extend the GenericServlet class provided with the Java Servlet API. • The HttpServlet class, though, provides methods, such as doGet and doPost, for handling HTTPspecific services. • HttpServlets are best used to generate responses to HTTP requests. – This is the basis for web-based applications. The LifeCycle of a Servlet? • The life cycle of a servlet is controlled by the container in which the servlet has been deployed. When a request is mapped to a servlet, the container performs the following steps. 1. If an instance of the servlet does not exist, the Web container a. Loads the servlet class. b. Creates an instance of the servlet class. c. Initializes the servlet instance by calling the init method. 2. Invokes a service method, passing a request and response object. • If the container needs to remove the servlet, it finalizes the servlet by calling the servlet's destroy method. Important HTTPServlet Methods • init – After the Web container loads and instantiates the servlet class and before it delivers requests from clients, the Web container initializes the servlet. – You can customize this process to allow the servlet to read persistent configuration data, initialize resources, and perform any other one-time activities by overriding the init method of the Servlet interface. – A servlet that cannot complete its initialization process should throw UnavailableException. Important HTTPServlet Methods • doGet and doPost – Called when browser makes an HTTP get request – Most calls are gets • doPost – Called when browser makes an HTTP post request • Usually you can handle both types of calls similarly Important HTTPServlet Methods • Finalize – When a servlet container determines that a servlet should be removed from service (for example, when a container wants to reclaim memory resources, or when it is being shut down), it calls the destroy method of the Servlet interface. • In this method, you release any resources the servlet is using and save any persistent state. – All of a servlet's service methods should be complete when a servlet is removed. The server tries to ensure this completion by calling the destroy method only after all service requests have returned or after a server-specific grace period, whichever comes first. An Example of a Servlet • To see the source code for a very simple servlet, go to: http://www2.bc.edu/~bernier/MC365/Lecture Notes/SimpleServlet.java Configuring Tomcat to Use New Servlets • Set context path – The context path is the first path name that comes after the url of your server • • • • In the url http://localhost:8080/test http://localhost:8080 is the server name and port /test is the context path Set the context path for new applications in the C:\Program Files\Apache Group\Tomcat 4.1\conf\server.xml. – Search for “context path” to find out where to make this change – You can see a sample server.xml file here. • You must restart the app server when you update the server.xml config file. Configuring Tomcat to Use New Servlets • Set servlet mappings – Servlet mappings refers to the mapping of servlets to url patterns. – In web.xml • Invoker – To use a servlet name you must have invoker in servlet mapping in web.xml – test with http://localhost:8080/test/servlet/SimpleServlet • Mappings – If you want to use a url other than the servlet name, use the servlet mapping tags. • See a sample web.xml configuration file here. Configuring Tomcat to Use New Servlets • Autoloading of servlets: – When you update servlets or web.xml you do not need to restart Tomcat – This is a nice feature of Tomcat. Debugging Servlets • Some common errors: – HTTP Status 404: • This means that the page/servlet is not found. • Either the servlet class is not in the classes folder or you did not configure the app server correctly. – HTTP Status 500: • This is the error code you get when a servlet abends (ends abnormally). • To correct this, fix the servlet. – Use logging to debug servlet issues. • If you do not define your own logger, app server messages go to the stdout.log and stderr.log files in the logs directory.