Java 单击按钮时如何调用servlet
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22505347/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to call servlet when button click
提问by blackHyman
As new to the jsp and servlet and only having basic idea of it and still learning .I want to know how can we call the servlet class on that button click.I m using button in place of submit button
作为 jsp 和 servlet 的新手,只有它的基本概念并且仍在学习。我想知道我们如何在该按钮上调用 servlet 类单击。我使用按钮代替提交按钮
Here is the page content:-
以下是页面内容:-
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JSON DATA EXAMPLE</title>
<script type="text/javascript">
function callservlet() {
var servletname=document.getdata.fetchdata.value;
if(servletname== "")
{
alert("NO value..");
return false;
}
else
{
alert("value"+servletname);
document.location.href="JsonServlet";
return false;
}
}
</script>
</head>
<body>
<div>
<form name="getdata" action="JsonServlet" method="post">
<input type="button" name="fetchdata" value="CLick to get data" onclick="return callservlet();">
</form>
</div>
</body>
</html>
and here the servlet class content
这里是 servlet 类内容
public class JsonServlet extends HttpServlet
{
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
JsonParser parser=new JsonParser();
if(req.getParameter("fetchdata")!=null)
{
System.out.println("Button Clicked");
}
else
{
System.out.println("Button not clicked");
}
}
}
and here is the web.xml part
这是 web.xml 部分
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>JsonDataExample</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Jsonfetch</servlet-name>
<servlet-class>com.text.JsonServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Jsonfetch</servlet-name>
<url-pattern>/JsonServlet</url-pattern>
</servlet-mapping>
</web-app>
so is there any part i m write the method wrong because its gives me 404 error resource not found so i want the concept that on that button click how can i call my servlet class.Thanks for any reply
那么有没有我写的方法错误的部分,因为它给了我 404 错误资源未找到所以我想要的概念是在那个按钮上单击我如何调用我的 servlet 类。感谢任何回复
回答by Mohammad Adil
You just need to submit your form - (Instead of document.location.href="JsonServlet";
)
你只需要提交你的表格 - (而不是document.location.href="JsonServlet";
)
document.getElementByName('getdata').submit();
回答by T J
You can't set the location to a servlet. Instead, what you should do to hit the servlet is to submit your form:
您不能将位置设置为 servlet。相反,您应该做的是点击 servlet 提交您的表单:
function callservlet() {
//do your processing.
document.getElementsByName('getdata')[0].submit();
}
Or you can simply use a submit type button.
或者您可以简单地使用提交类型按钮。
回答by Shekhar Khairnar
try this :
尝试这个 :
function callservlet() {
var servletname=document.getdata.fetchdata.value;
if(servletname== "")
{
alert("NO value..");
return false;
}
else
{
alert("value"+servletname);
document.forms[0].action = "JsonServlet"
document.forms[0].submit();
}
}
回答by Shekhar Khairnar
I want to know how can we call the servlet class on that button click.I m using button in place of submit button
我想知道我们如何在该按钮上调用 servlet 类单击。我使用按钮代替提交按钮
You don't need js here, you can do it with the existing form itself. Replace the input tag as follows:
你在这里不需要js,你可以用现有的表单本身来做。替换输入标签如下:
<input type="submit" name="fetchdata" value="CLick to get data" />
If you still want to use js, try below script instead:
如果您仍想使用 js,请尝试使用以下脚本:
function callservlet() {
document.forms.getdata.submit();
}