first servlet program by using Note pad (without using IDE)
developing the first servlet program
1. creating the home.html page
2. creating the servlet program (Example: lifycycleservlet)
3. creating deployment descriptor
4. deploying the application
5. running the application
Note:
<html>
<head>
<title servlet example </title>
</head>
<body>
<form action="myfirstServlet"><br/><br/><br/>
<center>
<input type="submit" value="invoke servlet lifecycle"/>
</center>
</form>
</body>
</html>
Deploying the application
developing the first servlet program
1. creating the home.html page
2. creating the servlet program (Example: lifycycleservlet)
3. creating deployment descriptor
4. deploying the application
5. running the application
Note:
1. creating the home.html page (NAME IT AS ANY NAME)
<html>
<head>
<title servlet example </title>
</head>
<body>
<form action="myfirstServlet"><br/><br/><br/>
<center>
<input type="submit" value="invoke servlet lifecycle"/>
</center>
</form>
</body>
</html>
- IT DISPLAYS THE WEBPAGE, ONCE YOU CLICK THE BUTTON IN THAT WEB PAGE, IT CALL THE "myfirstServlet" action
- this webpage is mapped to servlet progame (Example: LifeCycleServlet.java) in web.xml
2. creating the servlet program (Example: lifycycleservlet)
Note: A Servlet is a Java class so let us begin by creating a new text document called LifeCycleServlet.java in the 'src' subfolder of the WEB-INF folder.
steps:
1. The Servlet begins by importing the packages required for the class
Ex:
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
2. Next we define our Servlet class as extending the Servlet class
public class LifeCycleServlet extends Servlet
{
}
Ex:
package com.rajendra.servlets;//this is optional, you can create any package
import javax.servlet.*;//this is mandatory
public class LifeCycleServlet implements Servlet
{
public void init(ServletConfig sc)
{
System.out.println("init() method");
}
public void service(ServletRequest req, ServletResponse res) throws ServletException, java.io.IOException
{
java.io.PrintWriter out=res.getWriter();
out.println("hi frm lifecycleservlet");
System.out.println("in service");
}
public void destroy()
{
System.out.println("in destroy()");
}
public String getServletInfo()
{
return "LifeCycleServlet";
}
public ServletConfig getServletConfig()
{
return config;
}
private ServletConfig config;
}
- save this program with its class name
- set the class path Ex: it it is Tomcat 5.5 version
- D:\raj\servlets>set classpath=C:\Program Files\Apache Software Foundation\Tomcat
- 5.5\common\lib\servlet-api.jar;
- Now compile the servlet
- Note:If you encountered the error "javax.servlet does not exist" then your classpath is incorrect
- D:\raj\servlets>javac LifeCycleServlet.java
- D:\raj\servlets>
3. creating deployment descriptor (Test the Servlet)
We need to define each Servlet that you are intending to use in the web.xml file as follows.
The deployment descriptor is an XML document, used to describe the servlet container of the services supported by it.
According to Servlet API , each web application should have a web.xml file
<?xml version="1.0"?>
<!DOCTYPE web-app
PUBLIC "-//"sun Microsystems, Inc.//DTD web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<servlet>
<servlet-name>LifeCycleServlet</servlet-name>
<servlet-class>com.rajendra.servlets.LifeCycleServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletLifeCycleServlet</servlet-name>
<url-pattern>/myFirstServlet</url-pattern>
</servlet-mapping>
</web-app>
According to Servlet API , each web application should have a web.xml file
<?xml version="1.0"?>
<!DOCTYPE web-app
PUBLIC "-//"sun Microsystems, Inc.//DTD web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<servlet>
<servlet-name>LifeCycleServlet</servlet-name>
<servlet-class>com.rajendra.servlets.LifeCycleServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletLifeCycleServlet</servlet-name>
<url-pattern>/myFirstServlet</url-pattern>
</servlet-mapping>
</web-app>
- <servlet-name> tag explains a unique name LifeCycleServlet
- <servlet-class> tag explains a qualified name of a Servlet class, com.rajendra.servlets.LifeCycleServlet</servlet-class>
- the <url-pattern> nested tag in <servlet-mapping> tag takes a string with / or *. This string is used to locate Servlet. we specified <url-pattern> /myFirstServlet which is a value specified in action attribute of home.html page.
4. deploying the application
- first we need to compile the servlet program that is LifeCycleServlet.java
- To compile the servlet program that is LifeCycleServlet.java we have to set the class path so that the javax.servlet package is available in the classpath. If we are using Tomcat server 5.5 set the class path as follows :
- D:\raj\servlets\demoapp>set classpath=C:\Program Files\Apache Software Foundation\Tomcat 5.5\common\lib\servlet-api.jar;
- For tomcat web server, this package comes inside the servlet-api-jar file and Web Logic Server, this package is present inside the weblogic.jar file.
- If we are using Weglogic Server class path like this
- set classpath=d:\bea\weblogic81\server\lib\weblogic.jar. (i assumed that that the weblogic server is installed in the D:\ bea folder
- after compiling the servlet program
- First of all let us create a new web application with the appropriate folder structure. In Tomcat you'll find a webapps folder below the installation folder. In this folder create a new subfolder called 'testapp' and a subfolder of this called WEB-INF. Below WEB-INF create two subfolders 'classes' and 'src' and a xml file 'web.xml'. Your folder structure should be similar to the following.
- After compiling the Servlet, arrange the files as follows in your directory: Example:
- <demoapp>*.html
- \WEB-INF\web.xml
- \WEB-INF\lib\*.jar & *.zip (files if any)
- \WEB-INF\classes [\<package-name>]*.class
- <demoapp> may contain .jsp, .html,jpg, and .gif files
- the classes directory contains compiled java classes, including the Servlet used by the Web application.
- the lib directory contains the .jar files used by the Web application.
- next prepare the war file using the jar tool. (to use the jar tool, open the command prompt and navigate to the work folder of your application (in our program, it is demoapp folder)
- type jar -cvf <war filename>.war*. for example, we type the following D:\raj\servlets\demoapp> run jar -cvf demoapp.war * or jar -cvf demoapp.war Home.html WEB-INF
Ex:
D:\raj\servlets\demoapp>jar -cvf demoapp.war
D:\raj\servlets\demoapp>jar -cvf demoapp.war Home.html WEB-INF
D:\raj\servlets\demoapp>jar -cvf demoapp.war
D:\raj\servlets\demoapp>jar -cvf demoapp.war Home.html WEB-INF
- Now the appplication is ready to deploy on a server.
Note: Creating WAR files is optional as we create war file after completing the development phase of an application and next we go for production phase
Deploying the application
- to deploy the application on the tomcat web server, copy the demoapp.war file into the <tomcat home>\webapps folder
- to deploy the application on the Weblogic application server, copy the demoapp.war file into the <weblogic domain root>\application folder
Running the Application->
start the server: to start the tomcat server,
- go to start->program files->Apachi tomcat 5.5->Configure Tomcat->start the button->click on Ok button
- to run the application, open the browser and type the URL
- if u are using Tomcat server use
- syntax: http://computer name or IP ADDRESS:port number/foldername/html name
- Ex: http://rajendra:8080/demoapp/Home.html name
No comments:
Post a Comment