0

I am calling a javascript function on click of anchor tags in a jsp. In jsp i have one hidden parameter(actionID) as well, whose value i am setting up in called javascript function. But when i am trying to fetch this value in servlet using request.getParameter, i am not receiving it. Please suggest.

Here is the code:

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
    }
}
Srinu Chinka
  • 1,471
  • 13
  • 19
Rahul B
  • 11
  • 3
  • I am not able to receive the value in servlet using request.getParameter("actionID"). I am getting a Blank value or a NULL value. – Rahul B May 17 '16 at 12:43

1 Answers1

0

You are missing name attribute in you hidden input(actionID).

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

Request parameters are named by name attribute. This should work

<input type="hidden" id="actionID" name="actionID">
Srinu Chinka
  • 1,471
  • 13
  • 19
Kamyk.pl
  • 309
  • 2
  • 8
  • Hi, I have changed the code as below: ' and js function as ' Now i am getting blank value in place of NULL for "actionID" parameter. – Rahul B May 17 '16 at 12:32