页面定时跳转

页面定时跳转是指一个页面在显示了一定的时间之后自动跳转到另外一个页面,实现起来非常容易:

<meta content="3;login.jsp" http-equiv="Refresh" />

以上这行代码实现了3秒之后自动跳转到login.jsp页面,只使用了html的meta标签,比用什么javascript方便多了呢!

Posted in  html


Jsp/Servlet页面跳转

Jsp/Servlet页面跳转有两种,一种是Redirect(页面重定向),另一种是Forward(页面转发)。

Redirect:

response.sendRedirect("success.jsp");

完全跳转到一个新的请求,不共享之前Request中的数据。

Forward:

RequestDispatcher rd = request.getRequestDispatcher("ResultServlet");
rd.forward(request, response);

将当前请求转发到另一个请求中去,后一个请求共享先前一个请求的Request数据。

Posted in  java web


第一天上班

7月7日第一天去欧酷报道。

早上7点半从家里出门, 骑着自行车翻过苏州河,在娄山关路坐地铁二号线抵达张江,第一天去不知道去哪里坐班车,于是慢慢走,走着走着,发现忘记该在哪条马路转弯了,于是打电话求救,还好,九点之前顺利到达。

签订了劳动合同和保密协议,分到Rainbow组,跟着胖胖做开发。不过第一个月要去火车站那里的手机组作采购、编辑和推广工作,呵呵,算是先了解客户的需求。第二个月计划培训,C++, Java, Python, PHP都要学一些,还是蛮不错的。

公司五点就能下班了,算是比较早吧。拿到门禁卡之后去楼下二楼找朱去聊天,然后朱下班之后带我去坐花旗的班车,并告诉了我上班时的班车站点。沿着原路回家,到家也要七点了呢,以后争取六点到家吃饭,哈哈。

Posted in  life


Java连接LDAP服务器

LDAP是轻量目录访问协议,是一个用来发布目录信息到许多不同资源的协议。通常它都作为一个集中的地址本使用。 LDAP是一个比关系数据库抽象层次更高的存贮概念,与关系数据库的查询语言SQL属同一级别。LDAP最基本的形式是一个连接数据库的标准方式。该数据库为读查询作了优化。因此它可以很快地得到查询结果,不过在其它方面,例如更新,就慢得多。

Java连接LDAP服务器可以通过JDK的Context接口。下面定义了两种Context,env是直接查询referral指向的目标,而envIgnoreReferral则忽略referral,可以用来修改referral的值。我用的ldap服务器是openldap

private static Hashtable env = new Hashtable();
private static Hashtable envIgnoreReferral = new Hashtable();

static {
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    env.put(Context.PROVIDER_URL, "ldap://localhost:389");
    env.put("java.naming.ldap.version", "3");
    env.put(Context.SECURITY_AUTHENTICATION, "simple");
    env.put(Context.SECURITY_PRINCIPAL, "cn=Root, c=GB");
    env.put(Context.SECURITY_CREDENTIALS, "pwd");
    env.put("com.sun.jndi.ldap.connect.pool", "true");
    env.put("com.sun.jndi.ldap.connect.pool.prefsize", "50");
    env.put("com.sun.jndi.ldap.connect.pool.maxsize", "100");
    env.put(Context.REFERRAL, "follow");
    env.put("java.naming.ldap.attributes.binary", "userCertificate");

    envIgnoreReferral.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
    envIgnoreReferral.put(Context.PROVIDER_URL, "ldap://localhost:389");
    envIgnoreReferral.put("java.naming.ldap.version", "3");
    envIgnoreReferral.put(Context.SECURITY_AUTHENTICATION, "simple");
    envIgnoreReferral.put(Context.SECURITY_PRINCIPAL, "cn=Root, c=GB");
    envIgnoreReferral.put(Context.SECURITY_CREDENTIALS, "pwd");
    envIgnoreReferral.put("com.sun.jndi.ldap.connect.pool", "true");
    envIgnoreReferral.put("com.sun.jndi.ldap.connect.pool.prefsize", "50");
    envIgnoreReferral.put("com.sun.jndi.ldap.connect.pool.maxsize", "100");
    envIgnoreReferral.put(Context.REFERRAL, "ignore");
    envIgnoreReferral.put("java.naming.ldap.attributes.binary", "userCertificate");
}

同样地道理,我们需要定义两种到ldap服务器的连接

public static LdapContext getContext() throws NamingException {
    LdapContext context = null;
    try {
        context = new InitialLdapContext(env, null);
    } catch (NamingException e) {
        logger.error("Error creating Ldap DirContext");
        throw e;
    }
    return context;
}

public static LdapContext getContextIgnoreReferral() throws NamingException {
    LdapContext context = null;
    try {
        context = new InitialLdapContext(envIgnoreReferral, null);
    } catch (NamingException e) {
        logger.error("Error creating Ldap DirContext");
        throw e;
    }
    return context;
}

同数据库连接一样,必须定义关闭连接的接口,以及时释放系统资源

public static void close(LdapContext context) throws NamingException {
    if (context != null) {
        context.close();
    }
}

测试连接的代码如下,

public static boolean testConnect() {
    LdapContext context = null;
    try {
        context = getContext();
        return true;
    } catch (NamingException e) {
        return false;
    } finally {
        try {
            close(context);
        } catch (NamingException e) {
            return false;
        }
    }
}

具体的增删改查方法就留待下次在讲吧

Posted in  java ldap


sweeper

sweeper插件用于自动生成清扫缓存的sweeper类,监视特定的model,根据相应的事件触发其行动,下面是一个example:

1. 首先生成项目和初始数据:

$ rails test_sweeper
$ cd test_sweeper
$ script/generate scaffold post title:string body:text
#db/migrate/001_create_posts.rb
(1..50).each do |num|
  Post.create(:title => "test#{num}", :body => "Test#{num}")
end
$ rake db:migrate

2. 安装插件:

$ script/plugin install http://topfunky.net/svn/plugins/sweeper

3. 生成Sweeper类,该类在model增删改之后将缓存页面清除:

$ script/generate sweeper post after_save before_destroy
#app/models/post_sweeper.rb
class PostSweeper  ActionController::Caching::Sweeper
  observe Post

  def after_save(record)
    expire_page :controller => 'posts', :action => 'index'
  end

  def before_destroy(record)
    expire_page :controller => 'posts', :action => 'index'
  end
end

4. 在controller中指定缓存页面和sweeper类:

#app/controller/posts_controller.rb
caches_page :index
cache_sweeper :post_sweeper

5. 察看效果:

当第一次访问/posts时,server会查询数据库,render页面,并生成cache page:

Cached page: /posts.html (0.00737)

之后再访问/posts时,server就不会再去查询数据库,render页面,而直接去读cache page 直到我们对post做了增删改中任意一个操作,server会删除被cache的page:

Expired page: /posts.html (0.00014)

然后再访问/posts时,server又会去查询数据库,render页面,并生成cache page

Posted in  rails plugins


Fork me on GitHub