2017-02-24 98 views
0

下午好! 在我做的一個例子中,當我調試servlet出現在過濾器前面時。該servlet呼叫順序失敗

在系統上,我正在做的過濾器被稱爲第一個,該servlet ...這是造成問題。

我正在做系統的登錄部分。

我使用的是servlet的3.0版本。

在課堂上,我並不需要使用xml,但在某些網站上指出要做到這一點......這也沒有奏效。

指數

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
    pageEncoding="ISO-8859-1"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
<title>Insert title here</title> 
</head> 
<body> 
      <div> 
      <form action="template/inicio.html" method="post"> 
       <label for="login">Login </label>  
       <input type="text" name="login"> 

       <br> 

       <label for="senha">Senha </label> 
       <input type="text" name="senha"> 

       <br> 

       <input type="submit" value="Entrar"> 
      </form> 
     </div> 
     ${msg} 
</body> 
</html> 

FiltroLogin

package filtro; 

import java.io.IOException; 

import javax.servlet.DispatcherType; 
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.annotation.WebFilter; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import javax.servlet.http.HttpSession; 

/** 
* Servlet Filter implementation class FiltroLogin 
*/ 
@WebFilter(
     dispatcherTypes = { 
       DispatcherType.REQUEST, 
       DispatcherType.FORWARD, 
       DispatcherType.INCLUDE, 
       DispatcherType.ERROR 
     } 
        , 
     urlPatterns = { 
       "/FiltroLogin", 
       "/template/*" 
     }) 
public class FiltroLogin implements Filter { 

    /** 
    * Default constructor. 
    */ 
    public FiltroLogin() { 
     // TODO Auto-generated constructor stub 
    } 

    /** 
    * @see Filter#destroy() 
    */ 
    public void destroy() { 
     // TODO Auto-generated method stub 
    } 

    /** 
    * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain) 
    */ 
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { 

     HttpServletResponse res = (HttpServletResponse) response; 
     HttpServletRequest req = (HttpServletRequest) request; 
     HttpSession session = req.getSession(); 



     if(session.getAttribute("login") != null){ 
      //Se estiver logado, deixa a pagina ser exibida 
      chain.doFilter(request, response); 
     }else{ 
      //mandar embora 
      res.sendRedirect(req.getContextPath()); 
     } 
    } 

    /** 
    * @see Filter#init(FilterConfig) 
    */ 
    public void init(FilterConfig fConfig) throws ServletException { 
     // TODO Auto-generated method stub 
    } 

} 

ControleLogin

package control; 

import java.io.IOException; 
import java.util.List; 

import javax.servlet.ServletException; 
import javax.servlet.annotation.WebServlet; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
import javax.servlet.http.HttpSession; 

import org.firebirdsql.jdbc.parser.JaybirdSqlParser.function_return; 

import model.Funcionario; 
import persistence.FuncionarioDao; 
import persistence.LoginDao; 

/** 
* Servlet implementation class ControleLogin 
*/ 
@WebServlet({"/ControleLogin","/template/inicio.html", "/template/logout.html"}) 
public class ControleLogin extends HttpServlet { 
    private static final long serialVersionUID = 1L; 

    /** 
    * @see HttpServlet#HttpServlet() 
    */ 
    public ControleLogin() { 
     super(); 
     // TODO Auto-generated constructor stub 
    } 

    /** 
    * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) 
    */ 
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
     execute(request, response); 
    } 


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
     execute(request, response); 
    } 

protected void execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
     try{ 

       String url = request.getServletPath(); 

       if(url.equalsIgnoreCase("/template/inicio.html")){ 
        String login = request.getParameter("login"); 
        String senha = request.getParameter("senha"); 

       if(new LoginDao().logar(login, senha).booleanValue() == true){ 
        Funcionario func = new LoginDao().buscarUsuario(login); 

        HttpSession session = request.getSession(); 

        session.setAttribute("nomeP", func.getNome()); 

        response.sendRedirect("template/indCadastro.jsp"); 
       }else{ 
        request.setAttribute("msg", "<div class = 'alert alert-info'>"+ "Email ou senha Incorretos!"+"</div>"); 
        request.getRequestDispatcher("../index.jsp").forward(request, response); 
       } 

       }else if(url.equalsIgnoreCase("/template/logout.html")){ 
        HttpSession session = request.getSession(); 

        session.removeAttribute("nomeP"); 

        session.invalidate(); 

        response.sendRedirect(request.getContextPath() + "/"); 
       } 

     }catch(Exception e){ 
      e.printStackTrace(); 
     } 
    } 
} 
+0

不明。你想要達到什麼目的,你在測試中做了什麼,你期望發生什麼以及發生了什麼?爲什麼你的servlet映射到3個不同的URL?爲什麼不使用3個不同的servlet呢? –

回答

0

我解決的指數,是與「行動=」模板/ Logar「」,當我切換到「行動=」Logar「」,它的工作。 愚蠢的錯誤。