如何使用Java代码检查客户端浏览器中是否启用了JavaScript [英] How to check whether JavaScript is enabled in client browser using Java code

查看:105
本文介绍了如何使用Java代码检查客户端浏览器中是否启用了JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以帮我尝试使用Java代码检查客户端浏览器中是否启用了JavaScript。

can anyone help me in trying to check whether JavaScript is enabled in client browser using Java code.

推荐答案

假设你'重新编写Java Web应用程序,我成功使用的一种技术是访问第一个页面—通常是登录表单—在页面加载时编写会话cookie。然后使用表单提交的Java代码来检查该cookie是否存在。

Assuming you're writing a Java web application, one technique that I've used successfully is to have the first page that's accessed—typically a login form—write a session cookie when the page loads. Then have the Java code that the form submits to check for the existence of that cookie.

在客户端:

<script type="text/javascript">
  function createCookie(name, value, days) {
    var expires = "";
    if (days) {
      var date = new Date();
      date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
      expires = "; expires=" + date.toGMTString();
    }
    var cookie = name + "=" + value + expires + "; path=" + "/";
    document.cookie = cookie;
  }
  createCookie("JavaScriptEnabledCheck", 1, 0);
</script>

在服务器上:

/**
 * Returns <code>true</code> if the session cookie set by the login form
 * is not present.
 * 
 * @param request The HTTP request being processed
 * @return <code>true</code> if JavaScript is disabled, otherwise <code>false</code>
 */
private boolean isJavaScriptDisabled(HttpServletRequest request)
{
  boolean isJavaScriptDisabled = true;
  Cookie[] cookies = request.getCookies();

  if (cookies != null)
  {
    for (int i = 0; i < cookies.length; i++)
    {
      if ("JavaScriptEnabledCheck".equalsIgnoreCase(cookies[i].getName()))
      {
        isJavaScriptDisabled = false;
        break;
      }
    }
  }

  return isJavaScriptDisabled;
}

这篇关于如何使用Java代码检查客户端浏览器中是否启用了JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆