4

Servlet Configuration in web.xml

<servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>DataEntry</servlet-name>
    <servlet-class>com.ctn.origin.connection.DataEntry</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>DataEntry</servlet-name>
    <url-pattern>/dataentry</url-pattern>
  </servlet-mapping>

Javascript :

<script type="text/javascript">
    function unloadEvt() {

        document.location.href='/test/dataentry';

    }
</script>

But using this javascript can't call my servlet.

Is there any error ? how to call servlet ?

chetan
  • 3,175
  • 20
  • 72
  • 113

3 Answers3

15

From your original question:

document.location.href="/dataentry";

The leading slash / in the URL will take you to the domain root.

So if the JSP page containing the script is running on

http://localhost:8080/contextname/page.jsp

then your location URL will point to

http://localhost:8080/dataentry

But you actually need

http://localhost:8080/contextname/dataentry

So, fix the URL accordingly

document.location.href = 'dataentry';
// Or
document.location.href = '/contextname/dataentry';
// Or
document.location.href = '${pageContext.request.contextPath}/dataentry';

Apart from that, the function name unloadEvt() suggests that you're invoking the function during onunload or onbeforeunload. If this is true, then you should look for another solution. The request is not guaranteed to ever reach the server. This depends on among others the browser used. How to solve it properly depends on the sole functional requirement which is not clear from the question.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I want if user fill incomplete delete and close browser then that much detail enter into database. – chetan Jun 02 '11 at 18:12
  • OK, then solve it accordingly so that you don't need to hack on the `onunload`. If you can't figure how to solve it properly, feel free to ask a new question with the **functional requirement clearly elaborated**. – BalusC Jun 02 '11 at 18:12
1

You can try this if you are using jQuery. It's simple:

<script>
    $(window).unload(function() {
        document.location.href='/test/dataentry';
    });
</script>
bsimic
  • 926
  • 7
  • 15
  • How exactly does this make difference as opposed to `window.onload = function() { ... }`? – BalusC Jun 03 '11 at 11:59
  • you are using onload. I'm referring to unload. This will capture the unload event and forward the user to /test/dataentry – bsimic Jun 03 '11 at 21:48
0

This can be done using ajax

<script type="text/javascript">
    function loadXMLDoc() {
        var xmlhttp;
        if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        } else {// code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function() {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
            }
        }
        xmlhttp.open("GET", "/testmail/dataentry", true);
        xmlhttp.send();
    }
</script>
chetan
  • 3,175
  • 20
  • 72
  • 113
  • 1
    This is **not guaranteed** to work in all browsers. It also depends on the network speed between the client and the server. – BalusC Jun 04 '11 at 13:18