aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes context  getAttribute  code doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "context  getAttribute  code doubt" Watch "context  getAttribute  code doubt" New topic
Author

context getAttribute code doubt

S Sravs
Ranch Hand

Joined: Feb 25, 2008
Posts: 85
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class ContextServlet extends HttpServlet
{
private ServletContext context;

public void init()
{
context=getServletContext();
}

public void doGet(HttpServletRequest req,HttpServletResponse res) throws IOException,ServletException
{
processRequest(req,res);
}
public void doPost(HttpServletRequest req,HttpServletResponse res) throws IOException,ServletException
{
processRequest(req,res);
}
public void processRequest(HttpServletRequest req,HttpServletResponse res) throws IOException,ServletException
{
PrintWriter out=res.getWriter();
res.setContentType("text/html");
out.println("<html>");
out.println("<body>");

context.setAttribute("first","firstAttribute");
context.setAttribute("second","secondAttribute");


out.println("<br>Retriving Attributes<br>");
Enumeration enm=context.getAttributeNames();
while(enm.hasMoreElements())
{
String attr=(String)enm.nextElement();
out.println(context.getAttribute(attr));
}

out.println("</body>");
out.println("</html>");

}


}




<web-app>
<servlet>
<servlet-name>conx</servlet-name>
<servlet-class>ContextServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>conx</servlet-name>
<url-pattern>/context</url-pattern>
</servlet-mapping>
</web-app>


My output is


Retriving Attributes
secondAttribute [Ljava.lang.String;@51064e C:\Program Files\Apache Software Foundation\Tomcat 6.0\work\Catalina\localhost\serv /C:/Program%20Files/Apache%20Software%20Foundation/Tomcat%206.0/webapps/serv/WEB-INF/classes/;/C:/Program Files/Apache Software Foundation/Tomcat 6.0/lib/;/C:/Program Files/Apache Software Foundation/Tomcat 6.0/lib/annotations-api.jar;/C:/Program Files/Apache Software Foundation/Tomcat 6.0/lib/catalina-ant.jar;/C:/Program Files/Apache Software Foundation/Tomcat 6.0/lib/catalina-ha.jar;/C:/Program Files/Apache Software Foundation/Tomcat 6.0/lib/catalina-tribes.jar;/C:/Program Files/Apache Software Foundation/Tomcat 6.0/lib/catalina.jar;/C:/Program Files/Apache Software Foundation/Tomcat 6.0/lib/el-api.jar;/C:/Program Files/Apache Software Foundation/Tomcat 6.0/lib/jasper-el.jar;/C:/Program Files/Apache Software Foundation/Tomcat 6.0/lib/jasper-jdt.jar;/C:/Program Files/Apache Software Foundation/Tomcat 6.0/lib/jasper.jar;/C:/Program Files/Apache Software Foundation/Tomcat 6.0/lib/jsp-api.jar;/C:/Program Files/Apache Software Foundation/Tomcat 6.0/lib/servlet-api.jar;/C:/Program Files/Apache Software Foundation/Tomcat 6.0/lib/tomcat-coyote.jar;/C:/Program Files/Apache Software Foundation/Tomcat 6.0/lib/tomcat-dbcp.jar;/C:/Program Files/Apache Software Foundation/Tomcat 6.0/lib/tomcat-i18n-es.jar;/C:/Program Files/Apache Software Foundation/Tomcat 6.0/lib/tomcat-i18n-fr.jar;/C:/Program Files/Apache Software Foundation/Tomcat 6.0/lib/tomcat-i18n-ja.jar;/C:/Program%20Files/Apache%20Software%20Foundation/Tomcat%206.0/bin/bootstrap.jar;/C:/Program%20Files/Java/jre1.6.0_05/lib/ext/dnsns.jar;/C:/Program%20Files/Java/jre1.6.0_05/lib/ext/sunjce_provider.jar;/C:/Program%20Files/Java/jre1.6.0_05/lib/ext/sunmscapi.jar;/C:/Program%20Files/Java/jre1.6.0_05/lib/ext/sunpkcs11.jar org.apache.naming.resources.ProxyDirContext@174f02c firstAttribute org.apache.catalina.util.DefaultAnnotationProcessor@44006e




can any one help me why am i unable to retrive getAttributes of context


[ April 14, 2008: Message edited by: Vijaya Sravanthi ]
[ April 14, 2008: Message edited by: Vijaya Sravanthi ]
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

Your attributes are here, look carefully :

secondAttribute [Ljava.lang.String;@51064e C:\Program Files\Apache Software Foundation\Tomcat 6.0\work\Catalina\localhost\serv /C:/Program%20Files/Apache%20Software%20Foundation/Tomcat%206.0/webapps/serv/WEB-INF/classes/;/C:/Program Files/Apache Software Foundation/Tomcat 6.0/lib/;/C:/Program Files/Apache Software Foundation/Tomcat 6.0/lib/annotations-api.jar;/C:/Program Files/Apache Software Foundation/Tomcat 6.0/lib/catalina-ant.jar;/C:/Program Files/Apache Software Foundation/Tomcat 6.0/lib/catalina-ha.jar;/C:/Program Files/Apache Software Foundation/Tomcat 6.0/lib/catalina-tribes.jar;/C:/Program Files/Apache Software Foundation/Tomcat 6.0/lib/catalina.jar;/C:/Program Files/Apache Software Foundation/Tomcat 6.0/lib/el-api.jar;/C:/Program Files/Apache Software Foundation/Tomcat 6.0/lib/jasper-el.jar;/C:/Program Files/Apache Software Foundation/Tomcat 6.0/lib/jasper-jdt.jar;/C:/Program Files/Apache Software Foundation/Tomcat 6.0/lib/jasper.jar;/C:/Program Files/Apache Software Foundation/Tomcat 6.0/lib/jsp-api.jar;/C:/Program Files/Apache Software Foundation/Tomcat 6.0/lib/servlet-api.jar;/C:/Program Files/Apache Software Foundation/Tomcat 6.0/lib/tomcat-coyote.jar;/C:/Program Files/Apache Software Foundation/Tomcat 6.0/lib/tomcat-dbcp.jar;/C:/Program Files/Apache Software Foundation/Tomcat 6.0/lib/tomcat-i18n-es.jar;/C:/Program Files/Apache Software Foundation/Tomcat 6.0/lib/tomcat-i18n-fr.jar;/C:/Program Files/Apache Software Foundation/Tomcat 6.0/lib/tomcat-i18n-ja.jar;/C:/Program%20Files/Apache%20Software%20Foundation/Tomcat%206.0/bin/bootstrap.jar;/C:/Program%20Files/Java/jre1.6.0_05/lib/ext/dnsns.jar;/C:/Program%20Files/Java/jre1.6.0_05/lib/ext/sunjce_provider.jar;/C:/Program%20Files/Java/jre1.6.0_05/lib/ext/sunmscapi.jar;/C:/Program%20Files/Java/jre1.6.0_05/lib/ext/sunpkcs11.jar org.apache.naming.resources.ProxyDirContext@174f02c firstAttribute org.apache.catalina.util.DefaultAnnotationProcessor@44006e

It would be easier to read if you'd use something like :



(next time, please use the to enclose your source. It will make it easier to read)


[My Blog]
All roads lead to JavaRanch
S Sravs
Ranch Hand

Joined: Feb 25, 2008
Posts: 85
but i want to see the output as

Retriving Attributes
firstAttribute secondAttribute
[ April 14, 2008: Message edited by: Vijaya Sravanthi ]
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

The container also uses the servlet context to set some attributes. Why is it bothering you ?
S Sravs
Ranch Hand

Joined: Feb 25, 2008
Posts: 85
thanks
actually i want to know the reason why is it not giving the output which i expected
Naresh Gunda
Ranch Hand

Joined: Oct 15, 2005
Posts: 163
Try this
S Sravs
Ranch Hand

Joined: Feb 25, 2008
Posts: 85
Naresh i tried but the problem remains the same
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14688
    
  16

but the problem remains the same

What problem ? Do you still have a problem ? What Naresh does is just output a beautiful table The servlet context is still the same.
Ash More'
Ranch Hand

Joined: Apr 20, 2008
Posts: 47
use the while loop as follows:



that way you wont see the container added attributes.
S Sravs
Ranch Hand

Joined: Feb 25, 2008
Posts: 85
Thanks actually i know this way,
i am now sure that we cannot directly get attrubutes of context using enum without mentioning the attribute name in loop or conditonal statement
 
 
subject: context getAttribute code doubt