JAVA MEANS DURGASOFT
nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86, 80 96
96 96 96, 9246212143 | www.durgasoft.com Page 1
JAVA MEANS DURGASOFT
ServletContext
Q: What are the differences between ServletConfig and ServletContext?
1. ServletConfig is an object, it will manage all the configuration details of a particular
servlet, where the configuration details include logical name of the servlet, initialization
parameters and so on.
ServletContext is an object, it will manage all the context details of a particular web
application, where the context details include logical name of web application and context
parameters and so on.
2. ServletConfig is an object, it will provide the complete view of a particular servlet.
ServletContext is an object, it will provide the complete view of particular web
application.
3. ServletConfig object will be prepared by the container immediately after servlet
instantiation and just before calling init(_) method in servlet initialization.
ServletContext object will be prepared by the container the moment when we start the
server i.e. the time when we deploy the web application.
4. ServletConfig object will be destroyed by the container just before servlet deinstaniation.
ServletContext object will be destroyed by the container when we shutdown the server
i.e. the time when we undeploy the web application.
5. Due to the above reasons, the life of ServletConfig object is almost all the life of the
respective servlet object.
The life of ServletContext object is almost all the life of the respective web application.
6. If we declare any data in ServletConfig object then that data will be shared upto
respective servlet.
If we declare any data inServletContext object then that data will be shared to all the no.
of resources which are available in the present web application.
7. Due to the above reason, ServletConfig object will provide less sharability where as
ServletContext object will provide more sharability.
8. In web applications, container will prepare ServletConfig object when it receives the
request from client only except in load-on-startup case.
In web applications, container will prepare ServletContext object irrespective of getting
request from client.
nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86, 80 96
96 96 96, 9246212143 | www.durgasoft.com Page 2
JAVA MEANS DURGASOFT
nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86, 80 96
96 96 96, 9246212143 | www.durgasoft.com Page 3
JAVA MEANS DURGASOFT
9. In web applications, ServletConfig object will allow only parameters data but
ServletContext object will allow both parameters and attributes data.
10. Due to the above reason, ServletConfig object will allow only Static Inclusion of data
where as ServletContext object will allow both Static Inclusion and Dynamic Inclusion of
data.
To get the ServletContext object we have to use the following method from
ServletConfig.
public ServletContext getServletContext();
Ex: ServletContext context=config.getServletContext();
Note: In servlet3.0 version, it is possible to get ServletContext object even from
ServletRequest.
Ex: ServletContext context=req.getServletContext();
If we want to get the logical name of the web application from ServletContext object first
of all we have to declare it in web.xml file.
To declare a logical name in web.xml file we have to use the following xml tag.
<web-app>
-------------
<display-name>logical_name</display-name>
-------------
</web-app>
To get the logical name of web application from ServletContext object we will use the
following method.
Public String getServletContextName()
Ex: String lname=context.getServletContextName();
If we want to provide context parameters on ServletContext object first we have to
declare them in web.xml file.
To declare a context parameter in web.xml file we have to use the following xml files.
<web-app>
------------
<context-param>
nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86, 80 96
96 96 96, 9246212143 | www.durgasoft.com Page 4
JAVA MEANS DURGASOFT
nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86, 80 96
96 96 96, 9246212143 | www.durgasoft.com Page 5
JAVA MEANS DURGASOFT
<param-name>name</param-name>
<param-value>value</param-value>
</context -param>
-------------
</web-app>
When we start the server or at the time of application deployment the main job of the
container is to recognize the web application and to prepare ServletContext object.
At the time of recognizing the application container will recognize web.xml file then
perform web.xml file loading, parsing and reading the content.
While reading the content container will read all its context parameters and store them in
ServletContext object at the time of creation.
To get the particular context parameter value from ServletContext object we will use the
following method.
public String getInitParameter(String name)
Ex: String value=context.getInitParameter(“name”);
To get all the context parameter names from ServletContext object we will use the
following method.
public Enumeration getInitParameterNames()
Ex: Enumeration e=context.getInitParameterNames();
In web application, ServletContext object is able to allow attributes.
To set an attribute on ServletContext object we will use the following method.
public void setAttribute(String name, Object value)
Ex: context.setAttribute(“location”, “Hyd”);
To get an attribute from ServletContext object we will use the following method.
public Object getAttribute(String name)
Ex: String location=(String)context.getAttribute(“location”);
To remove an attribute from ServletContext object we will use the following method.
public void removeAttribute(String name)
Ex: context.removeAttribute(“location”);
nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86, 80 96
96 96 96, 9246212143 | www.durgasoft.com Page 6
JAVA MEANS DURGASOFT
To get all the names of attributes from ServletContext object we will use the following
method.
public Enumeration getAttributeNames()
Ex: Enumeration e=context.getAttributeNames();
Q: What is ForeignContext?
Ans: ForeignContext is a ServletContext object of another web application being executed
in the same server.
To get ForeignContext object we have to use the following method.
public ServletContext getContext(String path)
--------------Application by using ServletContext---------------
contextapp:-
web.xml:
<web-app>
<display-name>Context Application</display-name>
<context-param>
<param-name>a</param-name>
<param-value>apple</param-value>
</context-param>
<context-param>
<param-name>b</param-name>
<param-value>bombay</param-value>
</context-param>
<servlet>
<servlet-name>MyServlet</servlet-name>
<servlet-class>MyServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyServlet</servlet-name>
<url-pattern>/context</url-pattern>
</servlet-mapping>
</web-app>
MyServlet.java:
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86, 80 96
96 96 96, 9246212143 | www.durgasoft.com Page 7
JAVA MEANS DURGASOFT
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
publicclass MyServlet extends HttpServlet {
protectedvoid doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out=response.getWriter();
ServletContext context=getServletConfig().getServletContext();
String logicalName=context.getServletContextName();
String a=context.getInitParameter("a");
String b=context.getInitParameter("b");
Enumeration e=context.getInitParameterNames();
context.setAttribute("c", "cat");
context.setAttribute("d", "dog");
out.println("<html><body><h1><br>");
out.println("Logical Name : "+logicalName);
out.println("<br>");
out.println("a for ... "+a);
out.println("<br>");
out.println("b for ... "+b);
out.println("<br>");
while(e.hasMoreElements()){
out.println(e.nextElement()+"<br>");
}
out.println("c for ... "+context.getAttribute("c"));
out.println("<br>");
out.println("d for ... "+context.getAttribute("d")+"<br>");
e=context.getAttributeNames();
while(e.hasMoreElements()){
out.println(e.nextElement()+"<br>");
}
out.println("</h1></body></html>");
}
}
Q: Consider the following web application
D:\apps
contextapp2
WEB-INF
web.xml
nd classes
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86, 80 96
96 96 96, 9246212143 | www.durgasoft.com Page 8
JAVA MEANS DURGASOFT
ServletEx1.class
ServletEx2.class
web.xml:
A short representation of web.xml
a aaa
b bbb Here a,b are context parameters
ServletEx1
c ccc
d ddd Here c,d are initialization parameters
/first
ServletEx2
e eee
f fff Here e,f are initialization parameters
/second
ServletEx1.java:
nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86, 80 96
96 96 96, 9246212143 | www.durgasoft.com Page 9
JAVA MEANS DURGASOFT
// import statements
public class ServletEx1 extends HttpServlet{
public void deGet(HSR req, HSR res)throws SE, IOE {
ServletConfig config=getServletConfig();
ServletContext context=req.getServletContext();
out.println(“a-”+context.getInitParameter(“a”));
out.println(“b-”+context.getInitParameter(“b”));
out.println(“c-”+config.getInitParameter(“c”));
out.println(“d-”+config.getInitParameter(“d”));
out.println(“e-”+config.getInitParameter(“e”));
out.println(“f-”+config.getInitParameter(“f”));
ServletEx2.java:
// import statements
public class ServletEx2 extends HttpServlet{
public void deGet(HSR req, HSR res)throws SE, IOE{
ServletConfig config=getServletConfig();
ServletContext context=req.getServletContext();
out.println(“a-”+context.getInitParameter(“a”));
out.println(“b-”+context.getInitParameter(“b”));
out.println(“c-”+config.getInitParameter(“c”));
out.println(“d-”+config.getInitParameter(“d”));
out.println(“e-”+config.getInitParameter(“e”));
out.println(“f-”+config.getInitParameter(“f”));
}
nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86, 80 96
96 96 96, 9246212143 | www.durgasoft.com Page 10
JAVA MEANS DURGASOFT
nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86, 80 96
96 96 96, 9246212143 | www.durgasoft.com Page 11
JAVA MEANS DURGASOFT
Q1: http://localhost:8080/contextapp2/first?
Ans: a-aaa c-ccc e-null
b-bbb d-ddd f-null
Q2: http://localhost:8080/contextapp2/second?
Ans: a-aaa c-null e-eee
b-bbb d- null f-fff
In above web application, we can differentiate the scope of ServletConfig and
ServletContext objects i.e. the scope of ServletConfig object is upto the respective servlet
where as the scope of ServletContext object is through out the web application.
JBoss Server:
JBoss is an Application Server, it will provide almost all the middle ware services what
application servers are provided in general.
JBoss Server is compatible with jdk7 and it able to provide support for servlet3.0 version,
jsp2.1 version and so on.
JBoss server is not having its own web container, it was used Tomcat Container.
If we want to deploy and execute web applications with JBoss server we have to use the
following steps.
nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86, 80 96
96 96 96, 9246212143 | www.durgasoft.com Page 12
JAVA MEANS DURGASOFT
Step 1: Prepare web application and its war file.
D:\apps
jbossapp
WEB-INF
web.xml
classes
FirstServlet.java
FirstServlet.class
In case of JBoss7 to compile all the servlets we need to set CLASSPATH environment
variable to jboss-Servlet API_3.0_spec-1.0.0.Final.jar, which has provided by JBoss server
in the following location.
C:\jboss-as-7.1.0.Final\modules\javax\servlet\api\main\ jboss-servlet-
api_3.0_spec-1.0.0.Final.jar
D:\apps\jbossapp\WEB-INF\classes>set classpath=C:\jboss-as- 7.1.0.Final
\modules\javax\servlet\api\main\ jboss-Servlet API_3.0_spec-1.0.0.Final.jar;
D:\apps\jbossapp\WEB-INF\classes>javac *.java
To prepare war file we have to use the following command on command prompt.
D:\apps\jbossapp>jar –cvf jbossapp1.war *.*
nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86, 80 96
96 96 96, 9246212143 | www.durgasoft.com Page 13
JAVA MEANS DURGASOFT
Step 2: Start JBoss Application Server.
C:\jboss-as-7.1.0.Final\bin, where double click on standalone.bat file
Step 3: Open Administration Console.
To open Administration Console we have to use the following URL on browser.
http://localhost:8888
Actually JBoss port number is 8080, but it was changed to 8888.
If we use the above URL then we are able to get JBoss Server Welcome page, where click
on Administration Console and provide username(admin) and password(durga) in security
window.
Step 4: Deploy web application.
If we click on OK button in security window automatically JBoss Home page will between
open, where click on Manage Deployments under Deployments section.
If we do the above list of deployed applications will be displayed, where to deploy a new
web application we have to use following path.
Click on Add Content button
Browse
Select war file
Next
Save
Enable respective
Confirm
nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86, 80 96
96 96 96, 9246212143 | www.durgasoft.com Page 14
JAVA MEANS DURGASOFT
Step 5: Access the web application.
Open another window, where we have to provide the following URL.
http://localhost:8888/jbossapp1/first
To change JBoss port number we have to use the following path.
C:\jboss-as-7.1.0.Final\standalone\configuration\ standalone.xml,
where search for 8080 and replace our port number(8888).
<select-binding name=”http” port=”8888”/>
To create an account in JBoss server we have to use the following path.
C:\jboss-as-7.1.0.Final\bin, where double click on add-user.bat file.
If we do the above then a command prompt will be open, where we have to provide the
required details.
1. User type : Application User, press enter
2. Management realm : press enter
3. Username : durga, press enter
4. Password : durga, press enter
5. Re-enter password : durga, press enter
6. Is this correct yes/no ? yes press enter
nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86, 80 96
96 96 96, 9246212143 | www.durgasoft.com Page 15
JAVA MEANS DURGASOFT
nd
DURGASOFT, # 202,2 Floor,HUDA Maitrivanam,Ameerpet, Hyderabad - 500038, 040 – 64 51 27 86, 80 96
96 96 96, 9246212143 | www.durgasoft.com Page 16