Archive for 十二月, 2003


Tomcat4/5+IIS 用jk2连接方法

第一步,点击这里下载jk2 connector,解压到{Tomcat_Home}/conf/connector 目录下


第二步,打开IIS控制台,选择网站属性,ISAPI筛选器->添加,名称添加jakarta,执行文件找到isapi_redirector2.dll确定;增加一名为jakarta的虚拟路径指向{Tomcat_Home}/conf/connector,设置为“脚本和可执行文件”


第三步,编辑workers2.properties文件内容可能如下


[shm]
# 修改成向对应你的路径
file=d:/Tomcat/logs/jk2.log     
size=1048576


# Example socket channel, override port and host.
[channel.socket:localhost:8009]
# 这里对应Server.xml里面定义的端口
# 如果是默认设置不要更改

port=8009
host=127.0.0.1


# define the worker
[ajp13:reynir_net:8009]
channel=channel.socket:localhost:8009


# Uri mapping
# 映射路径
# 这里是Tomcat下面的路径映射到IIS的方法
# 需要把哪个目录放到IIS下就要写几个

[uri:/*.jsp]
[uri:/jsp-examples/*]
[uri:/my/*]
worker=ajp13:localhost:8009


# define the worker
[status:status]


# Uri mapping
[uri:/jkstatus/*]
worker=status:status


第四步,编辑isapi_redirect.2.0.reg文件,内容可能如下

Windows Registry Editor Version 5.00


[HKEY_LOCAL_MACHINE\SOFTWARE\Apache Software Foundation\Jakarta Isapi Redirector]


[HKEY_LOCAL_MACHINE\SOFTWARE\Apache Software Foundation\Jakarta Isapi Redirector\2.0]
“serverRoot”=”d:\\Tomcat”
“extensionUri”=”/jakarta/isapi_redirector2.dll”
“workersFile”=”d:\\Tomcat\\conf\\connector\\workers2.properties”
“logLevel”=”DEBUG”

几个路径改成相对应你的系统下的路径


第五步,在IIS下添加虚拟目录,比如指向”D:\Tomcat\webapps\jsp-examples”(添加的这个路径必须在workers2.properties里面的映射定义过),路径名为”jsp-examples”


最后,重启tomcat和IIS(要重启整个www服务),ok现在访问 “http://localhost/jsp-examples”看看是不是出来了

一些有用的java资源网站

类库网站
生成图表
http://www.ve.com/download.html?ref=gad5
一个Gif/JEPG Encoder
http://www.acme.com/java/software/Acme.JPM.Encoders.GifEncoder.html


Knowledge 网站
http://www.chinajavaworld.net/
http://www.javaworld.net/
http://www.jspinsider.com/
http://www.javaresearch.org/


 

在tomcat中使用数据库连接池

server.xml定义上下文,<Host>标签中
     <Context path=”/publish” docBase=”publish” debug=”5″ reloadable=”true” crossContext=”true”>
     <Resource name=”jdbc/publish_sqlserver” auth=”SERVLET” type=”javax.sql.DataSource”/>
     <ResourceParams name=”jdbc/publish_sqlserver”>
      <parameter>
       <name>factory</name>
       <value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
      </parameter>
      <!– Maximum number of dB connections in pool. Make sure you
         configure your mysqld max_connections large enough to handle
         all of your db connections. Set to 0 for no limit.
         –>
      <parameter>
       <name>maxActive</name>
       <value>100</value>
      </parameter>
      <!– Maximum number of idle dB connections to retain in pool.
         Set to 0 for no limit.
         –>
      <parameter>
       <name>maxIdle</name>
       <value>30</value>
      </parameter>
      <!– Maximum time to wait for a dB connection to become available
         in ms, in this example 10 seconds. An Exception is thrown if
         this timeout is exceeded.  Set to -1 to wait indefinitely.
         –>
      <parameter>
       <name>maxWait</name>
       <value>1000</value>
      </parameter>
      <!– MySQL dB username and password for dB connections  –>
      <parameter>
       <name>username</name>
       <value>sa</value>
      </parameter>
      <parameter>
       <name>password</name>
       <value>secret</value>
      </parameter>
      <!– Class name for mm.mysql JDBC driver –>
      <parameter>
       <name>driverClassName</name>
       <value>com.jnetdirect.jsql.JSQLDriver</value>
      </parameter>
      <!– The JDBC connection url for connecting to your MySQL dB.
         The autoReconnect=true argument to the url makes sure that the
         mm.mysql JDBC Driver will automatically reconnect if mysqld closed the
         connection.  mysqld by default closes idle connections after 8 hours.
         –>
      <parameter>
       <name>url</name>
       <value>jdbc:JSQLConnect://192.168.1.108:1433/database=repent/applicationName=sWPS2.0</value>
      </parameter>
     </ResourceParams>
    </Context>


web application的web.xml中定义使用的资源
 <resource-ref>
      <description>DB Connection</description>
      <res-ref-name>jdbc/publish_sqlserver</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Container</res-auth>
  </resource-ref>


redeploy web application后,如果数据库参数没有错误,可以按如下方法使用
<%@page import=”java.util.*”%>
<%@page import=”javax.naming.*”%>
<%@page import=”javax.sql.*”%>
<pre>
<%
 Context initialcontext = new InitialContext();
 Context envContext  = (Context)initialcontext.lookup(“java:comp/env”);
 DataSource dataSource = (DataSource)envContext.lookup(“jdbc/publish_sqlserver”);
 Connection connection = null;
 Statement stmt = null;
 
 if(dataSource!=null)
  connection = dataSource.getConnection();  


 if (connection != null) {
  stmt = connection.createStatement();
 }


 connection.close();
 stmt.close(); 
%>
</pre>

Log4j 简易使用指南

依据log4j的mannual中在servlet应用的一个例子


Log4jInit
package com.foo;


import org.apache.log4j.PropertyConfigurator;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.io.IOException;


public class Log4jInit extends HttpServlet {


  public
  void init() {
    String prefix =  getServletContext().getRealPath(“/”);
    String file = getInitParameter(“log4j-init-file”);
    // if the log4j-init-file is not set, then no point in trying
    if(file != null) {
      PropertyConfigurator.configure(prefix+file);
    }
  }


  public
  void doGet(HttpServletRequest req, HttpServletResponse res) {
  }
}


web.xml
  <servlet>
    <servlet-name>log4j-init</servlet-name>
    <servlet-class>com.foo.Log4jInit</servlet-class>


    <init-param>
      <param-name>log4j-init-file</param-name>
      <param-value>WEB-INF/classes/log4j.lcf</param-value>
    </init-param>


    <load-on-startup>1</load-on-startup>
  </servlet>


log4j.lcf
log4j.rootLogger=debug, stdout, R


log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout


# Pattern to output the caller’s file name and line number.
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) – %m%n
#R 输出到文件
log4j.appender.R=org.apache.log4j.RollingFileAppender
log4j.appender.R.File=d:/log4j.log


log4j.appender.R.MaxFileSize=100KB
# Keep one backup file
log4j.appender.R.MaxBackupIndex=1


log4j.appender.R.layout=org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern=[%p] %d{yyyy-MM-dd HH:mm:ss} %x %t %c – %m%n


In any class u can use like this
   Logger log = Logger.getLogger(this.getClass().getName());
   log.error(se);


When shut down
LogManager.shutdown();

DHTML高级使用: Event Hook

代码来源: 微软出版社出版<Inside DHTML>


<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.0 Transitional//EN”>
<html>
 <head>
  <title>Event Hook</title>
  <meta name=”GENERATOR” content=”Microsoft Visual Studio.NET 7.0″>
  <meta name=”vs_targetSchema” content=”http://schemas.microsoft.com/intellisense/ie5″>
  <style>
   .out {text-decoration: none}
   .over {text-decoration: underline}
  </style>
  <script>
   function hookupEvents(){
    var bindings = document.all.tags(“BINDEVENT”);
    for(var intLoop = 0; intLoop < bindings.length; intLoop++){
     var bind = bindings[intLoop];
     if(( null != bind.getAttribute(“event”)) &&
        (null != bind.getAttribute(“action”))){
       
        var bEvent = bind.getAttribute(“event”);
        var bAction = new Function( “return ” +
                    bind.getAttribute(“action”) +
                    ”(arguments[0]);”);
        var bCondition = ( null == bind.getAttribute(“condition”)) ?
               null:
               new Function( “return “+
                      bind.getAttribute(“condition”) +
                      ”(arguments[0]);” );
       var bTree = ( “walk” == bind.getAttribute(“tree”));
       var bSrc = bind.getAttribute(“for”);
       //alert(bCondition);
       register( bEvent, bAction, bCondition, bTree, bSrc);
     }
    }
   }
   
   function register(eventName, action){
    var eventSrc = ( null != arguments[4]) ?
            document.all[arguments[4]] :
            document;
    if( null == eventSrc.manager)
     eventSrc.manager = new Object;
    if( null == eventSrc.manager[eventName] ) {
     eventSrc.manager[eventName] = new Object;
     eventSrc.manager[eventName].length = 0;
     setupHandler(eventName, eventSrc);
    }
    
    var ct = eventSrc.manager[eventName].length++;
//    alert(eventSrc.manager[eventName].length);
    eventSrc.manager[eventName][ct] = new Object;
    eventSrc.manager[eventName][ct].doAction = action;
    eventSrc.manager[eventName][ct].condition =
     (null != arguments[2]) ? arguments[2] : alwaysTrue;
    eventSrc.manager[eventName][ct].doTree =
     (null != arguments[3]) ? arguments[3] : false;    
   }
   
   function setupHandler(eventName, eventSrc){
    eventSrc[eventName] = new Function( “runHandler( ‘” + eventName + “‘, this);”);
//    alert(eventSrc[eventName]);
   }
   
   function alwaysTrue(){
    return true;
   }
   
   function runHandler(eventName, eventSrc){
    var src = event.srcElement;
    for(var intLoop = 0; intLoop < eventSrc.manager[eventName].length; intLoop++){
//     alert(eventName+”:”+intLoop+”:”+eventSrc.manager[eventName][intLoop].condition);
     if( eventSrc.manager[eventName][intLoop].condition(src) )
      eventSrc.manager[eventName][intLoop].doAction(src);
    }
    
    src = src.parentElement;
    
    var top = ( this.tagName == null) ? “HTML” : this.tagName;
    while( top != src.tagName){
     for( var intLoop=0; intLoop < eventSrc.manager[eventName].length; intLoop++)
      if( eventSrc.manager[eventName][intLoop].condition(src) &&
       eventSrc.manager[eventName][intLoop].doTree)
       eventSrc.manager[eventName][intLoop].doAction(src);
       
     src = src.parentElement;
    }
   }
   
   //////////////////////////demostration****************************************************************************************
   
   function swapEffects (src){
    if( null != src.getAttribute(“effect”)){
     var tempClass = src.className;
     src.className = src.getAttribute(“effect”);
     src.setAttribute(“effect”, tempClass);
    }
   }
   
   function checkEffect(src){
    return (src.getAttribute(“effect”) !=null);
   }
   
   function swapText(src){
    src.innerText = “mouse over”;
   }
   window.onload = hookupEvents;
  </script>
 </head>
 <body >something not important
 <BINDEVENT event=”onmouseover” action=”swapEffects” condition=”checkEffect” tree=”walk” for=”myLink”></BINDEVENT>
<!–<BINDEVENT event=”onmouseover” action=”swapText” tree=”walk” for=”myLink”></BINDEVENT>–>
 <BINDEVENT event=”onmouseout” action=”swapEffects” condition=”checkEffect” tree=”walk” for=”myLink”></BINDEVENT>


 <font effect=over tree=”walk”>aaa <a href=# id=myLink effect=”over” class=”out”>mouse over <b>a</b>nd out effect</a></font>
 </body>
</html>


show effects of the code above

Sign a JAR

在applet中使用超过沙箱权限时可以通过对applet签名弹出用户确认窗口打开限制:


> keytool -genkey -alias mars -keypass pwdformars -keystore marsstore
> jar cvf Signed.jar *.class
> jarsigner -keystore marsstore -signedjar mars.jar Signed.jar mars


然后在applet中加入


<PARAM NAME = ARCHIVE VALUE = “mars.jar” >


详细参考:
http://www.mhdn.net/p/2002-11-06/6240.html
http://java.sun.com/security/signExample12/

web.xml quick reference

dtd类型
Servlet2.3
<!DOCTYPE web-app
    PUBLIC “-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN”
    “http://java.sun.com/dtd/web-app_2_3.dtd”>
or
Servlet2.4
<web-app xmlns=”http://java.sun.com/xml/ns/j2ee”
    xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”
    xsi:schemaLocation=”http://java.sun.com/xml/ns/j2ee web-app_2_4.xsd”
    version=”2.4″>
</web-app>


顶级元素
<webapp></webapp>


servlet元素
基本
<servlet>
  <servlet-name>你给servlet起的名字</servlet-name>
  <servlet-class>对照在WEB-INF/classes中的类名称</servlet-class>
 </servlet>
DTD定义
<!ELEMENT servlet (icon?, servlet-name, display-name?, description?, (servlet-class | jsp-file), init-param*, load-on-startup?, run-as?, security-role-ref*)>
init-param:初始化参数,servlet可以从这里得到初始参数值。(String initial = getInitParameter(“initial”);    )
load-on-startup:webapplication启动即装载。


servlet-mapping元素
 <servlet-mapping>
  <servlet-name>servlet名称</servlet-name>
  <url-pattern>映射url, i.e. /* or /hello.html</url-pattern>
 </servlet-mapping>


设置Session
 <session-config>
  <session-timeout>
            1 <!– minutes –>
  </session-timeout>
 </session-config>


HTTP认证
<web-app>
    <servlet>
        <servlet-name>
            secret
        </servlet-name>
        <servlet-class>
            SalaryServer
        </servlet-class>
    </servlet>


    <security-constraint>
        <web-resource-collection>
            <web-resource-name>
                SecretProtection
            </web-resource-name>
            <url-pattern>
                /servlet/*
            </url-pattern>
            <http-method>
                GET
            </http-method>
            <http-method>
                POST
            </http-method>
        </web-resource-collection>
        <auth-constraint>
            <role-name>
                engineer
            </role-name>
        </auth-constraint>
    </security-constraint>


    <login-config>
        <auth-method>
            BASIC       <!– BASIC, DIGEST, FORM, CLIENT-CERT –>
        </auth-method>                                              
        <realm-name>                                                
            Default     <!– optional, only useful for BASIC –>    
        </realm-name>                                               
    </login-config>                                                 


    <security-role>
        <role-name>
            engineer
        </role-name>
    </security-role>
</web-app>

编程土鳖

    到这个公司4个月了,不知道还能待多久,我已经厌倦了最底层的工作,至少物质生活的刺激让我觉得不能再这样,但我至少希望在这里能看到一些我想看到的东西,见识一些未曾接触的东西再考虑下一步。但现在还没有。我理解了程序员就是驴这句话,尤其是在这种有一点规模的公司,分工更细的公司,发挥自己的想象力不如快速机械的完成一件流程工作,不过至今我也还没做过什么具体工作,不知道为什么我总是扮演闲人的角色,难道是态度不够积极?不过我确实不想碰那些无聊的东西。


    眼高手低就是我,想到的永远比能做到的多,有好几次想开始做个什么东西,但总是在不能解决最终的问题、困扰我的问题而在一次又一次思考后抛弃掉。


    烦恼,感觉很烦,很久没有体验过升级的快感了,象是困在游戏迷宫中一样,不知道往哪个方向去才是正途。其实这个问题一直存在,以前有个不清晰、高远的目标,那时会为那目标而兴奋,当淡忘掉之后,才发现自己没有一个具体的方向,不为名不为利的目标实在无法为之去努力实践。


    除了编程,我对其他的东西现在似乎都知道的很少了,而对于编程,想知道的又太多,网络、3D和多媒体、数据库、无线设备每一个都是要积数年之功才能有所成就的领域,真是很羡慕有天龙八部里无涯子那种精通百艺的人。虽然编程有一通百通的说法,但有很多东西是需要行业知识去解决的,现在想来,要靠自己一人之力完成任何一个像样的作品都几乎是不可能的。


    如果程序员可以分为若干级别,我想我还属于土鳖一类,但是经过十数次小小的升级之后我觉得,也许一次大的蜕变要来了,感觉象是体内不成形的力量逐渐要聚集起来,却不知道如何释放出来,也许只是一种感觉,有时高亢有时压抑,也许还要3、4个月时间可以度过这一关。


    我只希望以后我的每一步都能得到相当付出的回报,我希望自己能努力的实现我的每个目标,能再一两年内再把对编程的感觉提高到一个新的层次,我不想再当个土鳖,或许以前也是一段美好的时光,但是现在需要改变,所有的东西。

栏目说明

栏目介绍

Powered by WordPress. Theme: Motion by 85ideas.