Comment éviter le code Java dans les fichiers JSP, en utilisant JSP 2

Mots clés : javajspscriptletjava

meilleur 4 Réponses Comment éviter le code Java dans les fichiers JSP, en utilisant JSP 2

vote vote

97

  public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {       if (((HttpServletRequest) request).getSession().getAttribute("user") == null) {           ((HttpServletResponse) response).sendRedirect("login"); // Not logged in, redirect to login page.       } else {           chain.doFilter(request, response); // Logged in, just continue request.       }   } 
  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {       try {           List<Product> products = productService.list(); // Obtain all products.           request.setAttribute("products", products); // Store products in request scope.           request.getRequestDispatcher("/WEB-INF/products.jsp").forward(request, response); // Forward to JSP page to display them in a HTML table.       } catch (SQLException e) {           throw new ServletException("Retrieving products failed!", e);       }   } 
  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {       String username = request.getParameter("username");       String password = request.getParameter("password");       User user = userService.find(username, password);        if (user != null) {           request.getSession().setAttribute("user", user); // Login user.           response.sendRedirect("home"); // Redirect to home page.       } else {           request.setAttribute("message", "Unknown username/password. Please retry."); // Store error message in request scope.           request.getRequestDispatcher("/WEB-INF/login.jsp").forward(request, response); // Forward to JSP page to redisplay login form with error.       }   } 
  protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {       try {           Action action = ActionFactory.getAction(request);           String view = action.execute(request, response);            if (view.equals(request.getPathInfo().substring(1)) {               request.getRequestDispatcher("/WEB-INF/" + view + ".jsp").forward(request, response);           } else {               response.sendRedirect(view);           }       } catch (Exception e) {           throw new ServletException("Executing action failed.", e);       }   } 
  <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>   ...   <table>       <c:forEach items="${products}" var="product">           <tr>               <td>${product.name}</td>               <td>${product.description}</td>               <td>${product.price}</td>           </tr>       </c:forEach>   </table> 
  <jsp-config>       <jsp-property-group>           <url-pattern>*.jsp</url-pattern>           <scripting-invalid>true</scripting-invalid>       </jsp-property-group>   </jsp-config> 
  <input type="text" name="foo" value="${param.foo}" /> 
  <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>   ...   <input type="text" name="foo" value="${fn:escapeXml(param.foo)}" /> 
vote vote

90

<jsp-config>   <jsp-property-group>     <url-pattern>*.jsp</url-pattern>      <scripting-invalid>true</scripting-invalid>   </jsp-property-group> </jsp-config> 
vote vote

75

<c:if test="${someAttribute == 'something'}">    ... </c:if> 
vote vote

61

<%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %> <html>     <head>     </head>     <body>          <c:out value="${x + 1}" />         <c:out value="${param.name}" />         // and so on      </body> </html> 

Questions similaires