wood burning stoves 2.0*
The moose likes Servlets and the fly likes getting servlet parameters in one file to reuse it Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "getting servlet parameters in one file to reuse it" Watch "getting servlet parameters in one file to reuse it" New topic
Author

getting servlet parameters in one file to reuse it

Farakh khan
Ranch Hand

Joined: Mar 22, 2008
Posts: 732
Hello,

Can I get parameters from HTML file to my servlet and to reuse these parameters by inheriting this class to other servlet classes?

Best regards
Miku Ranjan
Ranch Hand

Joined: Oct 11, 2011
Posts: 98
Hi,
Can you describe your question bit more clearly , I could not get what you want.
hemantha kumar k
Greenhorn

Joined: Apr 20, 2010
Posts: 27

one way you can do this,

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: 732
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
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: 732
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
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!
Farakh khan
Ranch Hand

Joined: Mar 22, 2008
Posts: 732
Thanks for giving me your precious time



By sending form data it throws the following error:
http://127.0.0.1:7070/servlet/Hello?name=Hello+World&rollNo=12


When am trying like this:

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

[/code]

thanks again

regards

Louis Bros
Ranch Hand

Joined: Jun 03, 2011
Posts: 54

Hi, It looks like 'name' and 'rollNo' only exist within the constructor scope of ParameterKey.

Also rather than allow direct access to variables you should use getter and setter methods.


OCA7
Pete Nelson
Ranch Hand

Joined: Aug 30, 2010
Posts: 147

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: 732
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: 732
Thanks to all from the bottom of my heart to helping me out
 
 
subject: getting servlet parameters in one file to reuse it