• 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

accessing clicked hyperlink value of jsp page in servlet

 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everybody

i am having a application where on a jsp page i have provided many links.
like this



here clicking on respective link , the user goes to corresponding linked page.


REQUIREMENT

What i require is instead of this flat link , i should first go to servlet and there deciding on which link has been clicked
i should move respective page.
can any body help me please , how to achieve this and what will be the servlet code to fetch the clicked link.

This jsp has a form tag which goes to servlet /RequestHandler


 
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to create a servlet mapping in your web.xml to direct your request to the corresponding servlet.



Now, whatever points to http://localhost:8080/PROJECT_NAME/RequestHandler will be redirected to the RequestHandler class.

This class needs to extend HttpServlet and override doGet or doPost.

In the servlet, then determine which page is requested. You could append a parameter to the url like ?page=PAGE_22, pick up this parameter and take it from there.
 
Ranch Hand
Posts: 1514
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can just append the URL as a parameter when you are constructing the URL string.
 
rammie singh
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
see what i am trying is writing here.
But is is giving a NumberFormatException.



where ?RequestHandler is the servlet where the action is supposed to go.

Now In my RequestHandler Servlet i am fetching the hidden variable value , which is 60 here and depending on this
value , the corresponding switch case is executed in the servlet.

code in requestHandler


Now on clicking on any link gives error as



presize is the name of my application and the main folder.

in RequestHandler class i am fetching the link clicked as



but it is not printing any of the message and giving NumberFormatException
any suggestions??

I don't know if writing action="/RequestHandler" in form tag and in thehypelink is correct or not. please comment.
 
rammie singh
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can any body please help me quickly as i need to implement this in my appplication.
 
Rancher
Posts: 377
Android Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well first are you clicking the link or submitting the form?

If you click the link then the actionSource value will not be sent to your servlet so of course you will get a number format exception as actionSource is null.

If you click the submit button then you will get a null pointer exception as the param value will not be sent to your servlet.

You need to rethink your solution as the way you are doing it, you can never send actionSource & param to the servlet at the same time.


Sean
 
rammie singh
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi sean..thanks for your valuable information.
Yaah you are right...i am clicking the link first and that is why actionSource is null.
But my application was earlier designed such that each page has a hidden variable with some no as it's value and
depending on this value fetched in servlet , the corresonding switch case is executed.

now this hyperlink part is on menu page and i want to access(go to respective clicked page ) this clicked hyperlink
through servlet and at the same time pass the actionSource value to servlet because actionSource hidden name is common on evry page and depending on this value only repective case is executed for the corresponding page.


Is there any way to achieve both. Please help me by suggesting the solutions.
 
Sean Clark
Rancher
Posts: 377
Android Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well the simplest way would just be to add the actionSource to the link.



I don't know if that is suitable.

Even better would be to use the <c:url> tag in the core tag library.
 
reply
    Bookmark Topic Watch Topic
  • New Topic