参数通过 javascript 函数传递给 servlet
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18583835/
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
Parameter passing through javascript function to a servlet
提问by xyz
Can we pass parameter to a servlet through javascript function?
我们可以通过javascript函数将参数传递给servlet吗?
Here is my code wherein i am trying to pass values from addUpdate.jsp's javascript function to BookServlet on click of a button 'Get Authors names'.While I click the button it is successfully going to servlet but it does not taking the parameter that I pass through function..
这是我的代码,其中我试图通过单击“获取作者姓名”按钮将 addUpdate.jsp 的 javascript 函数中的值传递给 BookServlet。当我单击按钮时,它成功地转到了 servlet,但它没有采用我的参数通过函数..
addUpdate.jsp :
addUpdate.jsp :
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@page import="com.fulcrum.obl.model.Author"%>
<%@page import="java.util.ArrayList"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@page isELIgnored="false" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript">
function submitAddForm(button) {
alert("hiee");
if(button.value=="Get Authors names") {
alert("in iff");
//alert(document.getElementById("bookDetails"));
document.getElementById("bookDetails").action="BookServlet?authorNames=authorNames";
}
}
function setAuthorsNames() {
var x =document.getElementById("select");
var myList = document.getElementById("selected");
var myOption = document.createElement("Option");
for(var i=0;i < x.options.length;i++) {
if(x.options[i].selected ==true){
alert("in if");
myOption.text = x.options[i].value;
myOption.value = x.options[i].value;
myList.add(myOption);
x.remove(x.options.selectedIndex);
}
}
}
</script>
</head>
<body>
<h4>Book Form</h4>
<form id="bookDetails">
Long Title : <input type="text" name="longT">
<br>
<br>
Short Title : <input type="text" name="shortT" />
<br>
<br>
Isbn : <input type="text" name="shortT" />
<br>
<br>
Date of Publication : <input type="text" name="dop" />
<br>
<br>
No of pages : <input type="text" name="nop" />
<br>
<br>
bound type :
<select id="boundType">
<option value="0">HardBound</option>
<option value="1">SoftBound</option>
</select>
<br>
<br>
DVD Availability :
<select id="dvd">
<option value="y">Yes</option>
<option value="n">No</option>
</select>
<br>
<br>
No of copies available : <input type="text" name="noca" />
<br>
<br>
<%
if(request.getAttribute("authorNamesList") != null) {
ArrayList<Author> booksList = (ArrayList<Author>)request.getAttribute("authorNamesList");
%>
<select id="select" size="6">
<c:forEach items="${authorNamesList}" var="authorNames">
<option value="${authorNames.firstName}">${authorNames.firstName}</option>
</c:forEach>
</select>
<%
}
%>
<select size="6" id="selected">
</select>
<input type="submit" value="Get Authors names" name="Go" onClick="submitAddForm(this)">
<input type="button" value="Add" name="Add" onClick="setAuthorsNames()">
<br>
<br>
<input type="submit" value="Save Book" name="submitBook" onClick="submitAddForm()">
</form>
</body>
</html>
here is the parameter that is getting passed..
这是传递的参数..
http://localhost:8082/OBL/BookServlet?longT=&shortT=&shortT=&dop=&nop=&noca=&Go=Get+Authors+names
回答by jsshah
You can use a hidden input element and set the value of the hidden input element before submitting the form.
您可以使用隐藏输入元素并在提交表单之前设置隐藏输入元素的值。
Lets say your hidden element has id "hidden_input" then in your submitAddForm function add the following line
假设您的隐藏元素具有 id“hidden_input”,然后在您的 submitAddForm 函数中添加以下行
document.getElementById("hidden_input").value = "desired_value";
now when form is submitted ... the hidden input value will be set and passed to the server
现在提交表单时......隐藏的输入值将被设置并传递给服务器
回答by Manish Sahni
You can change your code and pass the parameters from javascript to the servlets by using :-
您可以使用以下方法更改代码并将参数从 javascript 传递到 servlet:-
function submitAddForm(button) {
var longT = document.getElementById("bookDetails").value;
var shortT = document.getElementById("shortT").value;
...........
...........
then pass the variables and do
document.forms[0].action = "BookServlet?longT="+longT+"shortT="+shortT....and all the other variables to pass;
document.forms[0].submit();
}
And in your JSP you can have an "id" attribute as :
在您的 JSP 中,您可以将“id”属性设为:
Long Title : <input type="text" name="longT" id="longT">
长标题: <input type="text" name="longT" id="longT">