一些对web开发有用的东西
2008-11-12 11:31
405 查看
一、基本配置
基本的重要的配置在web.xml 文件中。
1、Jsp属性组
<jsp-property-group>
<url-pattern>/pages/*</url-pattern>
<el-ignore>true</el-ignore>
<page-encoding>UTF-8</page-encoding>
<include-prelude>/include/header.jspf</include-prelude>
<include-coda>/include/copyright.jspf</include-coda>
</jsp-property-group>
这个设置可以指定页面编码,页头页脚等等。
设置 <page-encoding>UTF-8</page-encoding> 的好处是不用在每个页面像这样指定编码:<%@page contentType="html/text;charset=UTF-8" %>
而设置 <include-prelude>/include/header.jspf</include-prelude> 使得每个页面都在头部包含header.jspf文件(通常把对标签的包含放在这里)。
2、数据库资源的引用
![](http://java.chinaitlab.com/UploadFiles_8734/200612/20061216102949224.gif)
<resource-ref>
![](http://java.chinaitlab.com/UploadFiles_8734/200612/20061216102949224.gif)
<description>CourseDesign JDNI datasource</description>
![](http://java.chinaitlab.com/UploadFiles_8734/200612/20061216102949224.gif)
<res-ref-name>jdbc/test</res-ref-name>
![](http://java.chinaitlab.com/UploadFiles_8734/200612/20061216102949224.gif)
<res-type>javax.sql.DataSource</res-type>
![](http://java.chinaitlab.com/UploadFiles_8734/200612/20061216102949224.gif)
<res-auth>Container</res-auth>
![](http://java.chinaitlab.com/UploadFiles_8734/200612/20061216102949224.gif)
</resource-ref>
前提是要在TOMCAT的中配置
<Context path="/Course" docBase="Course" debug="0" crosscontext="true" reloadable="true">
<Resource name="jdbc/test" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="root" password="123456"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/databaseName?useUnicode=true&characterEncoding=UTF-8" />
</Context>
在程序中可以这样获取连接
![](http://java.chinaitlab.com/UploadFiles_8734/200612/20061216102949224.gif)
public static Connection getConnection()
![](http://java.chinaitlab.com/UploadFiles_8734/200612/20061216102949374.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200612/20061216102949624.gif)
...{
![](http://java.chinaitlab.com/UploadFiles_8734/200612/20061216102950381.gif)
Connection conn=null;
![](http://java.chinaitlab.com/UploadFiles_8734/200612/20061216102950381.gif)
try
![](http://java.chinaitlab.com/UploadFiles_8734/200612/20061216102950943.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200612/20061216102950615.gif)
...{
![](http://java.chinaitlab.com/UploadFiles_8734/200612/20061216102950381.gif)
Context initContext = new InitialContext();
![](http://java.chinaitlab.com/UploadFiles_8734/200612/20061216102950381.gif)
Context envContext = (Context)initContext.lookup("java:/comp/env");
![](http://java.chinaitlab.com/UploadFiles_8734/200612/20061216102950381.gif)
DataSource ds = (DataSource)envContext.lookup("jdbc/test");
![](http://java.chinaitlab.com/UploadFiles_8734/200612/20061216102950381.gif)
conn = ds.getConnection();
![](http://java.chinaitlab.com/UploadFiles_8734/200612/20061216102950943.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200612/20061216102950615.gif)
}catch(Exception e)...{ }
![](http://java.chinaitlab.com/UploadFiles_8734/200612/20061216102950381.gif)
return conn;
![](http://java.chinaitlab.com/UploadFiles_8734/200612/20061216102950629.gif)
}
3、过滤器
一般来说,字符编码的处理,我们会写一个过滤器。这个过滤器的JAVA类在TOMCAT的例子中有提供,可以按需来更改再拿来用。只要在配置文件中设置:
<filter-name>setCharacterEncoding</filter-name>
<filter-class>powerwind.filter.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>setCharacterEncoding</filter-name>
<url-pattern>/pages/*</url-pattern>
</filter-mapping>
4、标签的URI
JSTL是个东西,里面提供了很好用的标签(Tag),但也不一定满足我们的要求,就自己写标签了。把 *.tld 文件直接放到WEB-INF下,在自己定义的tld文件中加上<uri>元素,如:<uri>http://powerwind/course</uri> 。
5、日志
只用过log4j这个日志包。首先是配置文件 log4j.properties (比较完整的配置,应根据情况选择):
![](http://java.chinaitlab.com/UploadFiles_8734/200612/20061216102949224.gif)
log4j.rootLogger = DEBUG,INFO, A1, A2,A3
![](http://java.chinaitlab.com/UploadFiles_8734/200612/20061216102949224.gif)
log4j.appender.A1 = org.apache.log4j.ConsoleAppender
![](http://java.chinaitlab.com/UploadFiles_8734/200612/20061216102949224.gif)
log4j.appender.A1.layout = org.apache.log4j.PatternLayout
![](http://java.chinaitlab.com/UploadFiles_8734/200612/20061216102949224.gif)
log4j.appender.A1.layout.ConversionPattern = %4p [%t] (%F:%L) - %m%n
![](http://java.chinaitlab.com/UploadFiles_8734/200612/20061216102949224.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200612/20061216102949224.gif)
log4j.appender.A2 = org.apache.log4j.RollingFileAppender
![](http://java.chinaitlab.com/UploadFiles_8734/200612/20061216102949224.gif)
log4j.appender.A2.File =../../log/test.log
![](http://java.chinaitlab.com/UploadFiles_8734/200612/20061216102949224.gif)
log4j.appender.A2.MaxFileSize = 1KB
![](http://java.chinaitlab.com/UploadFiles_8734/200612/20061216102949224.gif)
log4j.appender.A2.MaxBackupIndex = 3
![](http://java.chinaitlab.com/UploadFiles_8734/200612/20061216102949224.gif)
log4j.appender.A2.layout = org.apache.log4j.PatternLayout
![](http://java.chinaitlab.com/UploadFiles_8734/200612/20061216102949224.gif)
log4j.appender.A2.layout.ConversionPattern = %d{yyyy-MM-dd hh:mm:ss}:%p %t %c - %m%n
![](http://java.chinaitlab.com/UploadFiles_8734/200612/20061216102949224.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200612/20061216102949224.gif)
log4j.appender.A3=org.apache.log4j.jdbc.JDBCAppender
![](http://java.chinaitlab.com/UploadFiles_8734/200612/20061216102949224.gif)
log4j.appender.A3.URL= jdbc:mysql://localhost:3306/log4jTest
![](http://java.chinaitlab.com/UploadFiles_8734/200612/20061216102949224.gif)
log4j.appender.A3.driver= com.mysql.jdbc.Driver
![](http://java.chinaitlab.com/UploadFiles_8734/200612/20061216102949224.gif)
log4j.appender.A3.user= root
![](http://java.chinaitlab.com/UploadFiles_8734/200612/20061216102949224.gif)
log4j.appender.A3.password= 123456
![](http://java.chinaitlab.com/UploadFiles_8734/200612/20061216102949224.gif)
log4j.appender.A3.layout = org.apache.log4j.PatternLayout
![](http://java.chinaitlab.com/UploadFiles_8734/200612/20061216102949224.gif)
log4j.appender.A3.layout.ConversionPattern = INSERT INTO log4j (createDate, thread, level, class, message)
基本的重要的配置在web.xml 文件中。
1、Jsp属性组
<jsp-property-group>
<url-pattern>/pages/*</url-pattern>
<el-ignore>true</el-ignore>
<page-encoding>UTF-8</page-encoding>
<include-prelude>/include/header.jspf</include-prelude>
<include-coda>/include/copyright.jspf</include-coda>
</jsp-property-group>
这个设置可以指定页面编码,页头页脚等等。
设置 <page-encoding>UTF-8</page-encoding> 的好处是不用在每个页面像这样指定编码:<%@page contentType="html/text;charset=UTF-8" %>
而设置 <include-prelude>/include/header.jspf</include-prelude> 使得每个页面都在头部包含header.jspf文件(通常把对标签的包含放在这里)。
2、数据库资源的引用
![](http://java.chinaitlab.com/UploadFiles_8734/200612/20061216102949224.gif)
<resource-ref>
![](http://java.chinaitlab.com/UploadFiles_8734/200612/20061216102949224.gif)
<description>CourseDesign JDNI datasource</description>
![](http://java.chinaitlab.com/UploadFiles_8734/200612/20061216102949224.gif)
<res-ref-name>jdbc/test</res-ref-name>
![](http://java.chinaitlab.com/UploadFiles_8734/200612/20061216102949224.gif)
<res-type>javax.sql.DataSource</res-type>
![](http://java.chinaitlab.com/UploadFiles_8734/200612/20061216102949224.gif)
<res-auth>Container</res-auth>
![](http://java.chinaitlab.com/UploadFiles_8734/200612/20061216102949224.gif)
</resource-ref>
前提是要在TOMCAT的中配置
<Context path="/Course" docBase="Course" debug="0" crosscontext="true" reloadable="true">
<Resource name="jdbc/test" auth="Container" type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="root" password="123456"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/databaseName?useUnicode=true&characterEncoding=UTF-8" />
</Context>
在程序中可以这样获取连接
![](http://java.chinaitlab.com/UploadFiles_8734/200612/20061216102949224.gif)
public static Connection getConnection()
![](http://java.chinaitlab.com/UploadFiles_8734/200612/20061216102949374.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200612/20061216102949624.gif)
...{
![](http://java.chinaitlab.com/UploadFiles_8734/200612/20061216102950381.gif)
Connection conn=null;
![](http://java.chinaitlab.com/UploadFiles_8734/200612/20061216102950381.gif)
try
![](http://java.chinaitlab.com/UploadFiles_8734/200612/20061216102950943.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200612/20061216102950615.gif)
...{
![](http://java.chinaitlab.com/UploadFiles_8734/200612/20061216102950381.gif)
Context initContext = new InitialContext();
![](http://java.chinaitlab.com/UploadFiles_8734/200612/20061216102950381.gif)
Context envContext = (Context)initContext.lookup("java:/comp/env");
![](http://java.chinaitlab.com/UploadFiles_8734/200612/20061216102950381.gif)
DataSource ds = (DataSource)envContext.lookup("jdbc/test");
![](http://java.chinaitlab.com/UploadFiles_8734/200612/20061216102950381.gif)
conn = ds.getConnection();
![](http://java.chinaitlab.com/UploadFiles_8734/200612/20061216102950943.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200612/20061216102950615.gif)
}catch(Exception e)...{ }
![](http://java.chinaitlab.com/UploadFiles_8734/200612/20061216102950381.gif)
return conn;
![](http://java.chinaitlab.com/UploadFiles_8734/200612/20061216102950629.gif)
}
3、过滤器
一般来说,字符编码的处理,我们会写一个过滤器。这个过滤器的JAVA类在TOMCAT的例子中有提供,可以按需来更改再拿来用。只要在配置文件中设置:
<filter-name>setCharacterEncoding</filter-name>
<filter-class>powerwind.filter.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>setCharacterEncoding</filter-name>
<url-pattern>/pages/*</url-pattern>
</filter-mapping>
4、标签的URI
JSTL是个东西,里面提供了很好用的标签(Tag),但也不一定满足我们的要求,就自己写标签了。把 *.tld 文件直接放到WEB-INF下,在自己定义的tld文件中加上<uri>元素,如:<uri>http://powerwind/course</uri> 。
5、日志
只用过log4j这个日志包。首先是配置文件 log4j.properties (比较完整的配置,应根据情况选择):
![](http://java.chinaitlab.com/UploadFiles_8734/200612/20061216102949224.gif)
log4j.rootLogger = DEBUG,INFO, A1, A2,A3
![](http://java.chinaitlab.com/UploadFiles_8734/200612/20061216102949224.gif)
log4j.appender.A1 = org.apache.log4j.ConsoleAppender
![](http://java.chinaitlab.com/UploadFiles_8734/200612/20061216102949224.gif)
log4j.appender.A1.layout = org.apache.log4j.PatternLayout
![](http://java.chinaitlab.com/UploadFiles_8734/200612/20061216102949224.gif)
log4j.appender.A1.layout.ConversionPattern = %4p [%t] (%F:%L) - %m%n
![](http://java.chinaitlab.com/UploadFiles_8734/200612/20061216102949224.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200612/20061216102949224.gif)
log4j.appender.A2 = org.apache.log4j.RollingFileAppender
![](http://java.chinaitlab.com/UploadFiles_8734/200612/20061216102949224.gif)
log4j.appender.A2.File =../../log/test.log
![](http://java.chinaitlab.com/UploadFiles_8734/200612/20061216102949224.gif)
log4j.appender.A2.MaxFileSize = 1KB
![](http://java.chinaitlab.com/UploadFiles_8734/200612/20061216102949224.gif)
log4j.appender.A2.MaxBackupIndex = 3
![](http://java.chinaitlab.com/UploadFiles_8734/200612/20061216102949224.gif)
log4j.appender.A2.layout = org.apache.log4j.PatternLayout
![](http://java.chinaitlab.com/UploadFiles_8734/200612/20061216102949224.gif)
log4j.appender.A2.layout.ConversionPattern = %d{yyyy-MM-dd hh:mm:ss}:%p %t %c - %m%n
![](http://java.chinaitlab.com/UploadFiles_8734/200612/20061216102949224.gif)
![](http://java.chinaitlab.com/UploadFiles_8734/200612/20061216102949224.gif)
log4j.appender.A3=org.apache.log4j.jdbc.JDBCAppender
![](http://java.chinaitlab.com/UploadFiles_8734/200612/20061216102949224.gif)
log4j.appender.A3.URL= jdbc:mysql://localhost:3306/log4jTest
![](http://java.chinaitlab.com/UploadFiles_8734/200612/20061216102949224.gif)
log4j.appender.A3.driver= com.mysql.jdbc.Driver
![](http://java.chinaitlab.com/UploadFiles_8734/200612/20061216102949224.gif)
log4j.appender.A3.user= root
![](http://java.chinaitlab.com/UploadFiles_8734/200612/20061216102949224.gif)
log4j.appender.A3.password= 123456
![](http://java.chinaitlab.com/UploadFiles_8734/200612/20061216102949224.gif)
log4j.appender.A3.layout = org.apache.log4j.PatternLayout
![](http://java.chinaitlab.com/UploadFiles_8734/200612/20061216102949224.gif)
log4j.appender.A3.layout.ConversionPattern = INSERT INTO log4j (createDate, thread, level, class, message)
相关文章推荐
- 网站一些有用的东西
- Shell脚本一些有用且很酷的东西
- 罗列一些自认为比较有用的东西
- 罗列一些自认为比较有用的前端东西
- 一些有用的东西
- makefile的一些有用的东西,自己记录下
- 一些有用的东西
- 本人的一些有用的东西的汇总
- runtime一些有用的东西
- android一些有用的东西
- 给学习Java初学者一些个人感觉有用的东西
- 一些有用的东西集合
- C#一些有用的东西
- 一些关于注册表有用的东西
- 一些个人整理的,比较有用的东西
- <HTML5与CSS3实战指南>读书笔记之一些可能会有用的东西
- Java中一些比较有用的东西(在别的地方看到的,觉得挺好的,就记录下来了)
- 收集的一些可能有用的东西
- 看的一些有用常用的东西,收藏一下
- 关于服务器方面的东西,一些有用的链接