Servlet Example - Steps To Create A Servlet Example - Javatpoint
Servlet Example - Steps To Create A Servlet Example - Javatpoint
Here, we are going to use apache tomcat server in this example. The steps are
as follows:
Introduction To SQL
In-Person Training Course
impartica-training.co.uk OPEN
https://www.javatpoint.com/steps-to-create-a-servlet-using-tomcat-server 1/11
12/26/2018 Servlet Example : Steps to create a servlet example - javatpoint
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 Microsystem defines a unique standard to be followed by all the server
vendors. Let's see the directory structure that must be followed to create the
servlet.
As you can see that the servlet class file must be in the classes folder. The
web.xml file must be under the WEB-INF folder.
2)Create a Servlet
The HttpServlet class is widely used to create the servlet because it provides
methods to handle http requests such as doGet(), doPost, doHead() etc.
In this example we are going to create a servlet that extends the HttpServlet
class. In this example, we are inheriting the HttpServlet class and providing the
implementation of the doGet() method. Notice that get request is the default
request.
DemoServlet.java
import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
public class DemoServlet extends HttpServlet{
⇧
https://www.javatpoint.com/steps-to-create-a-servlet-using-tomcat-server 2/11
12/26/2018 Servlet Example : Steps to create a servlet example - javatpoint
2) weblogic.jar Weblogic
3) javaee.jar Glassfish
4) javaee.jar JBoss
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.
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. ⇧
https://www.javatpoint.com/steps-to-create-a-servlet-using-tomcat-server 3/11
12/26/2018 Servlet Example : Steps to create a servlet example - javatpoint
There are many elements in the web.xml file. Here is given some necessary
elements to run the simple servlet program.
web.xml file
<web-app>
<servlet>
<servlet-name>sonoojaiswal</servlet-name>
<servlet-class>DemoServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>sonoojaiswal</servlet-name>
<url-pattern>/welcome</url-pattern>
</servlet-mapping>
</web-app>
There are too 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:
Go to My Computer properties:
https://www.javatpoint.com/steps-to-create-a-servlet-using-tomcat-server 5/11
12/26/2018 Servlet Example : Steps to create a servlet example - javatpoint
Write JAVA_HOME in variable name and paste the path of jdk folder in variable
value:
https://www.javatpoint.com/steps-to-create-a-servlet-using-tomcat-server 6/11
12/26/2018 Servlet Example : Steps to create a servlet example - javatpoint
After setting the JAVA_HOME double click on the startup.bat file in apache
tomcat/bin.
https://www.javatpoint.com/steps-to-create-a-servlet-using-tomcat-server 7/11
12/26/2018 Servlet Example : Steps to create a servlet example - javatpoint
Changing the port number is required if there is another server running on the
same system with same port number.Suppose you have installed oracle, you need
to change the port number of apache tomcat because both have the default port
number 8080.
https://www.javatpoint.com/steps-to-create-a-servlet-using-tomcat-server 8/11
12/26/2018 Servlet Example : Steps to create a servlet example - javatpoint
But there are several ways to deploy the project. They are as follows:
You can also create war file, and paste it inside the webapps directory. To do so,
you need to use jar tool to create the war file. Go inside the project directory
(before the WEB-INF), then write:
Creating war file has an advantage that moving the project from one location to
another takes less time.
http://localhost:9999/demo/welcome
<<prev next>>
https://www.javatpoint.com/steps-to-create-a-servlet-using-tomcat-server 9/11
12/26/2018 Servlet Example : Steps to create a servlet example - javatpoint
Learn more
Please Share
B.Tech / MCA
DBMS DS DAA OS
https://www.javatpoint.com/steps-to-create-a-servlet-using-tomcat-server 10/11
12/26/2018 Servlet Example : Steps to create a servlet example - javatpoint
Intellij DBMS
https://www.javatpoint.com/steps-to-create-a-servlet-using-tomcat-server 11/11