Where should JSP files be created?

0

I am starting to learn about java web with tomcat and it is giving me problems where to place each file. The servlets and the java classes, I am creating them within src but the files jsp , html , the view of my application, where should it go in my project?

I was seeing that they are added within the WEB-INF folder but since it is not public how can I access it using servlet's ?

    
asked by MatiEzelQ 04.06.2016 в 22:57
source

2 answers

0

The JSPs and other resources of the view must go to the side of the web resources of the application, that is, the folder webapp, webview, web resources or whatever is called in your project. All the files that are inside your source folders (src or how you configure it) will go lately to the folder WEB-INF / classes, where your compiled reside.

Consider that if you place a JSP file inside WEB-INF, then users of your application can not access that information through the JSP link. This is useful when you need to pre-process the request before displaying the classic view of an MV * architecture (MVC, MVP, MVVM, etc.).

For example, if you need to load a list of products from your data source (database, web service, etc.) and show it in the application, it becomes more complicated to have the JSP as direct access to the client since you would have to place Java code in the JSP to get the information and then display it, this is achieved with scriptlets but that technology should not be used, it is not recommended. On the other hand, placing your JSP file inside WEB-INF facilitates this process by invoking the servlet first and making it forward to the appropriate view.

Here is a brief example. It is assumed that you have the JSTL library as dependencies in your project.

Project structure:

src
- edu.ltmj.controller
  + ArticulosServlet.java
Web Resources (o como se llame en tu proyecto)
- WEB-INF
  + articulos.jsp
  + web.xml
- resources
  + (tus archivos de recursos: JS, CSS, imágenes, etc, que son de acceso libre)

Code ArticulosServlet :

public class ArticulosServlet extends HttpServlet {
    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse res)
        throws IOException, ServletException {
        ArticuloService articuloService = new ArticuloService();
        List<Articulo> listaArticulos = articuloService.getListaArticulos(); //se conecta a base de datos u otra fuente de datos para traer la lista de artículos
        req.setAttribute("listaArticulos", listaArticulos);
        req.getRequestDispatcher("WEB-INF/articulos.jsp")
            .forward(req, res);
    }
}

Code of articles.jsp:

<%--
    Se agrega la librería JSTL
    Indispensable para realizar MV* con JSPs
--%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Articulos</title>
</head>
<body>
    Lista de artículos:
    <table>
        <thead>
            <tr>
                <th>Nombre</th>
                <th>Precio</th>
            </tr>
        </thead>
        <tbody>
        <c:forEach items="${listaArticulos}" var="articulo">
            <tr>
                <td>${articulo.nombre}</td>
                <td><fmt:formatNumber value="${articulo.precio}" type="currency" maxFractionDigits="3" /></td>
            </tr>
        </c:forEach>
        </tbody>
    </table>
</body>
</html>
    
answered by 05.06.2016 / 00:11
source
2

They should be stored in the folder called WebContent , here resources are added including the files .jsp, .html, images, etc.

To access a servlet from the folder WebContent I imagine you know, in any way I add it for other users, for example, according to the image, if we want to access the .jsp file inside the folder /jsp :

ServletContext context = request.getServletContext();
String pathWebContent = context.getRealPath("/");
String pathJsp = pathWebContent + "/jsp/Sample.jsp" ;
  

Folder WebContent Mandatory location of all web resources,   including HTML, JSP, graphic files, etc.

for more information: Dynamic web projects and applications.

    
answered by 04.06.2016 в 23:17