Vous êtes sur la page 1sur 3

How to Deploy a servlet at New Location in Tomcat 6.

0+:

0>> Stop the Tomcat Server.

1>> In order to deploy a new servlet at new location in tomcat 6.0 server folders
create the following directories and files:

Here uses new path as <TOMCAT_HOME>/webapps/ROOT /


//==========================================================================
=======================//
@ Folders
<TOMCAT_HOME>/webapps/ROOT /MyJSP - To create new JSP files
<TOMCAT_HOME>/webapps/ROOT /WEB-INF/classes - It is the place where tomcat
locates all class files.
<TOMCAT_HOME>/webapps/ROOT /WEB-INF/classes/MyServlets - To create/place new
java class files.

@ Files
<TOMCAT_HOME>/webapps/ROOT /MyJSP/MyServletTestJSP.jsp - Jsp file that
access the servlet file.
<TOMCAT_HOME>/webapps/ROOT /WEB-INF/classes/MyServlets/MyNewServlet.java -
It is the actual servlet file.

Here we need a deployment descriptor, "web.xml" - an XML file. By default


one is provided. If no file with name "web.xml" is found in the
irector <TOMCAT_HOME>/webapps/ROOT /WEB-INF/
//==========================================================================
=======================//
Create a new "web.xml" file with the following content inside it
//==========================================================================
========================//
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">

<display-name>Welcome to Tomcat</display-name>
<description>
Welcome to Tomcat
</description>

</web-app>
//==========================================================================
=======================//

2>> Create Servlet MyNewServlet.java :

By using any of your favourite editor type and save the following file with
the name "MyNewServlet.java"
//==========================================================================
========//
package MyServlets;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import javax.servlet.ServletConfig;

import java.io.IOException;
import java.io.PrintWriter;

public class MyNewServlet extends HttpServlet {


public void init(ServletConfig conf) {
try {
super.init(conf);
}catch(ServletException se) {}
}
public void doGet(HttpServletRequest req, HttpServletResponse res) {
try {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.println("<html><head><title>Packaged Servlet Hello</title>");
out.println("<body bgcolor=\"gray\" text=\"white\"><div
align=\"center\">This is an example Servlet that is packaged into MyServlets
package under ROOT folder</div></body></html>");
}catch(IOException ioe) {}
}
}
//==========================================================================
=======//

Now compile the above java file[servlet], then after compilation the class
file for the MyNewServlet.java is created. That's the file we needs.

3>> Deploy the servlet in web.xml :

Deployment means introducing the created servlet into the server. Each time
a request for the Servlet[ we created ] is resolved by the server by using
mapping path in the deployment descriptor.
Type the following servlet class and mapping info into the file
<TOMCAT_HOME>/webapps/ROOT /WEB-INF/web.xml
in between the <web-app></web-app> container tags[ Just before at the </web-
app> tag]
//======================================================//
<servlet>
<servlet-name>MyNewServlet</servlet-name>
<servlet-class>MyServlets.MyNewServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>MyNewServlet</servlet-name>
<url-pattern>/MyServlets/MyNewServlet</url-pattern>
</servlet-mapping>
//=====================================================//

4>> Create a Jsp MyServletTestJSP.jsp file that access the servlet MyNewServlet :

//==========================================================================
====//
<%@ page contentType="text/html; charset=ISO-8859-1" session="false" %>
<%-- MyServletTestJSP.jsp --%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>MyServlet Test Page</title>
<head>
<body bgcolor="#ffffff" text="orange">
<form method="GET" action="http://localhost:8080/MyServlets/MyNewServlet">
<div align="center">Click here to load Servlet<input type="submit"
value="Click here" /></div>
</form>
</body>
</html>
//==========================================================================
====//

6>> Start the tomcat Server.

Type the following URL


http://localhost:8080/MyJSP/MyServletTestJSP.jsp

By clicking the "Click here" button, browser loads our servlet file; you
might observe this by checking the address on the Browser's location
http://localhost:8080/MyServlets/MyNewServlets

Thank you
regards
J. M. V. Swamy Naidu, M.C.A.
For more at
http://www.NaiduMCA.co.cc and
http://www.docstoc.com/profile/StudentPowerMCA

Vous aimerez peut-être aussi