[英]Passing value from a jsp button click to a servlet
ininI am bit new to jsp and servlet. 我对jsp和servlet有点陌生。 I need to pass a value to a servlet on a button click. 我需要在单击按钮时将值传递给servlet。 below I have mentioned my code. 下面我提到了我的代码。 web.xml web.xml
<servlet>
<servlet-name>Login</servlet-name>
<servlet-class>org.wso2.carbon.identity.application.authentication.endpoint.oauth2.OAuth2Login</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Login</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
test.jsp test.jsp
function ok() {
$.ajax({
url: "/login",
data: 'test=' +'test',
type: "GET",
async: false,
success: function (data) {
}
});
}
below is my html code in test.jsp 以下是我在test.jsp中的html代码
<button id="ok" class="btn btn-primary btn-large" onclick="ok()">OK</button>
The servelet 发球
public class OAuth2Login extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,
IOException {
System.out.print("========do get fires===========");
}
} }
But when my test.jsp is loading it invokes the doget() of the servlet. 但是当我的test.jsp加载时,它会调用servlet的doget()。 But at the button click it does not. 但是在按钮上单击则不会。 I dont need to invoke the servlet at the page load. 我不需要在页面加载时调用servlet。 But I need it on the button click. 但是我需要在按钮上单击。 Help me to solve this out. 帮我解决这个问题。 Sorry for the ignorance. 对不起,很抱歉。 :) :)
Write your servlet mapping url in url attribute: 在url属性中编写您的servlet映射网址:
function myFun() {
var requestPath = "<%=request.getContextPath()%>";
$.ajax({
url: requestPath+"/login";
data: {"data1":"value1", "data2": "value2"}
type: "GET",
async: false,
success: function (data) {
}
});
}
I think in web.xml /test.jsp this is wrong in place of /test.jsp you can give yours servlet name like 我认为在web.xml /test.jsp中,这可以代替/test.jsp,这是错误的,您可以给您的servlet名称加上
<servlet-mapping>
<servlet-name>Login</servlet-name>
<url-pattern>/login</url-pattern>
</servlet-mapping>
and later on button click which is in test.jsp you can call ajax function and call get method of servlet 然后单击在test.jsp中的按钮,就可以调用ajax函数并调用servlet的get方法
function ok() {
$.ajax({
url: "/login",
data: 'test=' +'test',
type: "GET",
async: false,
success: function (data) {
}
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.