0

Hello

I have a jsp document,and a java class with some functions,i want that a button that located in the jsp document,will execute a function from the java call on click.

but i don't really know how to do it,and i am stuck on it for a week now.

If anyone can please help me,or direct me to an example or something.

Please

Thanks Ahead
Michael

2
Contributors
3
Replies
5
Views
7 Years
Discussion Span
Last Post by javaAddict
0

Well, you cannot mix java with JSP. Meaning that you cannot just execute a java function when a button is clicked, because java is executed at the server and when you are at the page, you are at the client.

What you can do is, when you click a button to submit to a servlet, then execute the java method and then redirect back to the JSP.

An easier way for you to understand, but not entirely the best way would be to submit to another jsp page:

Page 1:

<form action="page2.jsp">
  Input: <input type="text" name="someInput" />
  <input type="submit" name="submit" value="Submit Buton" />
<form>

Page 2:

// This will be executed at the server:
// JAVA CODE
<%
String input = request.getParameter("someInput");
JavaClass jc = new JavaClass();
String result = jc.someMethod(input);
%>

// And this would be rendered at the client:
// HTML CODE:
Result: <%=result%>
0

First of all thanks a lot for the reply.

I red somewhere that the more correct way is that the jsp document will cal a servlet that will execute the code and reply back to the jsp .

Do you accidentally have any example of how i do that ?

Thanks a lot again
Michael

0

First of all thanks a lot for the reply.

I red somewhere that the more correct way is that the jsp document will cal a servlet that will execute the code and reply back to the jsp .

Do you accidentally have any example of how i do that ?

Thanks a lot again
Michael

Check the tutorials on top of the jsp forum. Try the one about MVC and database connectivity. It has an example on how to redirect, using request dispatcher:

request.setAttribute(yourObject, "key");
RequestDispatcher dispatcher = request.getRequestDispatcher("page.jsp");
dispatcher.forward( request, response);

And at the page.jsp:

YourObject yo = (YourObject)request.getAttribute("key");
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.