CS6320 Deployment and Context L Grewe 1 The Servlet Container Servlets run in a container sometimes called the Servlet engine You must deploy a servlet for it to be able to run We deploy servlet s as part of a entity called a WebApp and this is what is actually deployed in our containers Many kinds of Servlet Containers available free and commercial 2 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 J2EE Server IBM s WebSphere Application Server BEA s Weblogic Application Server Macromedia s Jrun an engine that can be added to Microsoft s IIS Apache s Web servers and more Oracle Application Server 3 Deployment For MORE DETAILS ON DEPLOYMENT and web xml file see our class website THIS CAN CHANGE with your choice of servlet container You will need a correct directory structure Compile your code Organize supporting files html etc Create deployment descriptor files e g container specific xml files like sun web xml for GlassFish and depending on version of J2EE optional web xml file Possibly zip up your code into a WebApp file called WAR web archive Finally you need to deploy your Web Application WebApp using the containers administrative deployment tools 4 Optional of the WebApp deployment descriptor file web xml Servlet 3 and on use container specfic xml web app annotations in code see webpage servlet servlet name InitExample servlet name servlet class ServletInit servlet class init param param name login param name param value snoopy param value init param servlet web app 5 Loading a Servlet on Startup A Servlet is usually loaded when it is first being called You can set Tomcat to load a specific Servlet on startup in the Servlet declaration inside web xml servlet servlet name InitExample servlet name servlet class ServletInit servlet class load on startup servlet 6 The Servlet Context 7 The Servlet Context Object A Servlet context represents the Web application that Servlets live in There is one Servlet context per application You can get the Servlet context using the method getServletContext The Servlet context has many methods For example you can store in it objects that are kept throughout the application s life 8 An Example Service Count public class CounterServlet extends HttpServlet public void init throws ServletException Integer counter Integer getServletContext getAttribute counter if counter null getServletContext setAttribute counter new Integer 0 9
View Full Document
Unlocking...