Win a copy of Cloud Native PatternsE this week in the Cloud forum
or Natural Language Processing in the AI/ML forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

context getAttribute code doubt

 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
S Sravs
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but i want to see the output as

Retriving Attributes
firstAttribute secondAttribute
[ April 14, 2008: Message edited by: Vijaya Sravanthi ]
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The container also uses the servlet context to set some attributes. Why is it bothering you ?
 
S Sravs
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks
actually i want to know the reason why is it not giving the output which i expected
 
Ranch Hand
Posts: 163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this
 
S Sravs
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Naresh i tried but the problem remains the same
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
use the while loop as follows:



that way you wont see the container added attributes.
 
S Sravs
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!