Servlet - Servlet Basics Deploying Servlet Using Tomcat Rating: 4.7/5 (6 votes cast) | Level : Beginners Author : Arunkumar S Download Source : Not Avaliable
Deploying Servlet Web Applications to TomcatAssume that we have: - An application in a package named pack1
- The application consists of one Java class HelloWorld.java
- HelloWorld is a Servlet, i.e. it extends HttpServlet class.
- Compile HelloWorld.java to get HelloWorld.class
//HelloWorld.java public class HelloWorld extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { res.setContentType("text/html"); PrintWriter out = res.getWriter(); out.println("<HEAD><TITLE> www.JavaOrigin.com</TITLE></HEAD><BODY>"); out.println("<h1> HelloWorld - www.JavaOrigin.com</h1>"); out.println("<P>This is output is from HelloWorld."); out.println("</BODY>"); out.close(); } } - In TOMCAT_HOME\webapps\ create a directory named javaorigin (ex c:\Tomcat6.0\webapps\)
- In javaorigin create a directory named WEB-INF
- In WEB-INF create a directory named classes
- By now we have the following path: TOMCAT_HOME\webapps\javaorigin\WEB-INF\classes\
- Copy HelloWorld.class into classes directory.
- Create web.xml file and place into TOMCAT_HOME\webapps\javaorigin\WEB-INF\ .
- Open web.xml file and type following servlet mapping tegs
Copy the following text into the file: <?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN" "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
<web-app> <servlet> <servlet-name>HelloWorld</servlet-name> <servlet-class>HelloWorld</servlet-class> </servlet> <servlet-mapping> <servlet-name>HelloWorld</servlet-name> <url-pattern>/HelloWorld</url-pattern> </servlet-mapping> </web-app> To test the Servlet, go to this URL in the browser http://localhost:8080/javaorigin/HelloWorld This will run your Servlet. Notice that the use of the word servlet in the URL's path does not correspond to an actual directory. If the Servlet class did not have a package we would have copied it in TOMCAT_HOME\webapps\javaorigin\WEB-INF\classes\ , and would run it with the URL http://localhost:8080/javaorigin/HelloWorld - Assume that we want to install the application in a directory named C:\jap1\
- In C:\javaorigin\ create a directory named WEB-INF
- In WEB-INF create a directory named classes
- By now we have the following path: C:\javaorigin\WEB-INF\classes\pack1\
- Copy HelloWorld.class into classes directory.
- Using a text editor, open the file TOMCAT_HOME\conf\server.xml
- Find the </ContextManager> tag. (Note: this is an end tag)
- Directly in the line before it, insert the following:
<Context path="/javaorigin" docBase="c:/javaorigin" crossContext="false" debug="0" reloadable="true" > </Context> - Save and close the file.
This tells the server to map requests that start with /javaorigin (after the server name and port number) to the directory c:/javaorigin http://localhost:8080/javaorigin/HelloWorld < Previous 1 | 2 | 3 |
Discussion about this tutorial
 Start a new Discussion | Read All Discussion
|