Steps to create a servlet and start it in Tomcat:
Note: instructions with jdk1.7.0_80, newer instructions in Tomcat
Start Tomcat
Copy the folder Q:\Java\apache-tomcat-8.5.24 to folder P:\
Define the environment variables in the MS-DOS console:
SET JAVA_HOME=Q:\JAVA\jdk1.7.0_80
SET CATALINA_HOME=P:\apache-tomcat-8.5.24
SET PATH=%JAVA_HOME%\bin;%CATALINA_HOME%\bin;%PATH%
SET CLASSPATH=%CLASSPATH%;%CATALINA_HOME%\lib\servlet-api.jar;%CATALINA_HOME%\lib\Jama-1.0.2.jar;.
It’s recommended to save the previous statements in a bat file (example: sv.bat)
startup
Start the browser and write the address:
http://localhost:8080
shutdown
Create a Servlet
Step 1 of Start Tomcat (done)
Step 2 of Start Tomcat (type sv + return)
apache-tomcat-8.5.24
└── webapps
└── Servlet1
└── WEB-INF
└── web.xml
└── classes
Create the HelloWorld.java file in the webapps\Servlet1\WEB-INF\classes folder. HelloWorld.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloWorld extends HttpServlet{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println("Hello World");
}
}
javac HelloWorld.java
Create the web.xml file in P:\apache-tomcat-8.5.24\webapps\Servlet1\WEB-INF folder:
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app>
<servlet>
<servlet-name>primer</servlet-name>
<servlet-class>HelloWorld</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>primer</servlet-name>
<url-pattern>/start</url-pattern>
</servlet-mapping>
</web-app>
Start the Tomcat server as in step 3 of Start Tomcat:
startup
To execute the Java programs from a pendrive follow the next steps:
The content of the .bat file, for example s.bat, can be:
SET DRIVE=%cd:~0,3%
SET JAVA_HOME=%DRIVE%Java\jdk1.7.0_80
SET CATALINA_HOME=%DRIVE%Java\apache-tomcat-8.5.24
SET PATH=%JAVA_HOME%\bin;%CATALINA_HOME%\bin;%PATH%
SET CLASSPATH=%CLASSPATH%;%CATALINA_HOME%\lib\servlet-api.jar;%CATALINA_HOME%\lib\Jama-1.0.2.jar;.