从JSP调用Servlet的方法 [英] calling Servlet's method from JSP

查看:132
本文介绍了从JSP调用Servlet的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JSP和Servlets开发一个Web应用程序。



当用户点击Update按钮时,我想从JSP页面调用servlet的方法。

 < input type =buttonvalue =更新onclick =<%MyServletName.Update(request,response );%>中>< /输入> 

当我单击更新按钮时,此方法正在调用servlet,但问题在于当表单这个方法被自动调用。

预先感谢.....

源代码。 ...

 <%@ page import =MyPackageName.MyServletName%> 
< html>
< head>
< meta http-equiv =Content-Typecontent =text / html; charset = ISO-8859-1>
< title>更新< / title>
< / head>
< body>
< form>
<%
String [] [] data =(String [] [])request.getAttribute(data);
String [] columnNames =(String [])request.getAttribute(columnNames);
//填充表数据
if(columnNames.length!= 0&&; data.length!= 0)
{
%>< table><< ;%
}
for(int i = 0; i< data.length; i ++){
%>
< tr> < td><%= columnNames [0]%>< / td> < td>< input type =textname =text1value =<%= data [i] [0]%>>< / td> < / TR>
< tr> < td><%= columnNames [1]%>< / td> < td>< input type =textname =text2value =<%= data [i] [1]%>>< / td> < / TR>
< tr> < td><%= columnNames [2]%>< / td> < td>< input type =textname =text3value =<%= data [i] [2]%>>< / td> < / TR>
<%
}
%>
< tr>
< td>< / td>
< td>< input type =buttonvalue =Updateonclick =<%PlanProtocolEdit.Update(request,response);%>>< / input>< TD>
< / tr>
< / table>
< / form>
< / body>
< / html>

是否有任何方法可以调用除dogGet()和doPost()以外的servlet方法调用servlet的dogGet()和doPost()方法?

解决方案

您应该在表单上指定action而不是实际的输入按钮。



如果您在表单上定义了一个操作。例如

表单提交按钮会将表单提交给该URL。例如:

 < form action =/ mypath / someurl> 
< input type =submitvalue =Submit/>
< / form>

您可以将其他属性添加到窗体标签来指定您希望请求是 get 还是 post 。

 < form action =/ mypath / someurlmethod =POST> 

会发送一个post请求,然后您可以在Servlet中使用 handlePost method

应该使用上述方法,目前您正尝试在javascript onclick事件中调用Java方法。这是不正确的,它并没有做你认为的事。



PlanProtocolEdit 中的代码。更新应该在一个servlet的 doGet doPost 方法中,该方法将通过如上所述配置您的表单来触发。

I am developing a web application using JSP and Servlets.

I wants to call servlet's method from JSP page when user click's on the Update button.

<input type="button" value="Update" onclick="<%MyServletName.Update(request, response);%>"></input>

When I click on Update button then this method is calling the servlet, But the problem is that when form is loaded then this method is called automatically.

Thanks in advance.....

Source Code....

<%@page import="MyPackageName.MyServletName"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Update</title>
</head>
<body>
<form>
<%
String[][] data = (String[][])request.getAttribute("data");
String[] columnNames = (String[])request.getAttribute("columnNames");
//fill the table data
if(columnNames.length!=0 && data.length!=0)
{
%><table><%
}
for(int i=0;i<data.length;i++){
%>
<tr> <td><%= columnNames[0] %></td> <td><input type="text" name="text1" value="<%= data[i][0] %>"></td> </tr>
<tr> <td><%= columnNames[1] %></td> <td><input type="text" name="text2" value="<%= data[i][1] %>"></td> </tr>
<tr> <td><%= columnNames[2] %></td> <td><input type="text" name="text3" value="<%= data[i][2] %>"></td> </tr>
<%
}
%>
<tr>
<td></td>
<td><input type="button" value="Update" onclick="<%PlanProtocolEdit.Update(request, response);%>"></input></td>
</tr>
</table>
</form>
</body>
</html>

Is there any way that I can call the servlet method other than dogGet() and doPost() without invoking the servlets dogGet() and doPost() methods?

解决方案

You should specify the action on the form itself not the actual input button.

If you define an action on the form. E.g.

The forms submit button will submit the form to that URL. E.g.

<form action="/mypath/someurl">
    <input type="submit" value="Submit" />
</form>

You can add an additional attribute to the form tag to specify whether you want the request to be a get or a post.

<form action="/mypath/someurl" method="POST">

Would send a post request, you can then pick this up in your Servlet with the handlePost method

The above approach should be used, currently you are trying to call a Java method on a javascript onclick event. This is incorrect and it is not doing what you think it is.

The code in PlanProtocolEdit. Update should be in a doGet or doPost method of a servlet which will be triggered by configuring your form as described above.

这篇关于从JSP调用Servlet的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆