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 ?

3
  • did you try loading the servlet by typing the url into the browser's address bar? Commented Jun 2, 2011 at 17:53
  • I'll assume that the double double quote on the end of the URL is just a careless typo. Please fix it in your question if true. Commented Jun 2, 2011 at 17:56
  • yes i am call while submit form and work perfetct. Now i want to call it onunload event of javascript Commented Jun 2, 2011 at 18:06

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I want if user fill incomplete delete and close browser then that much detail enter into database.
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.
1

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

<script>
    $(window).unload(function() {
        document.location.href='/test/dataentry';
    });
</script>

2 Comments

How exactly does this make difference as opposed to window.onload = function() { ... }?
you are using onload. I'm referring to unload. This will capture the unload event and forward the user to /test/dataentry
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>

1 Comment

This is not guaranteed to work in all browsers. It also depends on the network speed between the client and the server.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.