我的Filter:
package myFilter;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class SessionChecker implements Filter {
private ServletContext context;
private String targetURI;
public void init(FilterConfig config) throws ServletException {
// TODO 自动生成方法存根
context =config.getServletContext();
targetURI=config.getInitParameter("targetURI");
System.out.print(targetURI);
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
// TODO 自动生成方法存根
HttpServletRequest httprequest=(HttpServletRequest) request;
HttpServletResponse httpresponse=(HttpServletResponse)response;
HttpSession session =httprequest.getSession();
String loginname=(String)session.getAttribute("myLoginName");
System.out.print(loginname);(这里是为空啊,)
if (loginname!=null){
chain.doFilter(httprequest, httpresponse);
}else {
httprequest.getRequestDispatcher(targetURI).forward(httprequest, httpresponse);
}
}
public void destroy() {
// TODO 自动生成方法存根
}
}
我的Servlet:
package myServlet;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class Log_Servlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 6655394384809860986L;
public Log_Servlet(){
super();
}
protected void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
response.setContentType("text/html;charset=gb2312");
request.getRequestDispatcher("index.jsp").forward(request,response);
}
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
doPost( request, response);
}
}
我需要过滤的东西:
<%@ page contentType="text/html;charset=gb2312" %>
<html>
<head>
<title>学生基本信息操作</title>
</head>
<body>
<a href="Log_Servlet">我要下载 </a>
</body>
</html>
我的web.xml配置:
<filter>
<filter-name>SessionChecker</filter-name>
<filter-class>myFilter.SessionChecker</filter-class>
<init-param>
<param-name>targetURI</param-name>
<param-value>/user/Login0.jsp</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>SessionChecker</filter-name>
<servlet-name>/Log_Servlet</servlet-name>
<dispatcher>FORWARD</dispatcher>(这个要加吗?)
</filter-mapping>
我想要的结果就是,当你没登陆时,你点"我要下载"时会转到登陆页面,
但现在直接到"index.jsp"页面,我的哪里错了,能告诉我吗?谢谢!在线等..