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.

...
import javax.servlet.*;
import javax.servlet.http.*;
.....
 
 
HttpServletRequest request = null;
String cityEntry = request.getParameter("city");
try{
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from cityTable where city = 'Boston'");
 
			
while (rs.next())
{
    String city = rs.getString("city");	
    if(cityEntry.equals(city))
    {
       //do something...
       ...............			
}

no, you have initialised the HttpServletRequest as null, how would it contain a parameter?

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.

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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.