Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

I can't get request.getAttribute to work in jsp

Posted on 2009-04-25
9
Medium Priority
?
2,992 Views
Last Modified: 2013-11-24
I am trying to pass a Attribute from a servlet to a JSP page and it is not working.  
Java Code:
hashMap = new HashMap();
for(int i = 0; i < categs.length; i++)
{
   hashMap.put( categs[i], i ); // adding value into HashMap
}
request.setAttribute("CATEG", hashMap);
System.out.println("HashMap contains " + hashMap.size() + " key value pair."); 
 System.out.println(hashMap);
 
In tomcat I get the contents of the Hash map iwth 9 key value pairs
 
JSP
HashMap hashMap = (HashMap) request.getAttribute("CATEG");
if (hashMap != null)
{
   if( hashMap.containsValue( new Integer(1) ) )
     {
         System.out.println("HashMap");
    }
else
{
   System.out.println("HashMap not working!");
}
I get HashMap not working.
Thanks,
Brian

Open in new window

0
Comment
Question by:Kcpl
  • 3
  • 3
  • 2
8 Comments
 
LVL 92

Expert Comment

by:objects
ID: 24234781
what exactly are you seeing in the logs?

0
 

Author Comment

by:Kcpl
ID: 24234864
I don't get any error having to do with the HashMap.  But the HashMap not working.  System.out.println("HashMap not working!");
0
 
LVL 92

Expert Comment

by:objects
ID: 24234876
How is you r java code forwarding to the jsp
can you post all your java method

0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:Kcpl
ID: 24234880
Here it is!
package pet;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
 
 
 
import java.io.*;
import java.sql.Connection;
import java.sql.SQLException;
/**
 * 
 * 
 * 
 * @version 1.0
 */
public class PetServlet extends HttpServlet
{
	 private Connection conn;
	 private DBAccess db;
	 private String [] categs;
	 public static Map<Integer, String> hashMap = null; 
	
	 private LinkedList <String>list;
 
	public void init(HttpServletRequest request, 
            HttpServletResponse response)
	  throws ServletException, IOException
	{
	
		  	}
    protected void doGet(HttpServletRequest request, 
                         HttpServletResponse response)
    throws ServletException, IOException
    {
 	   try
	    {
	       db = new DBAccess();
	    } catch (SQLException e)
	    {
	       // TODO Auto-generated catch block
	       e.printStackTrace();
	    } catch (IOException e)
	    {
	       // TODO Auto-generated catch block
	       e.printStackTrace();
	    } catch (ClassNotFoundException e)
	    {
	       // TODO Auto-generated catch block
	       e.printStackTrace();
	    }
	 //   createPanel();
	    db.connect();
	    
	    categs = db.getCateg();
	    hashMap = new HashMap <Integer, String>();
	    
	    for(int i = 0; i < categs.length; i++)
	    {
	       hashMap.put(i , categs[i]); // adding value into HashMap
	       
	    }
	    request.setAttribute("CATEG", hashMap);
	    //request.setAttribute("CATEG", "Still Trying");
	    //request.setAttribute("CATEG", categs);
	    
	    System.out.println("HashMap contains " + hashMap.size() + " key value pair."); 
	     System.out.println(hashMap);
 
        String searchStr = request.getParameter("id");
        if (searchStr != null && searchStr.length() > 0)
        {
        	int id = Integer.parseInt(searchStr);
            Pet search = db.getPet(id);
            request.setAttribute("SEARCH_RESULTS", search);
            System.out.println(search.getPetName());
        }
	    
        javax.servlet.RequestDispatcher dispatcher = request.getRequestDispatcher("/JSP-Code/Final.jsp");
        dispatcher.forward(request, response);
    }
    protected void doPost(HttpServletRequest request, 
            HttpServletResponse response)
    throws ServletException, IOException
 
    { 
    	doGet(request,response);
    }
}

Open in new window

0
 
LVL 28

Expert Comment

by:rrz
ID: 24235383
>   if( hashMap.containsValue( new Integer(1) ) )
try  
   if( hashMap.containsKey( new Integer(1) ) )    
0
 
LVL 28

Expert Comment

by:rrz
ID: 24235391
no try
 if( hashMap.containsKey(1) )
0
 
LVL 92

Accepted Solution

by:
objects earned 1000 total points
ID: 24235396
what you posted in the q, does not match whats in the code :)

   hashMap.put( categs[i], i ); // adding value into HashMap

               hashMap.put(i , categs[i]); // adding value into HashMap
0
 
LVL 28

Expert Comment

by:rrz
ID: 24235453
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Suggested Courses
Course of the Month15 days, 17 hours left to enroll

580 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question