aspose file tools*
The moose likes Servlets and the fly likes Make hyperlink that when clicked call a java method and display results Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Make hyperlink that when clicked call a java method and display results" Watch "Make hyperlink that when clicked call a java method and display results" New topic
Author

Make hyperlink that when clicked call a java method and display results

Nada Bajnaid
Ranch Hand

Joined: Aug 14, 2011
Posts: 45
Hi I'm using servlet to search for learning concepts. for example when the user query about validation and click submit the results appear as following:

important learning resources:
[url]hyperlink1:
hyperlink2:[/url]
:

other related topics
[url]topic1
topic2
topic3[/url]

for the first list it is dirct hyperlink. For the second list I need to hyperlink each topic so that when clicked the topic will be send to a method to search for it and display result. So there is no direct URL but I need to allow the user (if want to) to select one of the topic and send it for a search methd. How this can be done? shall I use the <a> tag with herf? but there is no url. or is there another way to make the action go to another servelet or method?

Thanks
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
You can associate an action with each hyperlink, then in your servlet check the action and call the appropriate method.


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Nada Bajnaid
Ranch Hand

Joined: Aug 14, 2011
Posts: 45
what is the right way to do this. is this code correct

printWriter out = response.getWriter();
out.println ("<a herf=pakageName\className\methodName" + topic1+ "</a>");

Thanks
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61653
    
  67

There is no such attribute as herf. And you need to make sure that whatever HTML you are emitting is correct.

Why are you using a servlet to create the view? That's the job of the JSP.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18907
    
    8

You seem to think it's possible to make a hyperlink which will connect to your server and call an arbitrary method in your Java code. It isn't.

When connecting to a servlet, the link will either call the doGet() method or the doPost() method of the servlet depending on how the link is set up.

So if you need certain methods to be called, the way to do that is to pass parameters to the servlet, which the servlet code will then inspect and do the appropriate thing, as Bosun Bello already suggested.
Nada Bajnaid
Ranch Hand

Joined: Aug 14, 2011
Posts: 45
I'm not sure that I clearly understand what you said. This is my first servelet that manipulates OWL ontology using Jena and JessBridge. Everything works fine except this part. Do you mean that I build another servlet within the project that accepts parameters and call it in hyperlink?

the herf is the an attribute of the <a> tag that allow displaying text as hyperlink in html. Is there another way?

Thanks
Nada Bajnaid
Ranch Hand

Joined: Aug 14, 2011
Posts: 45
I don't think this can be done using JSP as the output is changrd each time based on creating instances of other classes and calling some methods of these classes which returns the results in vectors that are displayed to the user
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61653
    
  67

Nada Bajnaid wrote:the herf is the an attribute of the <a> tag that allow displaying text as hyperlink in html.

No, its not. Check your spelling.

And of course it can be done in a JSP. JSP is all about dynamic displays.
Nada Bajnaid
Ranch Hand

Joined: Aug 14, 2011
Posts: 45
Sorry it is href
Nada Bajnaid
Ranch Hand

Joined: Aug 14, 2011
Posts: 45
OK. suppose that I make this search task using JSP then it displays the results as in first message then when one of the topics is clicked the same process will be done for this topic as the query text. Will it be possible to receive client request (clicking the topic) in a JSP and then make the JSP call itself to restart the search process?

Is there a good reference for such code that I can learn from it. I run out of time

Thanks
Nada Bajnaid
Ranch Hand

Joined: Aug 14, 2011
Posts: 45
Thanks for your support

I did it using the onClick attribute

out.println ("<a href=\"newPage.html\" onClick=\"SQA.System/testMethod(s);return false\">" +(String)itr.next()+"</a><br>");

where testMethod placed in the same servlet. and SQA>System is the package.

There is no syntax error. The application runs correctly untill I click on one of the topics where the function testMethod should be processed but it doesn't. I think the problem is with the function URL. I also tried it without the package name.

Does any one knows what I did wrong? How I should pass the method testMethod?

Thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Make hyperlink that when clicked call a java method and display results