An HttpServlet is a java class that handles HTTP specific request (like GET and POST) and has a lifecycle of initialization, service and destruction.
The ServletContext acts as a container for a given web application. It is an interface implemented by the web container to represent the configuration and state of the web application. For every web application there can be only one instance of a ServletContext. This relationship is required by the Java Servlet Specification and is enforced by all servlet containers.
We illustrate this relationship with the use of a servlet and a JSP (ContextServlet.java & Context.jsp). The servlet stores an object in the ServletContext, with the purpose of making this object available to all server-side components in the web application. It uses the ServletContext to access application wide resources and parameters from web.xml. Thus you can think of a ServletContext as a shared memory segment for web applications. When an object is created in the ServletContext, it exists for the life cycle of the Web application, unless it is explicitly removed or replaced.
In the ContextServlet example, it first gets a reference to the ServletContext, using getServletContext.
ServletContext context = getServletContext(); It then gets a reference to the object bound to the USERNAME attribute, using the getAttribute method
String userName = (String) context.getAttribute ("USERNAME");
If no object is bound to this attribute, it is created and added to the ServletContext using the setAttribute() method.
if (userName == null) {
userName = new String ("Bob Roberts");
context.setAttribute ("USERNAME", userName);
}
The value of this reference is then printed to the output stream, using an instance of PrintWriter.println() method.
out.println("<p> The Current User is : " + userName + ".</p>");
ContextServlet.java
package src;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class ContextServlet extends HttpServlet {
private static final String CONTENT_TYPE = "text/html";
public void doGet (HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost (HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ServletContext context = getServletContext();
String userName = (String) context.getAttribute ("USERNAME");
if (userName == null) {
userName = new String ("Bob Roberts");
context.setAttribute ("USERNAME", userName);
}
response.setContentType (CONTENT_TYPE);
PrintWriter out = response.getWriter();
out.println ("<html>");
out.println("<head><title>Context Servlet</title></head>");
out.println("<p> The Current User is : " + userName + ".</p>");
out.println("</html></code></pre>");
}
public void destroy(){
}
}
Context.jsp
<HTML>
<HEAD>
<TITLE>
Context
</TITLE>
</HEAD>
<BODY>
<%
String userName = (String)application.getAttribute("USERNAME");
if (userName == null) {
out.println("<b>Attribute USERNAME not found");
}
else {
out.println("<b>The current user is : " + userName + "</b>");
}
%>
</BODY>
</HTML>
web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
"http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
<web-app>
<servlet>
<servlet-name>ContextServlet</servlet-name>
<servlet-class>src.ContextServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ContextServlet</servlet-name>
<url-pattern>context</url-pattern>
</servlet-mapping>
</web-app>
We now compile and deploy this code on the tomcat web server under webapps as follows. If you are using another web server like JBOSS, the deployment folder will be different, and accordingly the deployment directory will change.We create a new folder context under the webapps folder.
| Directory | Contains | |
|---|---|---|
| /context | The root directory of the web application. All JSP and HTML files are stored here | |
| /context/WEB-INF | This directory contains all resources related to the application that are not in the document root of the application. It is where the web application deployment descriptor is located | |
| /context/WEB-INF/classes | This is where the servlet and other classes are located. | |
| /context/WEB-INF/lib | This directory contains Java Archive (JAR) files that the web application is dependent on | |
| /context/src | The directory having the java source code. |
We place the .java file in the folder /context/src folder as below
E:\install\apache-tomcat-6.0.0\apache-tomcat-6.0.0\webapps\context\src\ContextServlet.javaWe place the jsp file in the /context folder as below
E:\install\apache-tomcat-6.0.0\apache-tomcat-6.0.0\webapps\context\Context.jspWe place the web.xml file in the /context/WEB-INF folder as below
E:\install\apache-tomcat-6.0.0\apache-tomcat-6.0.0\webapps\context\WEB-INF\web.xmlWe compile the code using ant. We download a suitable version of ant and unzip it into a suitable folder path.
For the build we need to tell the ant build cmd a few important path variables
CLASSPATH
ANT_HOME
JAVA_HOME
PATH
In CLASSPATH I need to set the path for servlet-api.jar
set CLASSPATH=E:\install\apache-tomcat-6.0.0\apache-tomcat-6.0.0\lib\servlet-api.jar;
Set the path for ANT_HOME
set ANT_HOME=E:\install\ant\apache-ant-1.7.1-bin\apache-ant-1.7.1
Set the path for JAVA_HOME
set JAVA_HOME=E:\install\jdk
In the PATH variable I set the path for ANT_HOME/bin and JAVA_HOME/bin
set PATH=C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;E:\install\jdk\jdk\bin;E:\install\ant\apache-ant-1.7.1-bin\apache-ant-1.7.1\bin
I can put all the path variables in a bat file and run the bat file on cmd prompt.
E:\install\apache-tomcat-6.0.0\apache-tomcat-6.0.0\webapps\context>set_path
E:\install\apache-tomcat-6.0.0\apache-tomcat-6.0.0\webapps\context>set CLASSPATH=E:\install\apache-tomcat-6.0.0\apache-tomcat-6.0.0\lib\servlet-api.jar;
E:\install\apache-tomcat-6.0.0\apache-tomcat-6.0.0\webapps\context>set ANT_HOME=E:\install\ant\apache-ant-1.7.1-bin\apache-ant-1.7.1
E:\install\apache-tomcat-6.0.0\apache-tomcat-6.0.0\webapps\context>set JAVA_HOME=E:\install\jdk
E:\install\apache-tomcat-6.0.0\apache-tomcat-6.0.0\webapps\context>set PATH=E:\install\21cXE\dbhomeXE\bin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files\PuTTY\;E:\install\jdk\jdk\bin;C:\Program Files (x86)\MySQL\MySQL Server 6.0\bin;E:\install\jdk\bin;C:\Users\winni\AppData\Local\Microsoft\WindowsApps;E:\install\ant\apache-ant-1.7.1-bin\apache-ant-1.7.1\bin
Once the various path variables are set, I create the ant build file and run the ant command.
A sample ant build file to compile my java code is below.
Output on running
As this attribute is not yet set. To set this attribute in the ServletContext object we invoke the servlet as below
<project name="context" default="all" basedir=".">
<property name="struts.home" value="E:\winnith\java\struts\home"/>
<property name="miscjar.home" value="E:\winnith\java\project\strutsapp"/>
<property name="CATALINA_HOME" value="E:\install\apache-tomcat-6.0.0\apache-tomcat-6.0.0"/>
<property name="tomcat.home" value="${CATALINA_HOME}"/>
<property name="src.dir" value="${basedir}/src"/>
<property name="build.dir" value="${basedir}/WEB-INF/classes"/>
<path id="compile.class.path">
<fileset dir="${tomcat.home}\lib">
<include name="*.jar"/>
</fileset>
<fileset dir="${struts.home}">
<include name="*.jar"/>
</fileset>
<fileset dir="${miscjar.home}">
<include name="*.jar"/>
</fileset>
</path>
<target name="all" depends="clean,compile"/>
<target name="compile" depends="clean">
<mkdir dir="${build.dir}"/>
<javac srcdir="${src.dir}" destdir="${build.dir}"/>
</target>
<target name="clean">
<delete dir="${build.dir}"/>
</target>
</project>
I now run the ant command as below
E:\install\apache-tomcat-6.0.0\apache-tomcat-6.0.0\webapps\context>ant
Buildfile: build.xml
clean:
[delete] Deleting directory E:\install\apache-tomcat-6.0.0\apache-tomcat-6.0.0\webapps\context\WEB-INF\classes
compile:
[mkdir] Created dir: E:\install\apache-tomcat-6.0.0\apache-tomcat-6.0.0\webapps\context\WEB-INF\classes
[javac] Compiling 1 source file to E:\install\apache-tomcat-6.0.0\apache-tomcat-6.0.0\webapps\context\WEB-INF\classes
all:
BUILD SUCCESSFUL
Total time: 0 seconds
The ContextServlet.class file gets created in the path below
E:\install\apache-tomcat-6.0.0\apache-tomcat-6.0.0\webapps\context\WEB-INF\classes\src\ContextServlet.classhttp://localhost:8080/context/Context.jsp
Attribute USERNAME not found
http://localhost:8080/context/context
This browser commands looks for the url-pattern /context in the web-xml file in the /webapps/context/WEB-INF folder.
<servlet-name>ContextServlet</servlet-name>
<url-pattern>context</url-pattern>
It gets the servlet name corresponding to this url-pattern in servlet-name parameter.
It then looks for the class file for this in WEB-INF/classes folder corresponding to src.ContextServlet given in servlet-class parameter.
<servlet-name>ContextServlet</servlet-name>
<servlet-class>src.ContextServlet</servlet-class>
Once this ContextServlet class is run, we get the output as below
The Current User is : Bob Roberts.http://localhost:8080/context/Context.jsp
We will get the output as since the attribute has got set in the ServletContext of the "context" web application.
The Current User is : Bob Roberts.
References
God's Word for the day
Do not say, "What do I need,
and what further benefit can be mine?"
Do not say, "I have enough,
and what harm can come to me now?"
In the day of prosperity, adversity is forgotten,
and in the day of adversity, prosperity is not remembered.
For it is easy for the Lord on the day of death
to reward individuals according to their conduct.
An hour's misery makes one forget past delights,
and at the close of one's life, one's deeds are revealed.
Call no one happy before his death;
By how he ends a person becomes known.
Sirach 11:22-28
When Jesus saw the crowds, he went up the mountain;
And after he sat down, his disciples came to him.
Then he began to speak and taught them saying
Blessed are the poor in spirit for theirs is the kingdom of heaven
Blessed are those who mourn for they will be comforted.
Blessed are the meek, for they will inherit the earth.
Blessed are those who hunger and thirst for righteousness, for they will be filled.
Blessed are the merciful, for they will receive mercy.
Blessed are the pure in heart, for they will see God.
Blessed are the peacemakers, for they will be called children of God.
Blessed are those who are persecuted for righteousness sake, for theirs is the kingdom of God.
Blessed are you when people revile you and persecute you and utter all kinds of evil against you
falsely on my account. Rejoice and be glad, for your reward is great in heaven, for in the same way
they persecuted the prophets who were before you.
Mathew 5:1-6
No comments:
Post a Comment