我们可以通过javascript函数将参数传递给servlet吗?
这是我的代码,其中我试图通过单击“获取作者姓名”按钮将值从 addUpdate.jsp 的 javascript 函数传递给 BookServlet。当我单击按钮时,它成功地转到 servlet,但它没有采用我的参数通过函数..
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>
这是正在传递的参数..
http://localhost:8082/OBL/BookServlet?longT=&shortT=&shortT=&dop=&nop=&noca=&Go=Get+Authors+names