0

I have made two programs in servlet....one for setting attribute and another for getting that attribute....


Here are those programs...

/* For Setting Attribute */

import javax.servlet.*;
import java.io.*;
public class SettingCntx extends GenericServlet
{
ServletContext ctx;
public void init(ServletConfig cfig)
{
/*Obtain the ServletContext object*/
ctx = cfig.getServletContext();
}
public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException
{
/*Set the context attribute*/
ctx.setAttribute("NEW_URL","jdbc:odbc:EmployeesDB");
/*Obtain the PrintWriter object*/
PrintWriter pw = response.getWriter();
/*Send response to indicate that the URL attribute as been
set*/
response.setContentType("text/html");
pw.println("<B>The JDBC URL has been set as a context attribute</B>");
}
}

---------------------------------------------------

/* For getting that attribute */

import javax.servlet.*;
import java.io.*;
public class RetrievingCntx extends GenericServlet
{
ServletContext ctx;
String new_url;
public void init(ServletConfig cfig)
{
/*Obtain the ServletContext object*/
ctx = cfig.getServletContext();
}
public void service (ServletRequest request, ServletResponse response) throws ServletException, IOException
{
/*Retrieve the URL attribute*/
new_url = (String)ctx.getAttribute("NEW_URL");
System.out.println("new url is"+new_url);
/*Obtain a PrintWriter object*/
PrintWriter pw = response.getWriter();
/*Send response to display the value of the URL attribute*/
response.setContentType("text/html");
pw.println("<B>The URL value is </B>: "+ new_url + "<BR>");
}
}

---------------------


But the output is like this respectively....

The JDBC URL has been set as a context attribute


and for second servlet output is like this..

The URL value is null


now i m not getting why its NULL....the ans must be like

The URL value is jdbc:odbc:EmployeeDB

Please help me for it....its urgent......

Edited by peter_budo: Keep It Clear - Do wrap your programming code blocks within [code] ... [/code] tags

2
Contributors
2
Replies
4
Views
6 Years
Discussion Span
Last Post by sneha mehta
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.