EXECUTING SERVLETS & JSP WEB APPLICATION USING APACHE TOMCAT WEB SERVER 7.0.6
Step 1:
Install the Apache-Tomcat for Windows XP “apache-tomcat-7.0.6.exe” to the path C:\Program Files/
Tomcat will be installed as C:\Program Files\Apache Software Foundation
Step 2.
Create “web.xml” file as below :
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0"
metadata-complete="true">
<filter>
<filter-name>Request Dumper Filter</filter-name>
<filter-class>org.apache.catalina.filters.RequestDumperFilter</filter-class>
</filter>
<!-- Example filter to set character encoding on each request -->
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>filters.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>EUC_JP</param-value>
</init-param>
</filter>
<!-- Define your servlets that are included in the Beer Advice Application -->
<!-- http://localhost:8080/beer/BeerForm.html -->
<servlet>
<servlet-name>Beer</servlet-name>
<servlet-class>BeerSelect</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Beer</servlet-name>
<url-pattern>/SelectBeer.do</url-pattern>
</servlet-mapping>
<security-constraint>
<display-name>Example Security Constraint</display-name>
<web-resource-collection>
<web-resource-name>Protected Area</web-resource-name>
<!-- Define the context-relative URL(s) to be protected -->
<url-pattern>/jsp/security/protected/*</url-pattern>
<!-- If you list http methods, only those methods are protected -->
<http-method>DELETE</http-method>
<http-method>GET</http-method>
<http-method>POST</http-method>
<http-method>PUT</http-method>
</web-resource-collection>
<auth-constraint>
<!-- Anyone with one of the listed roles may access this area -->
<role-name>tomcat</role-name>
<role-name>role1</role-name>
</auth-constraint>
</security-constraint>
<!-- Security roles referenced by this web application -->
<security-role>
<role-name>role1</role-name>
</security-role>
<security-role>
<role-name>tomcat</role-name>
</security-role>
</web-app>
Step 3.
Create a Folder Structure \beer\WEB-INF under C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\
Copy the web.xml File to the path C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\beer\WEB-INF
After copying the web.xml , The directory structure will look like below :
Step 4.
Create a html File - “BeerForm.html” as below :
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD><TITLE>Apache Tomcat BEER Advice Web Application</TITLE>
<META http-equiv=Content-Type content="text/html">
</HEAD>
<BODY>
<P> <H3 align="center">Beer Selection Form</H3>
<P></P>
<form method="POST" action="/beer/SelectBeer.do">
Color :
<select name="color" size="1">
<option value="light">light</option>
<option value="amber">amber</option>
<option value="brown">brown</option>
<option value="dark">dark</option>
</select>
<br> <br>
<center> <input type="SUBMIT" value="Enter"> </center>
</form>
</BODY>
</HTML>Step 5.
“BeerForm.html” will look like below :
Copy the html File-“BeerForm.html” to the path “C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\beer” as below:
Step 6.
Open the Directory C:\Program Files\Apache Software Foundation\Tomcat 7.0\bin
Step 7.
Double click the “tomcat7w.exe” icon , You may get the window below :
Step 8.
Click on Start
Step 9.
As you can see in the Service Status :
Tomcat ApacheWebServer is 'Started'
Step 10.
Open the Browser , Type in the url below in the Address Bar and hit enter http://localhost:8080/beer/BeerForm.html
You will get the BeerForm.html page populated inside the Browser window
Step 11.
Create the Folder structure /classes under the path C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\beer\WEB-INF\
The new structure after creating the classes directory under WEB-INF will be as below :
Step 12.
< CREATING THE MODEL JAVA CLASS >
a)Create the Folder structure /model under the path C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\beer\WEB-INF\classes/
Open the model directory , Create a “BeerExpertModel.java” file inside the model directory :
import java.io.*;
import java.util.*;
import model.*;
public class BeerExpertModel
{
public List getBrands(String color)
{
List brandsList = new ArrayList();
if(color.equals("amber"))
{
brandsList.add("Jack Amber");
brandsList.add("Red Mosse Amber");
}
else
{
brandsList.add("Jail Pale Ale");
brandsList.add("Gout Stout");
}
return(brandsList);
}
}
Step 13.
< CREATING THE SERVLET WHICH ACTS AS CONTROLLER >
Open the “classes” directory
Create a Servlet “BeerSelect.java” inside the C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\beer\WEB-INF\classes as below:
import model.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class BeerSelect extends HttpServlet {
public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException
{
//REQUEST FORM PARAMETER
String strColor=request.getParameter("color");
//MODEL LAYER
BeerExpertModel bemlist=new BeerExpertModel();
List resultColorList=bemlist.getBrands(strColor);
//REQUEST SEND TO VIEW LAYER
request.setAttribute("styles",resultColorList);
RequestDispatcher view = request.getRequestDispatcher("BeerAdviceResult.jsp");
view.forward(request,response);
}
public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException {
//REQUEST FORM PARAMETER
String strColor=request.getParameter("color");
//MODEL LAYER
BeerExpertModel bemlist=new BeerExpertModel();
List resultColorList=bemlist.getBrands(strColor);
//REQUEST SEND TO VIEW LAYER
request.setAttribute("styles",resultColorList);
RequestDispatcher view = request.getRequestDispatcher("BeerAdviceResult.jsp");
view.forward(request,response);
}
}
Step 14. < CREATING THE JSP VIEW >
Open the path “C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\beer” and create a JSP File “BeerAdviceResult.jsp” under the application directory “beer”.
“BeerAdviceResult.jsp”
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<%@ page import="java.util.*"%>
<HTML><HEAD><TITLE>Apache Tomcat Examples</TITLE>
<META http-equiv=Content-Type content="text/html">
</HEAD>
<BODY>
<P>
<H3 align="center">Beer Advice Result JSP</H3>
<P></P>
<%
List stylesList = (List)request.getAttribute("styles");
Iterator styleitr=stylesList.iterator();
while(styleitr.hasNext())
{
out.print("<br> try : " +styleitr.next());
}
%>
</BODY>
</HTML>
Step 15. <CREATING A BATCH FILE TO COMPILE JAVA-MODEL AND JAVA-SERVLET>
Open the path C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\beer\WEB-INF\classes and we will create a Batch file” setenv.bat” to compile our Servlet Java file” BeerSelect.java” which is inside classes directory and Model java file “BeerExpertModel.java” which is inside model directory.
In the batch File below JAVA_HOME is the place where your JDK is installed.
PATH is C:\Program Files\Java\jdk1.6.0_03/bin , It is the place where
our “javac –compiler” exists and it will be used further to compile our Servlet Java file” BeerSelect.java” which is inside classes directory and Model java file “BeerExpertModel.java” which is inside model directory.
SERVLETPATH is the place where your “servlet-api.jar” exists, This API is being used by our Servlet Java file” BeerSelect.java”.
CLASSPATH is the place where Java Run Time Environment “rt.jar” exists. This API is used by JVM to run or execute your Java Programs.
”setenv.bat”
--------------------------------------------------------------
SET JAVA_HOME=C:\Program Files\Java\jdk1.6.0_03
SET PATH=%PATH%;%JAVA_HOME%\bin;
SET SERVLETPATH=C:\Program Files\Apache Software Foundation\Tomcat 7.0\lib\servlet-api.jar
SET CLASSPATH=.;%JAVA_HOME%\jre\lib\rt.jar;%SERVLETPATH%;
javac BeerSelect.java
javac "C:\Program Files\Apache Software Foundation\Tomcat 7.0\webapps\beer\WEB-INF\classes\model\BeerExpertModel.java"
pause
--------------------------------------------------------------
Step 16. < EXECUTING THE BATCH FILE >
Double Click the Batch file ”setenv.bat” to execute.
It will compile the Servlet Java file” BeerSelect.java” which is inside classes directory and Model java file “BeerExpertModel.java” which is inside model directory.
After compilation “BeerSelect.class” file will be generated in classes directory as below :
and
“BeerExpertModel.class” file will be generated in model directory as below :
Step 17.
Running the Beer Advice Web Application using APACHE WEB SERVER.
a) Open the Directory C:\Program Files\Apache Software Foundation\Tomcat 7.0\bin
b)Double click the “tomcat7w.exe” icon , You may get the window below :
c) Click on Start
d) As you can see in the Service Status :
Tomcat ApacheWebServer is in Service Status as'Started'
Step 10.
Open the Browser , Type in the url below in the Address Bar and hit enter http://localhost:8080/beer/BeerForm.html
You will get the BeerForm.html page populated inside the Browser window.
f) Select any Color from the Dropdown and press Enter Button.
Beer Advice Result JSP page will be displayed on your Browser Screen.
Step 18. <B
eer Advice WEB Application Flow is as below>
http://localhost:8080/beer/BeerForm.html
a)BeerForm.html à <form method="POST" action="/beer/SelectBeer.do"> will look for the <url-pattern> in the web.xml which will be the<servlet-mapping>
<servlet-name>Beer</servlet-name> <url-pattern>/SelectBeer.do</url-pattern> </servlet-mapping> b)After fetching the <url-pattern>/SelectBeer.do</url-pattern>,
it will find the associated <servlet-name>Beer</servlet-name>in the <servlet-mapping> Here <servlet-name> is Beer, Then in the web.xml using the <servlet-mapping> <servlet-name> called Beer, It will search for the associatedServlet Class under the <servlet> tag <servlet> <servlet-name>Beer</servlet-name> <servlet-class>BeerSelect</servlet-class></servlet> The servlet-class is BeerSelect. You can see that <servlet-name>is common between <servlet> and <servlet-mapping> nodes. c)servlet-class “BeerSelect” will be executed.
BeerSelect Servlet Java will fetch the color details from the BeerForm.html and calls the [ MODEL Class BeerExpertModel method bemlist.getBrands(strColor); ] to pass the color submitted by the user from BeerForm.html page.
BeerExpertModel will return a list of brands to the BeerSelect Servlet. BeerSelect Servlet will then route or dispatches the request,response objects to VIEW BeerAdviceResult.jsp using RequestDispatcherRequestDispatcher view = request.getRequestDispatcher("BeerAdviceResult.jsp");
view.forward(request,response);
d) BeerAdviceResult.jsp will display the Bear Brands Advice
Flow Summary :
BeerForm.htmlà web.xml à BeerSelect Servlet --- BeerExpertModel à BeerAdviceResult.jsp