GeeCON Prague 2014*
The moose likes Servlets and the fly likes HashTable in Servlet!! Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "HashTable in Servlet!!" Watch "HashTable in Servlet!!" New topic
Author

HashTable in Servlet!!

Raajesh Chandran
Ranch Hand

Joined: May 04, 2001
Posts: 103
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


chandran..
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

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.
Sandeep Jain
Ranch Hand

Joined: Oct 25, 2000
Posts: 124
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


Try and Try Till u succeed<br /> <br />Sandeep Jain
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: HashTable in Servlet!!