简体   繁体   English

如何在不刷新页面的情况下将当前页面的url发送到servlet

[英]How to send the url of current page to the servlet without refreshing the page

I want to send the url of current page to the servlet without refreshing or reloading the page. 我想将当前页面的URL发送到servlet,而不刷新或重新加载页面。 here is the code- 这是代码-

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Action Onclick </title>

    <!--        <script>
                $('#contactForm').submit(function () {
                    alert('sdafjb');
                    return false;
                });            
            </script>-->
</head>
<body>

    <form id="contactForm" >
        <fieldset>
            <label for="Name">Name</label>
            <input id="contactName" type="text" />
        </fieldset>

        <fieldset>
            <label for="Email">Email</label>
            <input id="contactEmail" type="text" />
        </fieldset>

        <fieldset class="noHeight">
            <textarea id="contactMessage" cols="20"></textarea>


            <a href="#" onclick="document.getElementById('contactForm').submit();"> submit </a>
        </fieldset>

    </form>        
    <small id="messageSent">Your message has been sent.</small>

</body>

My servlet's name is scriptservlet. 我的servlet的名称是scriptservlet。 Please help me... 请帮我...

AJAX was built for communicating with a server without reloading or changing the current page in the browser. AJAX旨在与服务器通信而无需重新加载或更改浏览器中的当前页面。 You should be able to just create an AJAX call to your server and send your server whatever data you want without affecting the current page. 您应该能够只对服务器创建AJAX调用,并在不影响当前页面的情况下向服务器发送所需的任何数据。

Under normal circumstances, AJAX calls are restricted to "same origin" which means you can only communicate with a server on the same domain as the current web page so you would also have to make sure that you satisfy this security restriction. 在正常情况下,AJAX调用仅限于“同一来源”,这意味着您只能与与当前网页位于同一域的服务器进行通信,因此您还必须确保满足此安全性限制。

Do an ajax call to the servlet and pass the Url of current page by doing (request.getRequestUri()) to the servlet. 对servlet进行ajax调用,并通过(request.getRequestUri())对servlet传递当前页面的Url。

var requestUri = '<% request.getRequestUri()%>';
var hostname = location.host;

$.ajax({
  type: "POST",
  url: "/scriptServlet",
  data: { "host": hostname, "uri": requestUri  }
  }).done(function( msg ) {
  alert( "Data Saved: " + msg );
 });

Please note, syntax might not be correct but u have to make a ajax callt o achieve the result. 请注意,语法可能不正确,但是您必须进行ajax Callt操作才能获得结果。

This is possible by sending back the appropriate response code (204 I guess) back to the client from the server (servlet). 这可以通过将适当的响应代码(我猜是204)从服务器(servlet)发回给客户端来实现。 This way even though the request is submitted the page is not refreshed / reloaded. 这样,即使提交了请求,页面也不会刷新/重新加载。

您可以在表单中使用隐藏字段。

<input type="hidden" name="currentPage" value="<%=request.getRequestURL()%>">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM