web.xml
[xml]
<web-app>
<servlet>
<servlet-name>DemoServletAttribute</servlet-name>
<servlet-class>servletattribute.DemoServletAttribute</servlet-class>
</servlet>
<servlet>
<servlet-name>ForwardRequest</servlet-name>
<servlet-class>servletattribute.ForwardAttributes</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DemoServletAttribute</servlet-name>
<url-pattern>/welcome</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>ForwardRequest</servlet-name>
<url-pattern>/servlet2</url-pattern>
</servlet-mapping>
</web-app>
[/xml]
Make sure that while giving name of class name also give package name, here the developer has taken two classes.
DemoServletAttribute.java
[java]
package servletattribute;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class DemoServletAttribute extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
request.setAttribute("name", "SPlesson");//Create request attribute
//Create session attribute
HttpSession sessionObject = request.getSession();//Create session object
sessionObject.setAttribute("tutorial", "yes");//create set attribute
//Create ServletContext attribute
ServletContext servletContextObject = getServletContext();//create sessionContext object
servletContextObject.setAttribute("contact to", "SPlesson@info.com");
//forward request to servlet2 program
RequestDispatcher rd = request.getRequestDispatcher("servlet2");
rd.forward(request, response);
out.flush();
out.close();
}
}
[/java]
forwardAttributes.java
[java]
package servletattribute;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class ForwardAttributes extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//general settings
response.setContentType("text/html");
PrintWriter out = response.getWriter();
//reading request attribute value
out.println("servlet2 request attribute value::"+request.getAttribute("name")+"");
//reading session attribute value
HttpSession sessionObject = request.getSession();
out.println("servlet session attribute value::"+sessionObject.getAttribute("tutorial")+"");
//reading servlet context value
ServletContext contextObject = getServletContext();
out.println("servlet session attribute value::"+contextObject.getAttribute("contact to")+"");
out.flush();
out.close();
}
}
[/java]
Here the purpose of
setContentType(“text/html”) is that, It basically tells the client what content type it is so that it knows what to do with it. Where the developer created an object to read the request object, session object, context object to read the values as follows.
[java]HttpSession sessionObject = request.getSession();
ServletContext contextObject = getServletContext();
[/java]
Output
Result will be generated as follows.
Difference Between Session Attributes And ServletContext Attributes
ServletContext
A ServletContext attribute is a protest bound into a context through
ServletContext.setAttribute() technique and which is accessible to all servlets in that unique circumstance, or to different settings by means of the
getContext() strategy. By definition a setting property exists locally in the JVM where they were characterized. In this way, these are inaccessible on disseminated applications.
Session Attributes
Session attributes are bound to a session, as an intend to give state to an arrangement of related HTTP requests. Session attributes are accessible ONLY to those servlets which join the session. These are likewise inaccessible to various JVMs in dispersed situations. Items can be advised when bound/unbound to the session actualizing the
HttpSessionBindingListener interface.