Tuesday, December 7, 2021

Deploying the Servlet

 Deploying the Servlet


1. Create A directory Structure:

The directory structure defines that where to put the different types of files so that web container may get the information and respond to the client.
The Sun Microsystems defines a unique standard to be followed by all the server vendors.

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
Note: The HttpServlet class is widely used to create the servlet because it provides methods to handle http requests such as doGet(), doPost, doHead() etc.


  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:
Step1: Create one folder name: Servlets
Step2: Under Servlets folder load below html file .

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.

 web.xml
<web-app>
<servlet>
<servlet-name>s1</servlet-name>
<servlet-class>TestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>s1</servlet-name>
<url-pattern>/TestServlet</url-pattern>
</servlet-mapping>
</web-app>

Stpe5: under WEB-INF, create one more folder namely "classes".
Step6: in  "classes" folder , load below java file and name it as TestServlet.java.
TestServlet.java

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class TestServlet extends HttpServlet
{
public void doGet(HttpServletRequest req, HttpServletResponse res)throws ServletException,IOException
{
res.setContentType("text/html");
PrintWriter out= res.getWriter();
String s1=req.getParameter("tf1");
out.print("<h1> Welcome"+s1+"</h1>");
out.close();
}
}
Step7: copy this Servlet folder into "C:\Program Files\Apache Software Foundation\Tomcat 9.0\webapps".
Step8: Execute the java file from webapps folder
Step9: Start the tomcat server by typing localhost:8080 in your browser's URL.
Step10: after starting of tomcat server, deploy your servlet using localhost:8080/'folder-name'


No comments:

Post a Comment

A simple Java program to find the inverse of a given matrix

  import java.util.Scanner; public class MatrixInverse { public static void main (String[] args) { Scanner scanner =...