Java web mapping with xml

Asked

Viewed 43 times

1

Hello,

I am learning java for web, and I have a certain problem, I am not able to do the mapping in the xml file for the links of the pages to be cleaner.

I have the following class:

@WebServlet(urlPatterns = "/exec")
public class Controller extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession();
        session.removeAttribute("itemInvalido");
        String tarefa = req.getParameter("tarefa");
        if (tarefa == null) {
            throw new IllegalArgumentException("Voce esqueceu de passar a tarefa");
        }

            try {
                RequestDispatcher dispatcher;

                String nomeClasse = "br.com.portal.hidrosistemas.web." + tarefa;
                Class<?> type = Class.forName(nomeClasse);
                Tarefa instancia = (Tarefa) type.newInstance();
                String pagina = instancia.executa(req, resp);
                dispatcher = req.getRequestDispatcher(pagina);
                dispatcher.forward(req, resp);      

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

And the Login class:

public class Login implements Tarefa {

    @Override
    public String executa(HttpServletRequest req, HttpServletResponse resp) throws Exception {

        HttpSession session = req.getSession();
        Empresa empresa = (Empresa) session.getAttribute("usuarioLogado");

        if(empresa != null)
            return "/WEB-INF/paginas/pedidos.jsp";


        try (Connection con = new ConnectionPool().getConnetion()) {
            String email = req.getParameter("e-mail");
            String senha = req.getParameter("senha");

            empresa = new EmpresaDAO(con).buscaLogin(email, senha);


            if (empresa != null) {
                session = req.getSession();
                session.setAttribute("usuarioLogado", empresa);
                session.setMaxInactiveInterval(10*60);
                return "/WEB-INF/paginas/pedidos.jsp";

            }
        }catch(NullPointerException e) {
            return "/index.jsp";
        }
        req.setAttribute("erroLogin", "Usuario ou senha invalidos");
        return "/index.jsp";
    }
}

xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">

  <display-name>hidro-sistemas</display-name>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>

    <servlet>
    <servlet-name>login</servlet-name>
    <servlet-class>br.com.portal.hidrosistemas.web.Login</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>login</servlet-name>
    <url-pattern>/login</url-pattern>
  </servlet-mapping>

</web-app>

But the links do not continue in the same way, for example after login: "http://localhost:8080/hidro-sistemas/exec? task=Login", I would like it to be /paginaInicial or /login.

What am I doing wrong?

  • I recommend giving a read on how to debug small programs. Sopt appreciates well argued questions about the explanation of his problem. Otherwise, the question is good.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.