请求servlet中的对象,而不是从jsp接收参数值

时间:2016-05-17 10:46:38

标签: javascript jsp servlets

我在点击jsp中的锚标签时调用了一个javascript函数。 在jsp中我也有一个隐藏参数(actionID),其值在我调用的javascript函数中设置。 但是当我尝试使用request.getParameter在servlet中获取此值时,我没有收到它。 请建议。

以下是代码:

针对home.jsp:

<html>
<head>
<script type="text/javascript">
   function func1(str) {
       document.getElementById("actionID").value = str;
       document.forms["action"].submitTestPost.click();
   }
</script>

<title>Home</title>
</head>

<body>

    <div class="menubar">
        <form id = "action" action="/" method="post">
            <input type="hidden" id="actionID">
            <a href="#" onclick="func1('ViewEmployeeDetails')"> View Employee Details </a><br>
            <a href="#" onclick="func1('addLocation')"> Add New Location </a><br>
            <a href="#" onclick="func1('addCluster')"> Add New Cluster/Sub Cluster  </a><br>
            <a href="#" onclick="func1('CreateReport')"> Generate Report  </a><br>
            <input type="submit" name="submitTestPost" value="x">
        </form>
    </div>

</body>
</html>

的web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<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_3_0.xsd" id="WebApp_ID" version="3.0">
  <display-name>TestProj</display-name>
  <welcome-file-list>
    <welcome-file>Login.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <servlet-name>login</servlet-name>
    <servlet-class>com.pkg.Login.LoginServlet</servlet-class>
  </servlet>
    <servlet>
    <servlet-name>ViewEmployeeDetails</servlet-name>
    <servlet-class>com.pkg.Employee.ViewEmployeeDetails</servlet-class>
  </servlet>
  <servlet>
    <servlet-name>AddLocation</servlet-name>
    <servlet-class>com.pkg.Login.AddLocationClusterInfo</servlet-class>
  </servlet>
  <servlet>
    <servlet-name>AddCluster</servlet-name>
    <servlet-class>com.pkg.Login.AddLocationClusterInfo</servlet-class>
  </servlet>
    <servlet>
    <servlet-name>CreateReport</servlet-name>
    <servlet-class>com.pkg.Login.ReportServlet</servlet-class>
  </servlet>


  <servlet-mapping>
    <servlet-name>login</servlet-name>
    <url-pattern>/login</url-pattern>
  </servlet-mapping>
    <servlet-mapping>
    <servlet-name>ViewEmployeeDetails</servlet-name>
    <url-pattern>/ViewEmployeeDetails</url-pattern>
  </servlet-mapping>
    <servlet-mapping>
    <servlet-name>AddLocation</servlet-name>
    <url-pattern>/addLocation</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>AddCluster</servlet-name>
    <url-pattern>/addCluster</url-pattern>
  </servlet-mapping>
      <servlet-mapping>
    <servlet-name>CreateReport</servlet-name>
    <url-pattern>/CreateReport</url-pattern>
  </servlet-mapping>

</web-app>

AddLocationClusterInfo.java

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    try {
        System.out.println("actionID:"+request.getParameter("actionID"));                               
    }
    catch (Exception e) {
        // TODO: handle exception
    }
}

1 个答案:

答案 0 :(得分:0)

name输入hidden)中缺少actionID属性。

<input type="hidden" id="actionID">

请求参数由name属性命名。这应该工作

<input type="hidden" id="actionID" name="actionID">