This document outlines the steps to create a web application using servlets in Tomcat. It includes instructions for setting up the folder structure, creating necessary files such as HTML, web.xml, and the servlet program, as well as compiling and running the servlet. Additionally, it provides information on the required Java version and how to access the servlet via a web browser.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
39 views4 pages
Steps For Creating Servlet
This document outlines the steps to create a web application using servlets in Tomcat. It includes instructions for setting up the folder structure, creating necessary files such as HTML, web.xml, and the servlet program, as well as compiling and running the servlet. Additionally, it provides information on the required Java version and how to access the servlet via a web browser.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4
SERVLET EXECUTION STEPS
Procedure for creating web application using servlet
1. Create a new folder in webapps folder of Tomcat. Eg: FirstServlet 2. Create a sub folder with name WEB-INF (name, case should not be changed) in it 3. Create html file to access the servlet if required. (In case your servlet not loaded because of action performed from html form, no need of creating this file) Eg: first.html 4. In side WEB-INF, create two sub folders by name classes and lib. 5. Create a deployment descriptor file into WEB-INF folder with the name web.xml (Should not be modified) 6. Create servlet program inside WEB-INF folder. Eg: FirstServlet.java 7. Execute the Java program using the following steps 8. Set the path to point to appropriate Java version. (In this case when executed using Java 17 incompatibility issues raised. For Apache 8. 5. 23 the suitable Java version is Java 1.8 Steps from my local: (Here D:\Apache 8.5.23 is the location where Tomcat is installed on this machine. Choose the relevant path and execute your Java class) D:\Apache 8.5.23\webapps\FirstServlet\WEB-INF>set path="D:\Java\bin" D:\Apache 8.5.23\webapps\FirstServlet\WEB-INF>javac FirstServlet.java - classpath "D:\Apache 8.5.23\lib\servlet-api.jar" 9. Copy the .class file generated from the above step into WEB-INF/classes folder which we have created in current application 10. Copy servlet-api.jar into WEB-INF/lib folder. 11. Start the tomcat server and ensure that your server is running correctly. (Double click on the file shown here)
12. Observe the following icon in task bar
13. Run your servlet program using the link
http://localhost:8080/FirstServlet/first.html (in my case) 14. If you are directly accessing Servlet use the url pattern after that name of application in above URL as http://localhost:8080/FirstServlet/firstServlet (in my current example) Folder Structure: first.html <html> <head> <title> First Servlet </title> </head> <body> <form name="f1" action="/FirstServlet/firstServlet"> <input type="submit" value="Run Servlet"/> </form> </body> </html> web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app> <servlet> <servlet-name>FirstServlet</servlet-name> <servlet-class>FirstServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>FirstServlet</servlet-name> <url-pattern>/firstServlet</url-pattern> </servlet-mapping> </web-app> FirstServlet.java import javax.servlet.*; import java.io.*; public class FirstServlet extends GenericServlet { public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException { res.setContentType("text/html"); PrintWriter out = res.getWriter(); out.println("<h1>First servlet</h1>"); out.close(); } } Output: