Tuesday, 5 February 2013

SERVLETCONFIG, SERVLETCONTEXT, URL REDIRECTION | J2EE Tutorial pdf

SERVLETCONFIG, SERVLETCONTEXT, URL REDIRECTION

THE SERVLETCONTEXT
A ServletContext is an object that is defined in the javax.servlet package. It defines a set of methods that are used by server-side components of a Web application to communicate with the servlet container.
The ServletContext is most frequently used as a storage area for objects that need to be available to all of the server-side components in a Web application.
We can think of the ServletContext as a shared memory segment for Web applications.
When an object is placed in the ServletContext, it exists for the life of a Web application, unless it is explicitly removed or replaced. Four methods defined by the ServletContext are leveraged to provide this shared memory functionality. 
These methods are
setAttribute() -Binds an object to a given name, and stores the object in the current ServletContext. If the name specified is already in use, this method will remove the old object binding and bind the name to the new object.
getAttribute()- Returns the object referenced by the given name, or returns null if there is no attribute bind to the given key.
removeAttribute()- Removes the attribute with the given name from the ServletContext.
getAttributeNames()-Returns an enumeration of strings containing the object names stored in the current ServletContext.
In next example ContextDemo1 servlet set a message in ServletContext object:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ContextDemo1 extends HttpServlet {
public void doGet(HttpServletRequest req,HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/html");
PrintWriter out=res.getWriter();
ServletContext context=getServletContext();
context.setAttribute("msg","Hello, This is first servlet!");
out.println("Message has sent");
}
}

Output:

In next example ContextDemo2 servlet read a message from ServletContext object:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class ContextDemo2 extends HttpServlet {
public void doGet(HttpServletRequest req,HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/html");
PrintWriter out=res.getWriter();
ServletContext context=getServletContext();
String msg=(String)context.getAttribute("msg");
out.println(msg);
}
}

Output:
                                        Hello This is First Servlet
The ServletConfig
A servlet configuration object used by a servlet container used to pass information to a servlet
during initialization. The configuration information contains initialization parameters, a name of
the servlet. The initialization parameters and the name of a servlet can be specified in the
deployment descriptor.
 For example:
<web-app>
<servlet>
<servlet-name>configdemo</servlet-name>
<servlet-class>configdemo</servlet-class>
<init-param>
<param-name>email</param-name>
<param-value>admin@admin.com</param-value>
</init-param>
</servlet>
</web-app>

Methods of ServletConfig are:-
String getInitParameter(String name)
Returns a String containing the value of the named initialization parameter, or null if the parameter does not exist.
Enumeration getInitParameterNames()
Returns the names of the servlet's initialization parameters as an Enumeration of String objects, or an empty Enumeration if the servlet has no initialization parameters.
ServletContext getServletContext()
Returns a reference to the ServletContext in which the caller is executing.
String getServletName()
Returns the name of this servlet instance.
In next example ServletConfig object retrives data from web.xml file:
ConfigDemo.java
import java.io.*;
import javax.servlet.*;
public class ConfigDemo extends GenericServlet {
public void service(ServletRequest req,ServletResponse res)
throws IOException,ServletException
{
res.setContentType("text/html");
PrintWriter out=res.getWriter();
ServletConfig sc=getServletConfig();
String s=sc.getInitParameter("email");
out.println(s);
}
}

Output:
admin@admin.com

SERVLET COLLABORATION

There are two ways to forward control from one page to another.
1.Using RequestDispatcher
Defines an object that receives requests from the client and sends them to any resource (such as a servlet, HTML file, or JSP file) on the server. The servlet container creates the RequestDispatcher object, which is used as a wrapper around a server resource located at a particular path or given by a particular name.
Method Summary:
void forward(ServletRequest request, ServletResponse response)
Forwards a request from a servlet to another resource (servlet, JSP file, or HTML file) on the server.
void include(ServletRequest request, ServletResponse response)
Includes the content of a resource (servlet, JSP page, HTML file) in the response.
In next example Dispacher1 servlet forward request to Dispacher2 servlet--
Dispacher1.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Dispacher1 extends HttpServlet {
public void doGet(HttpServletRequest req,HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/html");
PrintWriter out=res.getWriter();
RequestDispatcher rd=req.getRequestDispatcher("Dispacher2");
rd.include(req,res);
}
}

Dispacher2.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Dispacher2 extends HttpServlet {
public void doGet(HttpServletRequest req,HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/html");
PrintWriter out=res.getWriter();
out.println(“I am output of Dispatcher2.java”);
}
}

Output:
I am output of Dispatcher2.java
2.Using sendRedirect()
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class RedirectDemo1 extends HttpServlet {
public void doGet(HttpServletRequest req,HttpServletResponse res)
throws ServletException, IOException {
res.sendRedirect("http://localhost:7001/servletdemo/
RedirectDemo2");
}
}

No comments: