参数通过javascript函数传递给servlet

时间:2013-09-03 03:56:45

标签: java javascript jsp servlets

我们可以通过javascript函数将参数传递给servlet吗?

这是我的代码,其中我试图将addUpdate.jsp的javascript函数中的值传递给BookServlet,只需单击“Get Authors names”按钮。虽然我单击按钮但它成功进入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

2 个答案:

答案 0 :(得分:0)

您可以在提交表单之前使用隐藏的输入元素并设置隐藏的输入元素的值。

让我们说你的隐藏元素有id“hidden_​​input”然后在你的submitAddForm函数中添加以下行

document.getElementById("hidden_input").value = "desired_value";

现在提交表单时...隐藏的输入值将被设置并传递给服务器

答案 1 :(得分:0)

您可以使用以下命令更改代码并将参数从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();
}

在JSP中,您可以将“id”属性设置为:

长标题:<input type="text" name="longT" id="longT">