Tuesday, 14 July 2009

Servlet Filter

FilterChain is an object provided by the servlet containner to the developer. Filters can use FilterChain to invoke the next filter, or invoking the resources as at the end of filter chain.

Redirecting a request

Another use for filters is to restrict access to resources - if a filter doesn't call FilterChain.doFilter(), the resource won't be loaded. Simply returning would send an empty document to the browser, so it's better to redirect or forward the request to a different resource. As with a servlet, there are two ways to do this. HttpServletReponse.sendRedirect() actually sends a response to the browser giving it a URL to fetch instead of the original one, which may be on a different server. The other option is to use a RequestDispatcher to load a different resource on the server instead of the resource which would have been loaded, transparently to the browser.

Unfortunately the Tomcat project doesn't provide any useful examples for this, so I'm reduced to writing my own code. SecureFilter checks the current session to see whether there is a User object; if not, it forwards the request to a login form. While hardly a robust component, you can see how a more useful system could be implemented.

public void doFilter(ServletRequest request,
ServletResponse response,
FilterChain chain)
throws IOException, ServletException
{
boolean authorized = false;
if (request instanceof HttpServletRequest) {
HttpSession session = ((HttpServletRequest)request).getSession(false);
if (session != null) {
User user = (User) session.getAttribute("user");
if (user != null)
authorized = true;
}
}

if (authorized) {
chain.doFilter(request, response);
return;
} else if (filterConfig != null) {
String login_page = filterConfig.getInitParameter("login_page");
if (login_page != null && !"".equals(login_page)) {
filterConfig.getServletContext().getRequestDispatcher(login_page).
forward(request, response);
return;
}
}

throw new ServletException
("Unauthorized access, unable to forward to login page");

}

Threading issues

Wednesday, 18 March 2009

Difference of Path convention in Unix and Win/Dos

The only difference between them is that where DOS/Windows uses "\" (the famous "backslash") to represent the "root" directory, and as a separator between directory levels, UNIX uses / (Shall we call it "forwardslash"?). This character is found on the same key as the questionmark ("?") on most keyboards.

Thursday, 9 October 2008

Database: Functional Dependencies and Normalization

Database Design Guideline
  1. Design each relation schema so that it is easy to explain its meaning. Intuitively, each schema should correspond to one entity or one relationship type in the real life.
  2. Design relation schema so that no insertion, deletion, or modification anomalies are present.

Tuesday, 9 September 2008

Database

------------------------------------------------------------------------------
------------------------------------------------------------------------------
Entity: an object in the real world that is distinguishable from other objects.
Entity Set: a collection of similar entities.

Attribute:
Key: a minimal set of attributes whose values uniquely identify an entity in the set.
Candidate key: there may be more than one attribute able to identify an entity.
Primary Key: we designate one of them as the primary key.

Relationship: an association among two or more entities.
Descriptive attributes: a relationship can have descriptive attributes.

Monday, 11 August 2008

Usage of NetBean 6.x Editor

Hot keys:
  1. Ctrl + space : Code completion.
  2. Alt + Insert : Generate "getter and setter; constructor; delegate; add property; etc"
  3. Alt + Enter : ???
  4. Ctrl + R : factoring name.
  5. Shift + Esc : enlarge current window.

Tutorial on using NetBean IDE

Persist LOB using JPA in J2EE/J2SE

-----------------------------------------------------------------------------------------------
JPA (Sun persistence standard)==> Persistence Provider (Toplink) ==> JDBC (version 11.)

1) JPA provides a simple solution for persisting LOB in Database. Developer only need to annotate the Entity variable that is a LOB. Looks like that, at least from the document, that I have read.
2) However, persistence manager seems cannot persist a string bigger than 4000 byte. Why?
//------------------------------------------------------------------------------------------------
The reason is that the JDBC driver, I am using, ojdbc14.jar, which is only compatible for jdk1.4 not for jdk1.5. It doesn't support write BLOB into database just like writing in a String. So I download the latest version JDBC driver, ojdbc5.jar, and problem solved at once.
------------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------------
JDBC driver: ojdbc14.
  • Specification-Title: "Oracle JDBC driver classes for use with JDK1.4"
  • Specification-Version: "Oracle JDBC Driver version - 9.0.2.0.0"
JDBC driver 10.2 above, it can treat Clob as if they were strings, with code like:

ps = conn.createPreparedStatement( "update tab set lob =?");
ps.setString(1,"Bob")

The actual way this is done internally depends on the size of String set on the bind-parameter.

Better performance can be squeezed out of this for String between 4000 bytes and 32Kb, using a bit of PL/SQL:

ps = conn.prepareStatement("begin \n
update tab set lob= ? where id = ?;\n
End;")
Ps.setString(1, StringUpTo32K);



Log4j notes


log4j.rootLogger=debug, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d [%t] %-5p %c{1} - %m%n

Log4j提供的appender有以下几种:
  1. org.apache.log4j.ConsoleAppender(控制台)
  2. org.apache.log4j.FileAppender(文件)
  3. org.apache.log4j.DailyRollingFileAppender(每天产生一个日志文件)
  4. org.apache.log4j.RollingFileAppender(文件大小到达指定尺寸的时候产生新文件)
  5. org.apache.log4j.WriterAppender(将日志信息以流格式发送到任意指定的地方)
Log4j提供的layout有以下几种:
  1. org.apache.log4j.HTMLLayout(以HTML表格形式布局),
  2. org.apache.log4j.PatternLayout(可以灵活地指定布局模式),
  3. org.apache.log4j.SimpleLayout(包含日志信息的级别和信息字符串),
  4. org.apache.log4j.TTCCLayout(包含日志产生的时间、线程、类别等等信息)
Log4j 自定义格式
  1. %p 输出优先级,即DEBUG,INFO,WARN,ERROR,FATAL
  2. %r 输出自应用启动到输出该log信息耗费的毫秒数
  3. %c 输出所属的类目,通常就是所在类的全名
  4. %t 输出产生该日志事件的线程名
  5. %n 输出一个回车换行符,Windows平台为“\r\n”,Unix平台为“\n”
  6. %d 输出日志时间点的日期或时间,默认格式为ISO8601,也可以在其后指定格式,比如:%d{yyy MMM dd HH:mm:ss,SSS},输出类似: 2002年10月18日 22:10:28,921
  7. %l 输出日志事件的发生位置,包括类目名、发生的线程,以及在代码中的行数。举例:Testlog4.main(TestLog4.java:10)

Can Jackson Deserialize Java Time ZonedDateTime

Yes, but must include JSR310. Thus ZonedDateTime can be deserialized directly from JSON response to POJO field. <dependency> <g...