servlet调用顺序失败

时间:2017-02-24 19:06:47

标签: java servlets servlet-filters

下午好! 在我在课堂上做的一个例子中,当我调试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();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我解决了索引,使用了“Action =”template / Logar“”,当我切换到“Action =”Logar“”时,它工作正常。 愚蠢的错误。