Benny's Blog
Navigation: Home » Archives for JEE
26. April 2012

This is a good example to dynamically include a header file in JavaServer Pages (JSP):

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<!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:choose>
    <c:when test="${fn:contains(userAgent,'iPhone')}">
      <%@include file="header_iphone.jsp" %>
    </c:when>
    <c:when test="${fn:contains(userAgent,'iPad')}">
      <%@include file="header_ipad.jsp" %>
    </c:when>
    <c:when test="${fn:contains(userAgent,'Android')}">
      <%@include file="header_android.jsp" %>
    </c:when>
    <c:otherwise>
      <%@include file="header_other.jsp" %>
    </c:otherwise>
  </c:choose>
</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>
13. April 2012

I played around with some Server Side Includes using org.apache.catalina.ssi.SSIServlet and noticed that some statements (which were fine on Apache) are not working on GlassFish. Finally I found the solution.

In Apache (using mod_ssi) you can do this:

<!--#if expr="{$HTTP_USER_AGENT} == /(iPhone)/" -->
<p>It's an iPhone.</p>
<!--#endif -->

But on GlassFish Server (with SSIServlet) you have to do this:

<!--#if expr="{$HTTP_USER_AGENT} = '/(iPad)/'" -->
<p>It's an iPhone.</p>
<!--#endif -->
10. April 2012

In a production site you always want to have minified and obfuscated code (i.e. CSS and JavaScript files) so that the file content is as small as possible because this will save bandwidth and loading time. The best known and easiest tool for this is the YUI Compressor from Yahoo!.

In a Java Enterprise application, the YUI Compressor can be integrated very easy with Maven. All you need is the Maven Minify Plugin. That’s how to use it:

…weiterlesen

4. April 2012
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public static void main(String[] args)
{
  String[] firstNames =
  {
    "Marge", "Homer", "Lisa", "Bart", "Maggie"
  };
 
  System.out.println("- With for loop:");
 
  for (int i = 0; i < firstNames.length; i++)
  {
    System.out.println(firstNames[i]);
  }
 
  System.out.println("- With foreach loop");
 
  for (String name : firstNames)
  {
    System.out.println(name);
  }
}
Tags: Java, , , , , , ,
1. April 2012

If you are used to MySQL then you probably know the MySQL function NOW() which inserts the current date and time in a MySQL query. But if you use JDBC and prepared statements, then you can reconstruct this function with a GregorianCalendar (which is the successor of Date):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
public void insertArticle(Article article) throws SQLException
{
  //query = "INSERT INTO articles(title,content,date) VALUES (?,?,NOW())";
  query = "INSERT INTO articles(title,content,date) VALUES (?,?,?)";
 
  statement = connection.prepareStatement(query);
 
  statement.setString(1, article.getTitle());
  statement.setString(2, article.getContent());
  statement.setTimestamp(3, new Timestamp(new GregorianCalendar().getTimeInMillis()));
 
  logger.info(statement);
  statement.executeUpdate();
}