1 CS6320 – Deployment and Context L. Grewe 2 The Servlet Container Servlets run in a container...

9
1 CS6320 – CS6320 – Deployment and Deployment and Context Context L. Grewe L. Grewe

Transcript of 1 CS6320 – Deployment and Context L. Grewe 2 The Servlet Container Servlets run in a container...

11

CS6320 – Deployment CS6320 – Deployment and Contextand Context

L. GreweL. Grewe

22

The Servlet ContainerThe Servlet Container Servlets run in a container sometimes called the Servlets run in a container sometimes called the

Servlet engine.Servlet engine. You must “deploy” a servlet for it to be able to run.You must “deploy” a servlet for it to be able to run. We deploy servlet(s) as part of a entity called a We deploy servlet(s) as part of a entity called a

WebApp and this is what is actually deployed in our WebApp and this is what is actually deployed in our containers.containers.

Many kinds of Servlet Containers available free and Many kinds of Servlet Containers available free and commercialcommercial

33

Supporting ServletsSupporting Servlets To run Servlets, the Web server must support themTo run Servlets, the Web server must support them

• ApacheApache TomcatTomcat Also functions as a module for other Apache serversAlso functions as a module for other Apache servers

• SunSun Java System Web Server Java System Web Server andand Java System Java System Application Server (J2EE Server)Application Server (J2EE Server)

• IBMIBM's 's WebSphereWebSphere Application Server Application Server

• BEABEA’s’s WeblogicWeblogic Application Server Application Server

• MacromediaMacromedia’s’s JrunJrun – an engine that can be added to – an engine that can be added to Microsoft’s IIS, Apache’s Web servers and more...Microsoft’s IIS, Apache’s Web servers and more...

• OracleOracle Application Server Application Server

• ……

44

For MORE DETAILS ON DEPLOYMENT and web.xmlfile see our class website!!!! THIS CAN CHANGE with yourchoice 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 onversion 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.

DeploymentDeployment

55

<web-app>…<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>

Optional of the WebApp deployment Optional of the WebApp deployment descriptor file web.xml ---Servlet 3.* descriptor file web.xml ---Servlet 3.* and on use container specfic xml + and on use container specfic xml + annotations in code –see webpageannotations in code –see webpage

66

Loading a Servlet on StartupLoading a Servlet on Startup

A Servlet is usually loaded when it is A Servlet is usually loaded when it is first being calledfirst being called

You can set Tomcat to load a specific You can set Tomcat to load a specific Servlet on startup in the Servlet Servlet on startup in the Servlet declaration inside declaration inside web.xmlweb.xml

<servlet> <servlet-name>InitExample</servlet-name> <servlet-class>ServletInit</servlet-class> <load-on-startup/></servlet>

77

The Servlet ContextThe Servlet Context

88

The Servlet Context ObjectThe Servlet Context Object A A Servlet contextServlet context represents the Web represents the Web

application that Servlets live inapplication that Servlets live in There is one Servlet context per applicationThere is one Servlet context per application You can get the Servlet context using the You can get the Servlet context using the

method method getServletContext()getServletContext() The Servlet context has many methodsThe Servlet context has many methods For example, you can store in it objects For example, you can store in it objects

that are kept throughout the application's that are kept throughout the application's lifelife

99

An Example: Service CountAn 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)); }}