简体   繁体   English

从 JSP 上的其他 Class 调用方法

[英]Invoke Method from other Class on JSP

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.有 JSP index.jsp 文件和 Button 调用 Class GetDetails 和内部方法,让我们说“测试”方法。 Button works fine if ie js function is called, but doesn't for other Class Method.如果调用了即 js function,则按钮可以正常工作,但对于其他 Class 方法则不行。 pls take a look and point me my errors.:)请看一下并指出我的错误。:)

index.jsp索引.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获取详细信息.java

import try.invokeMe;
    public class GetDetails {
        static public String testMethod2(){
            System.out.println("testetstest");
            return "testetsttest";
        }
    }

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")我找到了一些不同的解决方案,希望它会有所帮助:(从 java class 按下按钮调用方法,然后从那里创建回复到网站作为文本:“testtesttest”)

index.jsp索引.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 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;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM