• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

HashTable in Servlet!!

 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys!!
Hi!!
Again I want U'r Help.Now I am doing chat.In this,From one servlet I'm getting all the usernames and put it in the Hashtables.How can retrive the names from the Hashtable from another servlet.
Help needed.Thankx in Advance,

Regards,
chandran
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I understand the question correctly, you can either store the data on the users session, or you create a class to cache the data.
If you store it on the session then each person can end up having a copy of this data and it quickly becomes a drag on the system.
If everyone can use the same data you can store it in a static reference in a class (a singleton class) and make sure it's thread safe. This is my preference but it depends on how you are going to use the data.
Dave.
 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
This is one solution that I have tried and it works fine ...
*************
1 servlet
*************
import java.util.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class servlet1 extends HttpServlet
{
public static Hashtable table=new Hashtable();
public void service(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
{
try
{
table.put("sandy","sandy");
PrintWriter pw=res.getWriter();
pw.println("I have dumped the data");
}catch(Exception e)
{
System.out.println("Exception "+e);
}
}
}
***************
2 Servlet
***************
import java.util.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class servlet2 extends HttpServlet
{
public void service(HttpServletRequest req,HttpServletResponse res)
{
try
{
PrintWriter pw=res.getWriter();
ServletContext context=getServletContext();
servlet1 s=(servlet1)context.getServlet("servlet1");
String temp=(String)s.table.get("sandy");
if(temp==null)
{
pw.println("sorry I could not do that ");
}
else
{
pw.println("yes I got the value "+temp);
}
pw.close();
}
catch(Exception e)
{
System.out.println("Exception "+e);
}
}
}

------------------
Sandeep Jain
 
reply
    Bookmark Topic Watch Topic
  • New Topic