Get Parameter value

Reply

Join Date: Nov 2007
Posts: 34
Reputation: chicago1985 is an unknown quantity at this point 
Solved Threads: 0
chicago1985 chicago1985 is offline Offline
Light Poster

Get Parameter value

 
0
  #1
Jan 12th, 2009
For my Java class with resultset I am trying to get the request.getParameter value.
Please advise if this is the correct way use the request object in a Java class because I cant seem to get any data from the request object.
  1. ...
  2. import javax.servlet.*;
  3. import javax.servlet.http.*;
  4. .....
  5.  
  6.  
  7. HttpServletRequest request = null;
  8. String cityEntry = request.getParameter("city");
  9. try{
  10. Statement stmt = conn.createStatement();
  11. ResultSet rs = stmt.executeQuery("select * from cityTable where city = 'Boston'");
  12.  
  13.  
  14. while (rs.next())
  15. {
  16. String city = rs.getString("city");
  17. if(cityEntry.equals(city))
  18. {
  19. //do something...
  20. ...............
  21. }
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 686
Reputation: sillyboy is on a distinguished road 
Solved Threads: 61
sillyboy's Avatar
sillyboy sillyboy is offline Offline
Practically a Master Poster

Re: Get Parameter value

 
0
  #2
Jan 12th, 2009
no, you have initialised the HttpServletRequest as null, how would it contain a parameter?
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 137
Reputation: PoovenM is on a distinguished road 
Solved Threads: 11
PoovenM PoovenM is offline Offline
Junior Poster

Re: Get Parameter value

 
0
  #3
Jan 13th, 2009
I'm not sure why you said 'no,' sillyboy? But yes, since chicago1985 instantiated the object to null, there wouldn't be anything to 'get.'

What concerns me is that he actually declared the variable request. I'm assuming the class this method belongs to, is a servlet? That is, the class extends javax.servlet.http.HttpServlet... please confirm this. If it is a servlet then you'd have access to the request object via the doGet and doPost methods. Depending on how you send information to the servlet, one or both methods are applicable.

The request object is the HttpServletRequest object found in the parameter of either mentioned method. Hope this helps.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 34
Reputation: chicago1985 is an unknown quantity at this point 
Solved Threads: 0
chicago1985 chicago1985 is offline Offline
Light Poster

Re: Get Parameter value

 
0
  #4
Jan 13th, 2009
Originally Posted by PoovenM View Post
I'm not sure why you said 'no,' sillyboy? But yes, since chicago1985 instantiated the object to null, there wouldn't be anything to 'get.'

What concerns me is that he actually declared the variable request. I'm assuming the class this method belongs to, is a servlet? That is, the class extends javax.servlet.http.HttpServlet... please confirm this. If it is a servlet then you'd have access to the request object via the doGet and doPost methods. Depending on how you send information to the servlet, one or both methods are applicable.

The request object is the HttpServletRequest object found in the parameter of either mentioned method. Hope this helps.
This is in a Java Helper class only and is not extending a servlet.
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 137
Reputation: PoovenM is on a distinguished road 
Solved Threads: 11
PoovenM PoovenM is offline Offline
Junior Poster

Re: Get Parameter value

 
0
  #5
Jan 13th, 2009
Hmmm, so you're developing a web application using JavaEE hey? So depending on whatever the user typed in web form, you want to search for a matching city... then the SQL statement you have will not work unless the user typed in 'Boston.' Do you understand why this is so?

Even thought this is a helper class, it must be spawned from the servlet because there is no other way (that I know of) to access the web application object request. If this is so, then simply pass on the request object from the servlet to the helper class (probably via the constructor). Does this make sense? It's crucial that you understand what the request object is and why it's only accessible via the doPost and doGet methods. Remember that when you submit the form (in your JSP file?), you can choose whether to submit it using the POST or GET method.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC