July 27, 2012

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
//creating page variable
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("");
out.println("");
out.println(""+x+""); 
x++;
out.println("");
out.println("");
}
}



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 inceasing. 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 servlet .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("");
if(x ==null)
x=1;
else
x++;
sc.setAttribute("hit",x);
out.println("
Welcome

");
out.println("
Hit count

");
out.println("");
}
}

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("");
out.println("
Users visited "+x+"

");
out.println("");
}
}

Web.xml settings



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">


welcome
welcome


user1
user1


welcome
/welcome


user1
/user1


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.

1 comment:

I'm certainly not an expert, but I'll try my hardest to explain what I do know and research what I don't know.

My Favorite Site's List

#update below script more than 500 posts