0

I know Subject looks similar to ones you can find on forum, but I did try and unfortunatelly couldn't solve my problem. Have JSP index.jsp file with Button which invokes to Class GetDetails and inner method, let say "test" method. Button works fine if i.e js function is called, but doesn't for other Class Method. pls take a look and point me my errors.:)

index.jsp

....
<html>
<head>
    <meta charset="UTF-8">
    <title>Invoke method - test</title>
</head>
<body>
<button onclick="f_show()">show</button>
<div id="div1"></div>
<div id="div1"></div>

</body>
<script>
    function f_show(){
        var sText="testtesttest"
        document.getElementById("div1").innerHTML=sText;
     <%
 GetDetails test=new GetDetails();
 test.testMethod2();
 %>
        document.getElementById("div2").innerHTML = test;
    }

</script>

GetDetails.java

import try.invokeMe;
    public class GetDetails {
        static public String testMethod2(){
            System.out.println("testetstest");
            return "testetsttest";
        }
    }
k1313
  • 11
  • 4
  • there is, sorry, updating... yes, just to test now if Button can invoke other Clkass Method – k1313 Mar 19 '21 at 16:07
  • It will never work the way you want it to work if you do it like that. That method won't be executed when you click the button, but when the page is rendered on the server side. What you probably want is a button that does an ajax call to a servlet that calls the method you need to call. – Federico klez Culloca Mar 19 '21 at 16:10
  • Take a look at [this other question](https://stackoverflow.com/questions/13840429/what-is-the-difference-between-client-side-and-server-side-programming) for a more detailed explanation about why that won't work. – Federico klez Culloca Mar 19 '21 at 16:11
  • And [here](https://stackoverflow.com/questions/46659076/calling-servlet-on-jsp-page-button-click) for how to do it with a servlet. – Federico klez Culloca Mar 19 '21 at 16:13
  • Even better, see [this](https://stackoverflow.com/questions/4112686/how-to-use-servlets-and-ajax) for an example using ajax. – Federico klez Culloca Mar 19 '21 at 16:15
  • thx Frederico, looking promising, checking now – k1313 Mar 19 '21 at 16:18
  • Could you add a working and a not-working example to see what exactly you tried? – Poohl Mar 19 '21 at 19:39
  • the first div "div1" shows properly "testtesttest" coz function is ar the same page. second, "div2" doesnt show anything. believe, as Frederico said, it is matter of addressing to servlet. working on it, looking easy but...:) , I already created similar example as he linked me to print out plain text but doesnt work... – k1313 Mar 19 '21 at 20:37

1 Answers1

0

I found a little bit different solution, hope it can be helpfull: (pressing the button invoking method from java class and from there creating response back to website as text: "testtesttest")

index.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html lang="en">
<head>
    <title>test button</title>
</head>
<body>
<p></p>
<c:if test="${not empty exampleText}">
    <p><c:out value="${exampleText}"/></p>
</c:if>
<form action="test" method="post">
    <button id="somebutton">press here</button>
    <div id="somediv"></div>
</form>
</body>
</html>

testButtonController.java

package com.example;

import jakarta.servlet.ServletContext;

import java.io.IOException;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.*;

@WebServlet("/test")

public class testButtonController extends HttpServlet {

    protected void doGet(jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher("index.jsp").forward(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        createExampleText();
        response.sendRedirect(request.getContextPath());

    }

    private String createExampleText() {
        ServletContext context = getServletContext();
        String exampleText = (String) context.getAttribute("exampletext");
        exampleText="testtesttest";
        context.setAttribute("exampleText", exampleText);
        return exampleText;
    }
}
k1313
  • 11
  • 4