<sql:query var="rs" dataSource="jdbc/TestDB">
select id, foo, bar from testdata
</sql:query>
<html>
<head>
<title>DB Test</title>
</head>
<body>
<h2>Results</h2>
<c:forEach var="row" items="${rs.rows}">
Foo ${row.foo}<br/>
Bar ${row.bar}<br/>
</c:forEach>
</body>
</html>
h.新建web應用
下載jakarta-taglibs-standard-1.1.0
copy jstl.jar and standard.jar to your web app's WEB-INF/lib
DBTest/
WEB-INF/
web.xml
lib/
jstl.jar
standard.jar
test.jsp
拷貝到webapps/ 下
i.啟動mysql,tomcat
訪問:
http://localhost:8080/DBTest/test.jsp
顯示:
Results
Foo hello
Bar 12345
4.ssl的配置,以jdk1.4.2為例
a.進入%JAVA_HOME%\bin
運行命令:keytool -genkey -alias tomcat -keyalg RSA
以tomcat 安裝密碼為198277,ketool設置密碼為198277為例
輸入keystore密碼: 198277
您的名字與姓氏是什么?
[Unknown]: ycg
您的組織單位名稱是什么?
[Unknown]: nju
您的組織名稱是什么?
[Unknown]: nju
您所在的城市或區域名稱是什么?
[Unknown]: nanjing
您所在的州或省份名稱是什么?
[Unknown]: jiangsu
該單位的兩字母國家代碼是什么
[Unknown]: nd
CN=ycg, OU=nju, O=nju, L=nanjing, ST=jiangsu, C=nd 正確嗎?
[否]: y
輸入<tomcat>的主密碼
(如果和 keystore 密碼相同,按回車): 198277
b.在你的D:\Documents and Settings\的當前用戶目錄下可以找到.keystore文件.將其拷貝到conf/文件夾下.
c.在server.xml 中找到
<!--
<Connector port="8443"
maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" disableUploadTimeout="true"
acceptCount="100" debug="0" scheme="https" secure="true"
clientAuth="false" sslProtocol="TLS" />
-->
去掉注釋
添加配置字段:keystoreFile="/conf/.keystore" keystorePass="198277"
如: <!-- Define a SSL Coyote HTTP/1.1 Connector on port 8443 -->
<Connector port="8443"
maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" disableUploadTimeout="true"
acceptCount="100" debug="0" scheme="https" secure="true"
clientAuth="false" sslProtocol="TLS" keystoreFile="/conf/.keystore"
keystorePass="198277"/>
d.測試為:
https://localhost:8443
e.在自己的程序中添加ssl認證方式為:
在web.xml 中<web-app></web-app>添加
<security-constraint>
<web-resource-collection>
<web-resource-name>Success</web-resource-name>
<url-pattern>/</url-pattern>
<http-method>GET</http-method>
<http-method>POST</http-method>
</web-resource-collection>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
f.用上提為例就是
修改web.xml 為
<web-app xmlns=" xmlns:xsi=" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
version="2.4">
<description>MySQL Test App</description>
<security-constraint>
<web-resource-collection>
<web-resource-name>Success</web-resource-name>
<url-pattern>/</url-pattern>
<http-method>GET</http-method>
<http-method>POST</http-method>
</web-resource-collection>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/TestDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
訪問:
https://localhost:8443/DBTest/test.jsp
g.如果與2配置的jdbcRealm結合起來進行表單認證
先在user_roles表中添加user_name:ycg role_name:web-user
在users表中添加user_name:ycg user_pass:198277
然后在web.xml中添加
<auth-constraint>
<role-name>web-user</role-name>
</auth-constraint>
<login-config>
<auth-method>BASIC</auth-method>
<realm-name>My Member Area</realm-name>
</login-config>
修改后的web.xml如:
<web-app xmlns=" xmlns:xsi=" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
version="2.4">
<description>MySQL Test App</description>
<security-constraint>
<web-resource-collection>
<web-resource-name>Success</web-resource-name>
<url-pattern>/</url-pattern>
<http-method>GET</http-method>
<http-method>POST</http-method>
</web-resource-collection>
<auth-constraint>
<role-name>web-user</role-name>
</auth-constraint>
<user-data-constraint>
<transport-guarantee>CONFIDENTIAL</transport-guarantee>
</user-data-constraint>
</security-constraint>
<login-config>
<auth-method>BASIC</auth-method>
<realm-name>My Member Area</realm-name>
</login-config>
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/TestDB</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
測試:
http://localhost:8080/DBTest/test.jsp
將通過ssl連接,并進行表單認證.用戶密碼可在user_roles,和users中添加.
5.中文亂碼問題:
mysql 默認編碼 iso
tomcat request 傳輸編碼 iso
如果要顯示中文
在*.jsp中添加
<head>
<%@ page
language="java"
contentType="text/html; charset=GB18030"
pageEncoding="GB18030"
%>
</head>
如果是數據傳輸中的亂碼(如用servlet從mysql數據庫讀出的數據)
用以下兩個轉碼函數轉碼,如果不清楚由哪種編碼轉成哪種編碼,就多嘗試.
//轉碼GBK轉ISO
public String toISO(String input) {
try {
byte[] bytes = input.getBytes("GBK");
return new String(bytes,"ISO8859-1");
}catch(Exception ex) {
}
return input;
}
//轉碼IS0轉GBK
public String toGBK(String input) {
try {
byte[] bytes = input.getBytes("ISO8859-1");
return new String(bytes,"GBK");
}catch(Exception ex) {
}
return input;
}
以上配置都測試通過.主要參考tomcat5.0的幫助文檔.將過程寫出來與大家共享.如果發現其中錯誤,請指出.
歡迎給我來信ycg01@software.nju.edu.cn共同探討.
posted on 2005-08-17 09:38
my java 閱讀(446)
評論(0) 編輯 收藏 所屬分類:
java身份認證轉帖