Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

getting servlet parameters in one file to reuse it

 
Farakh khan
Ranch Hand
Posts: 833
  • Mark post as helpful
  • send pies
  • 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
 
Miku Ranjan
Ranch Hand
Posts: 98
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Can you describe your question bit more clearly , I could not get what you want.
 
hemantha kumar k
Greenhorn
Posts: 27
Hibernate Java jQuery
  • Mark post as helpful
  • send pies
  • 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: 833
  • Mark post as helpful
  • send pies
  • 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
 
Matthew Freake
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • 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: 833
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I mean like this code:




and this HTMLParameters.java may be called to many methods from different classes
 
Steven Walkling
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • 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: 833
  • Mark post as helpful
  • send pies
  • 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

 
Louis Bros
Ranch Hand
Posts: 54
Java Tomcat Server Ubuntu
  • Mark post as helpful
  • send pies
  • 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.
 
Pete Nelson
Ranch Hand
Posts: 147
Debian Eclipse IDE Tomcat Server
  • Mark post as helpful
  • send pies
  • 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: 833
  • Mark post as helpful
  • send pies
  • 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: 833
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to all from the bottom of my heart to helping me out
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic