代碼如下:
<%@ page import="java.sql.*,javax.sql.*,javax.naming.*,com.maxcard.util.*"%>
<html>
<head>
<title> New Document </title>
</head>
<body>
<%
if(request.getHeader("Authorization") == null){
response.setStatus(401);
response.setHeader("WWW-authenticate","Basic realm="www.maxcard.com"");
}else{
//取得輸入的信息
String encoded=(request.getHeader("Authorization"));
//利用Base64作編碼的轉化
String up = StringUtil.decodeBase64(encoded);
String user="";
String password="";
if(up!=null){
//取出用戶名和密碼
user=up.substring(0,up.indexOf(":"));
password=up.substring(up.indexOf(":")+1);
}
if(user.equals("david")&&password.equals("maxcard")){
out.print("認證成功");
}else{
out.print("認證失敗");
}
}
%>
</body>
</html>
相關工具包下載: http://www.bcxy.com/java/dbutil.htm