二:Servlet页面Servlet.java
package com.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Servlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//取得内容
String name = request.getParameter("name");
//设置内容到下一页面
request.getSession().setAttribute("name", name);
//客户端跳转
response.sendRedirect("admin/ok.jsp");
//request.getRequestDispatcher("admin/ok.jsp").forward(request, response);
}
}
三:Filter过滤器实现统一中文编码FilterTest2.java
package com.filter;
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;
public class FilterTest2 implements Filter {
public void destroy() {
// TODO Auto-generated method stub
}
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
System.out.println("**********统一中文过滤***********");
try{
arg0.setCharacterEncoding("utf-8");
arg1.setCharacterEncoding("utf-8");
}catch(Exception e){
e.printStackTrace();
}
arg2.doFilter(arg0, arg1);
}
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
}
四:Firlter过滤器实现登陆验证FirterTest.java
package com.filter;
import java.io.IOException;
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.*;
import javax.servlet.http.*;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
/**
* @author
*
*/
public class FilterTest implements javax.servlet.Filter {
public void destroy() {
// TODO Auto-generated method stub
System.out.println("**********销毁***********");
}
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
System.out.println("**********过滤2***********");
//强制转换取得Request对象
HttpServletRequest req = (HttpServletRequest) arg0;
//强制转换取得Response对象
HttpServletResponse res = (HttpServletResponse)arg1;
if(req.getSession().getAttribute("name")!=null){
arg2.doFilter(arg0, arg1);
}else{
//客户端跳转
res.sendRedirect("../login.jsp");
//res.setHeader("refresh", "1;url=../login.jsp");
//arg0.getRequestDispatcher("../login.jsp").forward(arg0, arg1);
}
}
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
System.out.println("**********初始化***********");
}
}
五:Admin文件夹下的ok.jsp页面
六:web.xml的设置
//先过滤全部文件夹,再进行admin文件夹下的过滤~