简体   繁体   English

从jsp页面调用Java方法

[英]calling Java method from jsp page

I have jsp page that imports Testing.java 我有导入Testing.java的jsp页面

<jsp:useBean id="test" scope="session" class="Testing" />
<jsp:useBean id="sb" scope="session" class="SessionBean" />
<jsp:useBean id="eb" scope="session" class="ErrorBean" />

I need to call public method that is in Testing class after user confirms changes. 用户确认更改后,我需要调用Testing类中的public方法。

this is what I have so far: 这是我到目前为止所拥有的:

<tr>
    <td align="left">
        <a href="<%=test.persistPrintingInfo(eb,sb) %>" >
        <img src="../images/update.gif" OnClick="if( !confirm('Update printing information?')) return false"></a>
    </td>
</tr>

Does anyone know how to do this? 有谁知道如何做到这一点?

Can I maybe call javascript method and call persistPrintingInfo() through javascript? 我可以调用javascript方法并通过javascript调用persistPrintingInfo()吗?

您可以使用这种方式,尽管有更好的方法使用servlet。

<%com.example.Testing.yourMethod()%>

the page has been sent by the server to your browser. 该页面已由服务器发送到您的浏览器。 while javascript can modify the content of your page , in order to call a bean's method you must make a call to the server(a request to the servlet) beacause the bean lives on the server side. 尽管javascript可以修改页面的内容,但是要调用Bean的方法,您必须对服务器进行调用(对Servlet的请求),因为Bean位于服务器端。 and this call can be made by creating an url mapped to the servlet, or a form whose action is the servlet 可以通过创建映射到servlet的url或以servlet为动作的形式来进行此调用

`<FORM ACTION="${pageContext.request.contextPath}/sampleServlet">`

if the form's method is GET, then on the doGet() method of the servlet you call your bean's method. 如果表单的方法是GET,则在servlet的doGet()方法上调用bean的方法。

this form does not need to contain any kind of field. 此表格不需要包含任何类型的字段。 it is created just to make a request to the servlet. 创建它只是为了向Servlet发出请求。 while you would normally click the submit button to proceed to the action, this time we will submit the form through javascript. 尽管您通常会单击“提交”按钮以继续操作,但是这次我们将通过javascript提交表单。 with some javascript tricks, i think this form can also be hidden, because you don't actually need it to be displayed in your page 使用一些JavaScript技巧,我认为此表单也可以隐藏,因为您实际上不需要将其显示在页面中

so you simply create this form in your jsp, and submit it through javascript , like this: on your link, you will have onClick=myJavascriptMethod() ; 因此,您只需在jsp中创建此表单,然后通过javascript提交即可,如下所示:在链接上,您将拥有onClick=myJavascriptMethod() in your jsp, you create a javascript block 在您的jsp中,您创建一个javascript块

<script type="text/javascript"> function myJavascriptMethod)=() { document.forms["myform"].submit(); } </script>

a second approach which i found while googling is this one: 我在谷歌搜索时发现的第二种方法是:

How do I pass current item to Java method by clicking a hyperlink or button in JSP page? 如何通过单击JSP页面中的超链接或按钮将当前项目传递给Java方法?

in your case, the code will be 在您的情况下,代码将是

<a href="newPage.jsp"><img.. ></a>

the newPage.jsp will contain just newPage.jsp将只包含

<%yourPackage.YourClass.yourMethod()%>

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

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