index.jsp登陸頁面 UserServlet.java是針對登陸的一個servlet
UserFilter.java是控制權限的類show.jsp是登陸成功後進入的頁面
error.jsp是當用戶繞過index.jsp登陸直接通過控制url進入show.jsp時會跳出的頁面。
文件結構 XML配置 index.jsp如圖
UserServlet.java
public class UserServlet extends HttpServlet{
public void doGet(HttpServletRequest req, HttpServletResponse resp)throws ServletException,IOException{
String name=req.getParameter("username");
String password=req.getParameter("password");
UserInfo info= new UserInfo();
try{
if(password.equals("123456")){
info.setName(name);
info.setPassword(password);
req.getSession().setAttribute("userinfo", info);
System.out.println("登陸成功");}
else{
System.out.println("登錄失敗");}}
catch(Exception e){
e.printStackTrace();}
RequestDispatcher rd=req.getRequestDispatcher("/show.jsp");
rd.forward(req, resp);}
public void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException,IOException{
doGet(request,response);}}
UserFilter.java
public class UserFilter {
public void destroy() {}
public void doFilter(ServletRequest request, ServletResponse response,FilterChain filter) throws IOException, ServletException {
UserInfo info=(UserInfo)((HttpServletRequest) request).getSession().getAttribute("userinfo");
if(info.getName()!=null) {
filter.doFilter(request, response); }
else {
response.setContentType("text/html;charset=utf-8");
((HttpServletResponse) response).sendRedirect("../error.jsp"); }}
public void init(FilterConfig arg0) throws ServletException {}}
404,是你點擊哪個頁面出來的呢,還是說一訪問login.jsp登陸用戶名時候跳轉404?,如果是這樣的話,
http://blog.csdn.net/liuxiaogangqq/article/details/8257400
參考一下這個,你的過濾器代碼中
if(info.getName()!=null) {
filter.doFilter(request, response);
}
如果用戶存在,應該跳轉,你為啥沒跳轉呢
一般是這樣寫
if (user != null) {
chain.doFilter(req, resp);
return;
}
最後一堆判斷,都符合了再跳轉,而且你的error.jsp不是和show。jsp同級目錄嗎,為啥有..呢