Hiding Pages (Java for Web Development)

Asked

Viewed 69 times

0

I’m studying the Java booklet for Web Development - Caelum, but I’m struggling at one point. In chapter 9.10 HIDING OUR PAGES I have already put all my pages in the WEB-INF/jsp directory and I have already changed the section Return"/WEB-INF/jsp/list-contacts.jsp"; in the Contactsscape class, but I cannot access the.jsp contact list jsp directly from the address http://localhost:8080/fj21-agenda/contact list.jsp. I will make available my Controllerservlet, Contact Listlogic and JSP contact list codes.

Could you help me solve?

Thank you!

Controllerservlet class:

package br.com.caelum.agenda.servlet;

import java.io.IOException;

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 br.com.caelum.mvc.logica.Logica;
@WebServlet("/mvc")
public class ControllerServlet extends HttpServlet {
  protected void service(HttpServletRequest request, HttpServletResponse             response) throws ServletException, IOException {
    String parametro = request.getParameter("logica");
    String nomeDaClasse = "br.com.caelum.mvc.logica." + parametro;

    try {
      Class classe = Class.forName(nomeDaClasse);
      Logica logica = (Logica) classe.newInstance();
      String pagina = logica.executa(request, response);
      request.getRequestDispatcher(pagina).forward(request, response);

    } catch(Exception e) {
      throw new ServletException("A lógica de nogócios causou uma exceção", e);
    }
  }
}

Contact Us Class

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import br.com.caelum.agenda.dao.ContatoDao;
import br.com.caelum.agenda.modelo.Contato;

public class ListaContatosLogic implements Logica {
    public String executa(HttpServletRequest req, HttpServletResponse res) throws Exception {
        List<Contato> contatos = new ContatoDao().getLista();
        req.setAttribute("contatos", contatos);
        return"/WEB-INF/jsp/lista-contatos.jsp";
    }
}

jsp contact list.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>

<!DOCTYPE html>
<html>
  <head>
    <meta charset="ISO-8859-1">
    <title>Insert title here</title>
  </head>
  <body>
    <c:import url="cabecalho.jsp" />

    <table><!-- percorre contatos montando as linhas da tabela -->
        <c:forEach var="contato" items="${contatos}">
            <tr>
                <td>${contato.nome}</td>
                <td>                    
                    <c:choose>
                        <c:when test="${not empty contato.email}">
                            <a href="mailto:${contato.email}">${contato.email}</a>
                        </c:when>
                        <c:otherwise>
                            E-mail não informado
                        </c:otherwise>
                    </c:choose>
                </td>
                <td>${contato.endereco}</td>
                <td><fmt:formatDate value="${contato.dataNascimento.time}"
                    pattern="dd/MM/yyyy" /></td>

                <td>
                <a href="mvc?logica=RemoveContatoLogic&id=${contato.id}">Remover</a>
                </td>
            </tr>
        </c:forEach>
    </table>

     <c:import url="rodape.jsp" />      
  </body>
</html>
  • 2

    You cannot call JSP directly after placing it within WEB-INF, you have to call Servlet. For example: http://localhost:8080/fj21-agenda/mvc? logica=Contactlistinglogic

  • Thanks for the help Diego.

No answers

Browser other questions tagged

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