Deploying the Servlet
2)Create a Servlet
There are three ways to create the servlet.
- By implementing the Servlet interface
- By inheriting the GenericServlet class
- By inheriting the HttpServlet class
3)Compile the servlet
For compiling the Servlet, jar file is required to be loaded. Different Servers provide different jar files:
1) servlet-api.jaràApache Tomcat
2) weblogic.jaràWeblogic
3) javaee.jaràGlassfish
4) javaee.jaràJboss
Two ways to load the jar file
1. set classpath
2. paste the jar file in JRE/lib/ext folder
Put the java file in any folder. After compiling the java file, paste the class file of servlet in WEB-INF/classes directory.
4)Create the deployment descriptor (web.xml file)
The deployment descriptor is an xml file, from which Web Container gets the information about the serlvet to be invoked.
The web container uses the Parser to get the information from the web.xml file. There are many xml parsers such as SAX, DOM and Pull.
There are many elements in the web.xml file. Here is given some necessary elements to run the simple servlet program
There are many elements in the web.xml file. Here is the illustration of some elements that is used in the above web.xml file. The elements are as follows:
<web-app> represents the whole application.
<servlet> is sub element of <web-app> and represents the servlet.
<servlet-name> is sub element of <servlet> represents the name of the servlet.
<servlet-class> is sub element of <servlet> represents the class of the servlet.
<servlet-mapping> is sub element of <web-app>. It is used to map the servlet.
<url-pattern> is sub element of <servlet-mapping>. This pattern is used at client side to invoke the servlet.
5)Start the Server and deploy the project
To start Apache Tomcat server, double click on the startup.bat file under apache-tomcat/bin directory.
One Time Configuration for Apache Tomcat Server
You need to perform 2 tasks:
set JAVA_HOME or JRE_HOME in environment variable (It is required to start server).
Change the port number of tomcat (optional). It is required if another server is running on same port (8080).
5) How to deploy the servlet project
Copy the project and paste it in the webapps folder under apache tomcat
6) How to access the servlet
Open broser and write http://hostname:portno/contextroot/urlpatternofservlet.
For example:
http://localhost:9999/demo/welcome
Example:index.html
<form action="TestServlet" method="get">
Enter Name:<input type="text" name="tf1"/><br>
<input type="submit" value="submit"/>
</form>
Step3: under Servlets folder ,create another folder with name as "WEB-INF"
Step4: in WEB-INF folder, create one more file named as web.xml like this.
No comments:
Post a Comment