943,712 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 4637
  • Java RSS
Jan 12th, 2009
0

Get Parameter value

Expand Post »
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.
Java Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
chicago1985 is offline Offline
36 posts
since Nov 2007
Jan 12th, 2009
0

Re: Get Parameter value

no, you have initialised the HttpServletRequest as null, how would it contain a parameter?
Reputation Points: 85
Solved Threads: 64
Practically a Master Poster
sillyboy is offline Offline
686 posts
since Mar 2007
Jan 13th, 2009
0

Re: Get Parameter value

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.
Reputation Points: 56
Solved Threads: 11
Junior Poster
PoovenM is offline Offline
147 posts
since Aug 2006
Jan 13th, 2009
0

Re: Get Parameter value

Click to Expand / Collapse  Quote originally posted by PoovenM ...
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.
Reputation Points: 10
Solved Threads: 0
Light Poster
chicago1985 is offline Offline
36 posts
since Nov 2007
Jan 13th, 2009
0

Re: Get Parameter value

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.
Reputation Points: 56
Solved Threads: 11
Junior Poster
PoovenM is offline Offline
147 posts
since Aug 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Quick Search(Finding Duplicate Records)
Next Thread in Java Forum Timeline: Action listener





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC