• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

getting servlet parameters in one file to reuse it

 
Ranch Hand
Posts: 851
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Can you describe your question bit more clearly , I could not get what you want.
 
Greenhorn
Posts: 27
Hibernate jQuery Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 851
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 851
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I mean like this code:




and this HTMLParameters.java may be called to many methods from different classes
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 851
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
Ranch Hand
Posts: 54
Tomcat Server Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 147
Eclipse IDE Tomcat Server Debian
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Farakh khan
Ranch Hand
Posts: 851
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 851
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks to all from the bottom of my heart to helping me out
 
reply
    Bookmark Topic Watch Topic
  • New Topic