File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Calling function with onClick attribute Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Calling function with onClick attribute" Watch "Calling function with onClick attribute" New topic
Author

Calling function with onClick attribute

Nada Bajnaid
Ranch Hand

Joined: Aug 14, 2011
Posts: 45
I'm trying to call a function testMethod using onClick



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 Topic1 where the function testMethod should be processed but it doesn't. I think the problem is with the way I passed the function I also tried it without the package name.

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

Thanks
Abhishek Purwar
Ranch Hand

Joined: Dec 15, 2007
Posts: 63

You cannot call servlet methods directly from html page. onClick attribute works for javascript functions and not for servlet methods.
For servlet methods, you need to create url mapping and call url for servlet from HTML page.


Abhishek Purwar,
Bangalore.
Nada Bajnaid
Ranch Hand

Joined: Aug 14, 2011
Posts: 45
Ok. can I call another servlet using onClick? if not

What is url mapping? and how I can do it?

in my application the user enter a query concept and when submit it, in the doPost method where an ontology model is accessed and the results are displayed as following

important learning resources
hyperlink1...
hyperlink2...
:

Related Topics
Topic1
Topic2
:

I have no problem with the first list. With the second list I need each topic to be a link that when clicked the same process will be repeated where the query now is the selected topic? it works except for that part where the links are not direct URLs instead the process of the doPost is repeated but this time the user will not enter the query instead the query is assigned to the selected topic

This is why I tried methods to do the search jop and display the results.

Any suggestion is appreciated
Abhishek Purwar
Ranch Hand

Joined: Dec 15, 2007
Posts: 63

Url mapping is the path which is associated with servlet if you want to access servlet using http request.
In web.xml file, one can define url mapping for different servlets.

Can you provide sample urls which you want to invoke from different links??
Nada Bajnaid
Ranch Hand

Joined: Aug 14, 2011
Posts: 45
I use tomcat 7 where web.xml is not required instead I use @WebServlet("/SQA") wher in my html file I use
<form action="SQA" method = "get">

in the doGet method I use the following to get the user query
response.getWriter().print("<form action = './SQA' method = 'post'>");

then in the doPost model I do the search process which creates the results then from these results I need to do the process again? shall I call the doPost again asigning value to the query variable instead of getting it from parameters?
Nada Bajnaid
Ranch Hand

Joined: Aug 14, 2011
Posts: 45
I the doPost method I do the following

ReadOntology model = new ReadOntology();
String queryStr = request.getParameter("query");
model.coreLearningObjects(queryStr);
:

can I call the doPost method from the doPost itself passing value to the queryStr?

something like this

out.println ("<a href=\ "http://localhost:8080/myProjectName/myServletName?queryStr=" + selectedTopic+"</a>");

Nada Bajnaid
Ranch Hand

Joined: Aug 14, 2011
Posts: 45
Or shall I create another servlet and call its doPost passing parameters to it (?queryStr=)
if yes what is the way to do it?

Thanks
Akshay D Joshi
Greenhorn

Joined: Aug 04, 2011
Posts: 9
Hello Nada Bajnaid

I think...
You should write java script function .....
call this function on click of link.......
And in java script function assign the form's action to your servlet....

It will be called automatically.....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Calling function with onClick attribute