Servlets can use four types of scope for the
variables
- Local scope
- Page scope
- Session scope
- Application or server scope.
Local scope
If a variable gets declared
inside any method of the servlet or any block then it becomes local scope.
Page scope
If a variable can be available to all parts of the servlets
and all users share one instance of the variable, then it becomes page scope. An
instance or class variable of the servlet can be called as page scope variable.
Such types of variables can be used to share common information about all users
in all servlets.
Example
user.java
-
- import javax.servlet.*;
- import java.io.*;
- import javax.servlet.http.*;
- public class user extends HttpServlet {
- int x = 0;
- public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
- PrintWriter out = res.getWriter();
- out.println("<Html>");
- out.println("<Body bgcolor='CYAN'>");
- out.println("<H1>" + x + "</h1>");
- x++;
- out.println("</Body>");
- out.println("</Html>");
- }
- }
Web.xml settings
- <?xml version="1.0" encoding="ISO-8859-1"?>
- <!--
- Licensed to the Apache Software Foundation (ASF) under one or more
- contributor license agreements. See the NOTICE file distributed with
- this work for additional information regarding copyright ownership.
- The ASF licenses this file to You under the Apache License, Version 2.0
- (the "License"); you may not use this file except in compliance with
- the License. You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- -->
- <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_2_5.xsd"
- version="2.5">
-
- <servlet>
- <servlet-name>user</servlet-name>
- <servlet-class>user</servlet-class>
- </servlet>
-
- <servlet-mapping>
- <servlet-name>user</servlet-name>
- <url-pattern>/user</url-pattern>
- </servlet-mapping>
-
- </web-app>
Compilation
javac -cp servlet-api.jar user.java (for tomcat 6.0)
Output
Run the tomcat then write the below line in the URL
http://localhost:8081/serv/user
Here serv is the Context path, which we mentioned in the server.xml file, which
is present in (E:\Program Files\Apache Software Foundation\Tomcat 6.0\conf)
directory.
Note: - When we refresh the page the number goes on increasing. Also when
we open a new browser and write the above URL then also number goes on increasing.
So, here we note that for every visit of new user the number goes on increasing
until the server gets restarted.
Session scope
If a variable can be available to all servlets and for
each user it becomes individual then it is called as user or session scope. Such
types of variable can be used to store individual information for each user and
for multiple servlets. It can be created by using cookie or session.
Application or server scope
If a variable can be available to all
servlets and all users share a common instance of the variable then it is called
as application or server scope. Such type of variables can be used to store
common information about all servlet and all users.
Creation and reading process of application scope variables
- Create the object of
javax.servlet.servletContext by using getServletContext() method of generic
servlet.The object of servlet context represent all the servlets present in
the servlet container.
- Use setAttribute() of servlet context to
create application scope variable. It accepts two parameters as the name of
the variable and value of the variable.
- Use getAttribute() of servlet context to
find value of application scope variable. If the specified variable does not
exits then it returns null.
Example
welcome.java
- import java.io.*;
- import javax.servlet.*;
- import javax.servlet.http.*;
- public class welcome extends HttpServlet {
- public void doGet(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
-
- ServletContext sc = getServletContext();
- Integer x = (Integer) sc.getAttribute("hit");
- PrintWriter out = res.getWriter();
- out.println("<html><body>");
- if (x == null)
- x = 1;
- else
- x++;
- sc.setAttribute("hit", x);
- out.println("<h1>Welcome </h1>");
- out.println("<h3><a href='./user1'>Hit count</a></h3>");
- out.println("</body></html>");
- }
- }
user1.java
- import java.io.*;
- import javax.servlet.*;
- import javax.servlet.http.*;
- public class user1 extends HttpServlet {
- public void doGet(HttpServletRequest req, HttpServletResponse res)
- throws ServletException, IOException {
- ServletContext sc = getServletContext();
- Integer x = (Integer) sc.getAttribute("hit");
- PrintWriter out = res.getWriter();
- out.println("<html><body>");
- out.println("<H1>Users visited " + x + "</h1>");
- out.println("</body></html>");
- }
- }
Web.xml settings
- <?xml version="1.0" encoding="ISO-8859-1"?>
- <!--
- Licensed to the Apache Software Foundation (ASF) under one or more
- contributor license agreements. See the NOTICE file distributed with
- this work for additional information regarding copyright ownership.
- The ASF licenses this file to You under the Apache License, Version 2.0
- (the "License"); you may not use this file except in compliance with
- the License. You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- -->
- <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_2_5.xsd"
- version="2.5">
-
- <servlet>
- <servlet-name>welcome</servlet-name>
- <servlet-class>welcome</servlet-class>
- </servlet>
-
- <servlet>
- <servlet-name>user1</servlet-name>
- <servlet-class>user1</servlet-class>
- </servlet>
-
- <servlet-mapping>
- <servlet-name>welcome</servlet-name>
- <url-pattern>/welcome</url-pattern>
- </servlet-mapping>
-
- <servlet-mapping>
- <servlet-name>user1</servlet-name>
- <url-pattern>/user1</url-pattern>
- </servlet-mapping>
-
- </web-app>
Compilation
javac -cp servlet-api.jar welcome.java (for tomcat 6.0)
javac -cp servlet-api.jar user1.java (for tomcat 6.0)
Output
Run the tomcat then write the below line in the URL
Here serv is the Context path, which we mentioned in the server.xml file, which
is present in (E:\Program Files\Apache Software Foundation\Tomcat 6.0\conf)
directory.
http://localhost:8081/serv/welcome
Note: - In welcome. Java file the value is stored in hit variable and it is
retrieved in user1.java file. So, here we note that for every visit of new user
the number goes on increasing until the server gets restarted.
Conclusion
In this article, we learned about Scope of variables in Java Servlets, various types of scopes and their java implementation