This tutorial will guide you how to create and deploy Servlet without using Eclipse IDE.
Step 1. Download Tomcat : Tomcat is an application server from the Apache Software Foundation that executes Java servlets and renders Web pages that include Java Server Page coding.
Download latest release of Tomcat https://tomcat.apache.org/download-80.cgi
Step 2. Configure Tomcat : Unzip the tomcat package to C (or other) drive in my case it is D:\software\apache-tomcat-7.0.65-windows-x64\apache-tomcat-7.0.65
Now setup the JAVA_HOME, CATALINA_HOME, PATH environment variable:
Creating JAVA_HOME :
Click on -> Control Panel -> System -> Advanced->Environment Variable in User variables tab add new variable name: JAVA_HOME and variable value : C:\ "top level directory of your java install"
eg: variable name JAVA_HOME
variable value : C:\Program Files\Java\jdk1.8.0_40
Note: Separate variable values by semicolon, if other value already exists(;)
Creating CATALINA_HOME :
Click on -> Control Panel -> System -> Advanced->Environment Variable in User variables tab add new variable name: CATALINA_HOME and variable value : C:\ "top level directory of your Tomcat install"
eg: variable name CATALINA_HOME :
variable value : D:\software\apache-tomcat-7.0.65-windows-x64\apache-tomcat-7.0.65
Note: Separate variable values by semicolon, if other value already exists(;)
Appendng PATH :
Click on -> Control Panel -> System -> Advanced->Environment Variable in User variables tab select path variable and click on edit button. In variable value section give a semicolon and at the end paste the following text : %PATH%;%JAVA_HOME%\bin;%CATALINA_HOME%\bin
Step 3. Starting Tomcat: Go to the location of startup.bat D:\software\apache-tomcat-7.0.65-windows-x64\apache-tomcat-7.0.65\bin and double click on startup.bat
Now go to the browser and give url as localhost:8080 it should bring the apache tomcat home page.
Step 4. Creating Directory structure;
Step 5. Creating Servlet:
import javax.servlet.http.*;
import javax.servlet.*;
import java.io.*;
public class DemoServlet extends HttpServlet{
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException,IOException{
res.setContentType("text/html");
PrintWriter pw = res.getWriter();
pw.println("<html><body>");
pw.println("Welcome to servlet");
pw.println("</body></html>");
pw.close();
}
}
Step 6. Compiling Servlet:
Go the directory containing DemoServlet and compile DemoServlet using servlet-api.jar.
eg: javac -classpath "D:\software\apache-tomcat-7.0.65-windows-x64\apache-tomcat-7.0.65\lib\servlet-api.jar" "DemoServlet.java"
Copy the DemoServlet.class file to the classes directory.
Step 7. Creating Deployment Descriptor:
<web-app>
<servlet>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>default.html</welcome-file>
</welcome-file-list>
<servlet-name>demo</servlet-name>
<servlet-class>DemoServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>demo</servlet-name>
<url-pattern>/demoPage</url-pattern>
</servlet-mapping>
</web-app>
Step 8. Deploying application to server:
Method 1-> Copy the application folder (here it is context root) to the apache tomcat's webapps directory for example I copied inside D:\software\apache-tomcat-7.0.65-windows-x64\apache-tomcat-7.0.65\webapps
Method 2-> Create WAR of your application and deploy it to tomcat through browser.
eg: Creating WAR, in cmd do D:\myPrograms\servlets\web-app>jar -cvf myProject.war *
Upload myProject.war and deploy in Tomcat server.
Step 9. Access the Servlet through URL: http://localhost:8084/myProject/demoPage
No comments:
Post a Comment