Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

HashTable in Servlet!!

 
Raajesh Chandran
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
  • 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
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • 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.
 
Sandeep Jain
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic