编写将包含在其他网站中的js的正确方法是什么?

发布于 2024-11-08 18:34:18 字数 310 浏览 0 评论 0原文

我需要编写一个脚本,我想将其包含在不同的网站上(类似于您必须包含在网站页面中的谷歌分析js)。

该脚本必须向我的一个 servlet(我正在使用 java)发送一个请求。

在 servlet 中,我需要增加一些变量,然后将图像返回给客户端。该图像将显示在用户访问的网站上。

我还需要在 servlet 中获取客户端的信息(ip 等)。如果我使用 getRemoteAddr() 方法,在这种情况下它会起作用吗?

此外,我需要跟踪我向客户端显示的图像。(我不知道这应该在哪里,客户端还是服务器端)。

这是执行此操作的正确方法吗?

I need to write a script which i want to include on different websites(something similiar with the google analytics js that you have to include in your website pages).

This script has to send to one of my servlets(i'm using java) a request.

In the servlet i need to increment some variables and after that return an image to the client. The image will be displayed on the website the user accessed.

I also need to get the client's information (ip,etc) in the servlet. if i use getRemoteAddr() method, will it work in this case?

Furthermore i need to keep track on the images i displayed to the client.(i don't know where this should be, client side or server side).

Whics is the proper way of doing this ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

辞慾 2024-11-15 18:34:18

您的 JS 必须将指向 1x1 透明 GIF 图像的 打印/附加到文档中。 JS 收集的所有信息都可以作为图像 URL 上的查询字符串发送。 Google Analytics(分析)会执行类似的事情

基本上:

<script src="http://yourdomain.com/track.js"></script>

with:

var requestURI = window.location;
var referrer = document.referrer;
var resolution = screen.width + 'x' + screen.height;
var colorDepth = screen.colorDepth;
// ...

var query = '?requestURI=' + encodeURIComponent(requestURI)
          + '&referrer=' + encodeURIComponent(referrer)
          + '&resolution=' + encodeURIComponent(resolution)
          + '&colorDepth=' + encodeURIComponent(colorDepth);

document.write('<img src="http://yourdomain.com/track.gif' + query + '" />');

然后,在 yourdomain.com 中,您必须在图像 URL 上映射 servlet

<servlet>
    <servlet-name>trackServlet</servlet-name>
    <servlet-class>com.example.TrackServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>trackServlet</servlet-name>
    <url-pattern>/track.gif</url-pattern>
</servlet-mapping>

doGet() 方法可以收集所有信息并最终返回真实的 1x1 gif 图像:

private static final byte[] GIF = {
    71, 73, 70, 56, 57, 97, 1, 0, 1, 0, -16, 0, 0, 0, 0, 0, 0, 0, 0, 33, -7,
    4, 1, 0, 0, 0, 0, 44, 0, 0, 0, 0, 1, 0, 1, 0, 0, 2, 2, 68, 1, 0, 59
};

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // Gather JS-collected parameters.
    String requestURI = request.getParameter("requestURI");
    String referrer = request.getParameter("referrer");
    String resolution = request.getParameter("resolution");
    String colorDepth = request.getParameter("colorDepth");
    // ...

    // Gather server-collected parameters.
    String remoteAddr = request.getRemoteAddr();
    String userAgent = request.getHeader("user-agent");
    // ...

    // Send 1x1 transparent gif (and disable browser caching on it!)
    response.setHeader("Content-Type", "image/gif");
    response.setHeader("Cache-Control", "no-cache,no-store,must-revalidate");
    response.setHeader("Pragma", "no-cache");
    response.setDateHeader("Expires", 0);
    response.getOutputStream().write(GIF);
}

Your JS has to print/append an <img> pointing to a 1x1 transparent GIF image to the document. All information collected by JS can be sent as a query string on the image URL. Google Analytics does similar thing.

Basically:

<script src="http://yourdomain.com/track.js"></script>

with:

var requestURI = window.location;
var referrer = document.referrer;
var resolution = screen.width + 'x' + screen.height;
var colorDepth = screen.colorDepth;
// ...

var query = '?requestURI=' + encodeURIComponent(requestURI)
          + '&referrer=' + encodeURIComponent(referrer)
          + '&resolution=' + encodeURIComponent(resolution)
          + '&colorDepth=' + encodeURIComponent(colorDepth);

document.write('<img src="http://yourdomain.com/track.gif' + query + '" />');

Then, in yourdomain.com, you have to map a servlet on the image URL:

<servlet>
    <servlet-name>trackServlet</servlet-name>
    <servlet-class>com.example.TrackServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>trackServlet</servlet-name>
    <url-pattern>/track.gif</url-pattern>
</servlet-mapping>

In the doGet() method of the servlet you can gather all information and finally return a real 1x1 gif image:

private static final byte[] GIF = {
    71, 73, 70, 56, 57, 97, 1, 0, 1, 0, -16, 0, 0, 0, 0, 0, 0, 0, 0, 33, -7,
    4, 1, 0, 0, 0, 0, 44, 0, 0, 0, 0, 1, 0, 1, 0, 0, 2, 2, 68, 1, 0, 59
};

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // Gather JS-collected parameters.
    String requestURI = request.getParameter("requestURI");
    String referrer = request.getParameter("referrer");
    String resolution = request.getParameter("resolution");
    String colorDepth = request.getParameter("colorDepth");
    // ...

    // Gather server-collected parameters.
    String remoteAddr = request.getRemoteAddr();
    String userAgent = request.getHeader("user-agent");
    // ...

    // Send 1x1 transparent gif (and disable browser caching on it!)
    response.setHeader("Content-Type", "image/gif");
    response.setHeader("Cache-Control", "no-cache,no-store,must-revalidate");
    response.setHeader("Pragma", "no-cache");
    response.setDateHeader("Expires", 0);
    response.getOutputStream().write(GIF);
}
私野 2024-11-15 18:34:18

假设您使用 PHP 执行此操作:

文件:api.js.php

<?php

    // get user ip and do something with it
    $ip = $_SERVER['REMOTE_ADDR'];

    // since we're in a PHP file, we need to tell the client it's actually JS
    header('Content-Type: application/javascript',true);

?>

// your regular JS folows here
alert('Hi there');

Let's say you're doing this with PHP:

File: api.js.php

<?php

    // get user ip and do something with it
    $ip = $_SERVER['REMOTE_ADDR'];

    // since we're in a PHP file, we need to tell the client it's actually JS
    header('Content-Type: application/javascript',true);

?>

// your regular JS folows here
alert('Hi there');
柠檬心 2024-11-15 18:34:18

您可以只放置一个像这样的图像标签

<img href="yourserver.com/the-servlet-path" />

并根据该请求提供图像
那么你不需要分发脚本,

有关请求用户的信息都在 servlet api 中

you can just put an image tag like this

<img href="yourserver.com/the-servlet-path" />

and serve the image from that request
then you dont need to distribute a script

the information about the requesting user is all in the servlet api

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文