1. get the all parameter names from html using getParameterNames() method from the Request Object
2. use the getParmeter() to get the values of the html parameter by passing the names you got it from getParameterNames() method and store it any other class for further use.
Farakh khan
Ranch Hand
Joined: Mar 22, 2008
Posts: 672
posted
0
For example I have a servlet name HTMLParameters.java where I am getting parameters like: String name = request.getParamter("name");
then I want to use this parameter in another class e.g. DisplayRecords.java to show some records on basis of the parameters got in HTMLParameters.java??
regards
Matthew Freake
Greenhorn
Joined: Oct 26, 2011
Posts: 2
posted
0
I may be misunderstanding you, but can't you just pass the name in as a parameter to a method in your other class? So, something like:
Farakh khan
Ranch Hand
Joined: Mar 22, 2008
Posts: 672
posted
0
I mean like this code:
and this HTMLParameters.java may be called to many methods from different classes
Steven Walkling
Greenhorn
Joined: Nov 17, 2011
Posts: 3
posted
0
Hi Farakh!
It looks like you're trying to pass an instance of the Servlet class (HTMLParameters.java) into a function in another class (DisplayRecords.java) in your code. I'm not sure if that would work for what you're trying to accomplish. If I'm reading your code right, what you have won't work because "name" isn't class variable for HTMLParameters.
Since the parameters are contained on the request object, what you could do is pass the request object to the class that needs those values.
Or you create a class that can contains all of the parameter values you'll need (I'll call it ParameterKey), and pass an instance of that class to the classes that need it.
This is a newbie answer to an admittedly limited understanding of your question, but it's my best shot!
Let us know if it helps!
Update: Mixed UBB code with Not-UBB code. Embarrassing! And fixed!
C:\Program Files (x86)\Java\Tomcat7.2\webapps\ROOT\WEB-INF\classes>javac Hello.j
ava
.\DisplayRecords.java:8: cannot find symbol
symbol : variable name
location: class ParameterKey
System.out.println(paramKey.name);
^
.\DisplayRecords.java:9: cannot find symbol
symbol : variable name
location: class ParameterKey
System.out.println(paramKey.name);
^
2 errors
I am pretty sure the cause of the error is on line #5 of your servlet code:
You would need to make that a public class for the servlet container to access it.
Another thing to consider - use the ServletRequest's getParameterMap() method, and pass a Map to your constructor, rather than the full request object. Then your data class has no need to know anything of servlets.
OCPJP
In preparing for battle I have always found that plans are useless, but planning is indispensable. -- Dwight D. Eisenhower
Farakh khan
Ranch Hand
Joined: Mar 22, 2008
Posts: 672
posted
0
Pete Nelson wrote:I am pretty sure the cause of the error is on line #5 of your servlet code:
You would need to make that a public class for the servlet container to access it.
You are very well right. I changed the class to public and it start working
Thanks for helping but still this part of question is not working
Farakh khan
Ranch Hand
Joined: Mar 22, 2008
Posts: 672
posted
0
Thanks to all from the bottom of my heart to helping me out