Step to Deploy servlet application on tomcat web-server:
Step-1 Installing Tomcat :
If you have tomcat server already installed on your system go to step-2 directly.
Otherwise download the tomcat setup or zip file from http://tomcat.apache.org/ and extract the files wherever you prefer.Extracting is enough in case of zip file download,no special installation setup is required.
Step-2 Setting environment variables
>> Set JAVA_HOME variable to your JDK installation directory i.e C:\Program Files\Java\jdk1.6.0 in my
System.It may be different on your system so check for it.
>> Set classpath for servlet-api.jar file which contains all the servlet libraries which is essential for compiling
any servlet application.This servlet-api.jar file is provided with your server product in a location similar to
"D:\apache-tomcat-6.0.29\lib\servlet-api.jar" ,this path may vary according to the installation location of
tomcat server.
you need to execute this command from command prompt to set classpath:
set classpath=D:\apache-tomcat-6.0.29\lib\servlet-api.jar;
and your classpath is set proceed to next step.
Step-3 Designing Basic components like a servlet,html page.
>> Create a simple html file which will be used to invoke the servlet class.
//index.html
<html>
<center>
<form action="./myServlet" method="get">
<input type="submit"/>
</form>
</center>
</form>
>> Now its time to create a servlet class.
//Myservlet.java
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class Myservlet extends HttpServlet
{
public void doGet(HttpServletRequest req,HttpServletResponse resp)throws ServletException,IOException
{
PrintWriter pw=resp.getWriter();
pw.println("<html><center>this is servlet response</center></html>");
}
}
Step-4 Compile the servlet class.
Open the command prompt
Change current directory to the one which contains your Myservlet.java file
Now execute command: "javac Myservlet.java"
It will generate the class file for your servlet if there is no error.
Step-5 Create a deployment descriptor file i.e web.xml
deployment descriptor contains the servlet information like servlet class,servlet mapping and url pattern for which the container will invoke the servlet.
//web.xml
<web-app>
<servlet>
<servlet-name>myServlet</servlet-name>
<servlet-class>Myservlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>myServlet</servlet-name>
<url-pattern>/myServlet</url-pattern>
</servlet-mapping>
</web-app>
Step-6 Arranging all the above components in a standard directory structure
Create a root folder for your application i.e Sample here in my example
Inside root folder create a new folder WEB-INF.
Inside WEB-INF create sub-folder classes and arreange the all the files as shown in the directory structure.
Your final directory structure should look like the below one.
Deployment is nothing but placing root folder of your application inside the
"D:\apache-tomcat-6.0.29\webapps " folder that is inside the server installation directory.
Step-8 Make a request to your application:
>> start the tomcat server by clicking on startup.batch file located inside the bin folder of your tomcat installation directory.
>>open any browser and type the following url "http://localhost:8080/Sample/index.html "
it will show your html page with a submit button which will call Myservlet on clicking.
You are done,Good luck
No comments:
Post a Comment