package com.accp.epet.common;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class LoginFilter extends HttpServlet implements Filter {
private final static String[] exact_url = { "index.jsp", "styles.css",
"/images" ,"login.do" }; // 不用做权限判断的URL
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse res = (HttpServletResponse) response;
HttpSession session = req.getSession(false);
boolean needFilt = false;//是否需要过滤
String uri=req.getRequestURI();
//.jsp和.do的uri需要过滤
if (req.getRequestURI().indexOf(".jsp") >= 0
|| req.getRequestURI().indexOf(".do") >= 0)
needFilt = true;
//不用做权限判断的URL不需要过滤
for (int i = 0; i < exact_url.length; i++) {
if (req.getRequestURI().indexOf(exact_url[i]) >= 0) {
needFilt = false;
break;
}
}
if (needFilt && req.getSession().getAttribute("login") == null) {
res.sendRedirect(req.getContextPath() + "/index.jsp");
return;
}
chain.doFilter(req, res);
}
public void init(FilterConfig arg0) throws ServletException {
}
}
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*"%>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<%
// ***************** 数据库连接代码 开始 *****************
//异常处理语句
try
{
//以下几项请自行修改
String spath="\\db\\mydb.mdb"; //注意由网站根到文件的路径
String dbpath = application.getRealPath(spath); //转化成物理路径
//System.out.println(dbpath);
String dbname = "null"; //Access 数据库用户名,没有则为空
String dbpass = "admin"; //Access 数据库密 码,没有则为空
//数据库连接字符串
String url ="jdbc:odbc:Driver={Microsoft Access Driver (*.mdb)};DBQ="+dbpath;
//加载驱动程序
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
//建立连接
Connection conn= DriverManager.getConnection(url,dbname,dbpass);
//创建语句对象
Statement stmt=conn.createStatement(); //(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE)
// **** 数据库连接代码 结束 *********
//*********** 测试数据代码开始 *******************************
String sql = "select * from tSiteConfig";
ResultSet rs = stmt.executeQuery(sql);
out.print("恭喜你!数据库连接成功!<br>");
while(rs.next())
{
out.print(" 用户id:" + rs.getInt("id") + "<br>");
out.print(" 用户名:" + rs.getString("uname") + "<br>");
out.print(" 密 码:" + rs.getString("upass") + "<br>");
}
//*********** 测试数据代码结束 *******************************
rs.close(); //关闭记录集对象
stmt.close(); //关闭语句对象
conn.close(); //关闭连接对象
}catch(Exception e){
out.print("数据库连接错误!,错误信息如下:<br>");
out.print(e.getMessage());
}
%>