Benny's Blog
25. April 2012

This is how you can show the user agent with Server Side Includes:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta charset="utf-8" />
    <title>SSI Page</title>
  </head>
  <body>
    <!--#echo var="HTTP_USER_AGENT" -->
  </body>   
</html>
24. April 2012

This is how you can show the user agent in JavaServer Pages (JSP):

Version 1

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>JSP Page</title>
  </head>
  <body>
    <%
      String userAgent = request.getHeader("user-agent");
      out.print(userAgent);
    %>
  </body>
</html>

Version 2

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>JSP Page</title>
  </head>
  <body>
    <%
      String userAgent = request.getHeader("user-agent");
    %>
    <%= userAgent %>
  </body>
</html>

Version 3

<%@taglib uri='http://java.sun.com/jsp/jstl/core' prefix='c'%>
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>JSP Page</title>
  </head>
  <body>
  <c:out value="${header['User-Agent']}" />
</body>
</html>

Version 4

<%@taglib uri='http://java.sun.com/jsp/jstl/core' prefix='c'%>
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>JSP Page</title>
  </head>
  <body>
  <c:set var="userAgent" scope="page" value="${header['User-Agent']}"/>
  <c:out value="${userAgent}" />
</body>
</html>

Version 5

<%@taglib uri='http://java.sun.com/jsp/jstl/core' prefix='c'%>
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>JSP Page</title>
  </head>
  <body>
  <c:set var="userAgent" scope="page" value="${header['User-Agent']}"/>
  <%=pageContext.getAttribute("userAgent")%>
</body>
</html>
23. April 2012

If you want a jQuery UI slider with labels that looks like this:

Then this JavaScript code might help you:

$('#slider').slider({
  min: 1,
  max: 20,
  step: 1,
  value: 16,
  create: function(event,ui){
    $('a.ui-slider-handle').css({'text-decoration':'none','text-align':'center'}).text($(this).slider("value"));
  },
  slide: function(event,ui){
    $('a.ui-slider-handle').css({'text-decoration':'none','text-align':'center'}).text(ui.value);
  }
});
22. April 2012

The syntax of JavaScript and PHP is very similar. How similar is shown by the following function that I wrote to generate a random password:

PHP code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php
function randomPassword($length){
  $password = "";
  for($i=0; $i<$length; $i++){		
    $func = rand(0,2);
    if($func == 0)
      $password = $password.chr(rand(48,57));
    else if($func == 1)
      $password = $password.chr(rand(65,90));
    else if($func == 2)
      $password = $password.chr(rand(97,122));
  }
  return $password;
}
 
$randomPassword = randomPassword(20);
echo($randomPassword);
?>

JavaScript code:

<script type="text/javascript" line="1">
function rand(min,max){
  if (arguments.length === 0){
    min = 0;
    max = 32767;
  }
  return Math.floor(Math.random() * (max - min + 1)) + min;
}
 
String.prototype.chr = function(code){
  return String.fromCharCode(code);
}
 
function randomPassword($length){
  $password = "";
  for($i=0; $i<$length; $i++){		
    $func = rand(0,2);
    if($func == 0)
      $password += $password.chr(rand(48,57));
    else if($func == 1)
      $password += $password.chr(rand(65,90));
    else if($func == 2)
      $password += $password.chr(rand(97,122));
  }
  return $password;
}
 
$randomPassword = randomPassword(20);
document.write($randomPassword);
</script>
21. April 2012

The lower case letter “a” has ASCII code “97″ and can be generated in PHP like this:

<?php
  $a = chr(97);
  echo $a;
?>

In JavaScript, the same can be accomplished by:

<script type="text/javascript">
  var a = String.fromCharCode(97);
  document.write(a);
</script>

And this is how it works in Java:

public static void main(String[] args){
  String a = new Character((char) 97).toString();
  System.out.println(a);
}
20. April 2012

In PHP the rand() function returns a random number between a given minimum and maximum (by default 0 and 32767). The whole looks like this:

<?php
  $randomNumber = rand(1,100); // random number between 1 and 100
  echo $randomNumber;
?>

The same function can be rebuild with JavaScript:

<script type="text/javascript">
  function rand(min,max){
    if (arguments.length === 0){
      min = 0;
      max = 32767;
    }
    return Math.floor(Math.random() * (max - min + 1)) + min;
  }
 
  var randomNumber = rand(1,100); // random number between 1 and 100
  document.write(randomNumber);
</script>