-1

I wrote the JSP page and post the form by auto submit (JavaScript) code to the servlet.

In servlet I print result once but when I execute the app I get out put more than once. JSP:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title></title>
    <script type="text/javascript" src="/src/js/tableform.js"></script>
    <link rel="stylesheet" type="text/css" href="/css/table.css">
</head>
<body>


<form id="myForm" name="LOAD" action="/gridview" method="post">
    <input type="hidden" name="pageAction" value="readTable">
</form>
</body>
<script>
    setTimeout(submitform,0);

    function submitform()
    {
        //alert('test');
        document.getElementById("myForm").submit();
    }
</script>
</html>

Servlet doPost:

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("here");
        String pageAction = request.getParameter("pageAction");
        System.out.println(pageAction);
}

OUTPUT:

here
readTable
here
readTable
here
readTable
here
readTable
here
readTable
here
readTable
here
readTable
here
readTable
here
readTable
here
readTable
here
readTable
here
readTable
here
readTable
here
readTable
here
readTable
here
readTable
here
readTable
here
readTable
here
readTable

Lyubomyr Shaydariv
  • 20,327
  • 12
  • 64
  • 105
noor
  • 41
  • 1
  • 4
  • This is a HUGE [XY problem](http://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). Your real answer is here: http://stackoverflow.com/questions/3590961/calling-a-servlet-from-jsp-file/ – BalusC Jun 28 '15 at 08:07

1 Answers1

0

The setInterval() method calls a function at a specified intervals (in milliseconds). You write 0, so it will call submitform() method after every second. That's why you are getting output more than once. If you specify milliseconds, then it call after particular milliseconds.

var auto_refresh = setInterval(function() { submitform(); }, 0);

The setInterval() method will continue calling the function until clearInterval() is called, or the window is closed.

auto_refresh will be used as a parameter while using clearInterval function.

clearInterval(auto_refresh);

Example - I create a example in MyEclipse

JSP Page

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <script type="text/javascript">
    function submitform()
    {
        document.getElementById("myForm").submit(); 
    }
    </script>
  </head>

  <body onload="submitform()">
    <form id="myForm" name="LOAD" action="servlet/abc" method="post">
    <input type="hidden" name="pageAction" value="readTable">
</form>
  </body>
</html>

Servlet

public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        System.out.println("here");
        String pageAction = request.getParameter("pageAction");
        System.out.println(pageAction);
}

Output Screenshot - See result in last two lines

enter image description here

Puneet Chawla
  • 5,729
  • 4
  • 16
  • 33
  • Chawala I change the code still I get same problem – noor Jun 28 '15 at 06:26
  • Noor, setTimeout also work as same setInterval. Tell me one thing why you need to use it. what you want actually. Just call submitform() onload of body or click on any button or whenever you want. – Puneet Chawla Jun 28 '15 at 06:32
  • I want on page load jsp read data form servlet and set attribute on request object and loade that on first jsp – noor Jun 28 '15 at 06:59
  • Have you tried this - call submitform() method on onload event of body. – Puneet Chawla Jun 28 '15 at 07:02
  • I think, you are doing some mistake. I have tried in normal example. When we use then it call function on pageload (if function definition is available). Remove setTimeOut..... and use alert to check either submitform() method is calling or not or create a normal html form and check there. If doesn't work then wait some time, i will give complete example. – Puneet Chawla Jun 28 '15 at 07:11
  • I have updated my answer with giving complete example. As i said onload event would work and it's working. I hope, it will solve your problem now. – Puneet Chawla Jun 28 '15 at 07:31