Showing posts with label JSP. Show all posts
Showing posts with label JSP. Show all posts

Create zip from blobs and download

Create zip from blobs and download it

http://stackoverflow.com/questions/16585384/best-practices-to-create-and-download-a-huge-zip-from-several-blobs-in-a-webap

http://stackoverflow.com/questions/357851/in-java-how-to-zip-file-from-byte-array

http://stackoverflow.com/questions/1091788/how-to-create-a-zip-file-in-java


Simple custom implementations of patterns for web applications using servlets

There is simple custom implementations of design patterns for web applications using servlets, that have been mentioned on StackOverflow.

Folder structure

actions
----ActionFactory.java
----Action.java
----impl
--------HomePageActionImpl.java
--------CreateProductGetActionImpl.java
--------CreateProductPostActionImpl.java
----actionresults
--------ActionResult.java
--------impl
------------ForwardResult.java
------------RedirectResult.java
controller
----Controller.java

ActionFactory.java
public class ActionFactory {

    public static Map actions = new HashMap<String, Action>(
            new HashMap<String, Action>() {
                {
                    put("GET/", new HomePageActionImpl());
                    put("GET/createproduct", new CreateProductGetActionImpl());
                    put("POST/createproduct", new CreateProductPostActionImpl());
                }
            });

    public static Action getAction(HttpServletRequest request) {
        if (actions.containsKey(request.getMethod() + request.getRequestURI())) {
            return actions.get(request.getMethod() + request.getRequestURI());
        } else {
            return actions.get("GET/error");
        }
    }
}
Action.java
public interface Action {

    ActionResult execute(HttpServletRequest request, HttpServletResponse response) throws Exception;

}
HomePageActionImpl.java
public class HomePageActionImpl implements Action {

    public HomePageActionImpl(){}

    @Override
    public ActionResult execute(HttpServletRequest request, HttpServletResponse response) throws Exception {

        //...

        return new ForwardResult("");
    }

}

CreateProductGetActionImpl.java
public class CreateProductGetActionImpl implements Action  {

    @Override
    public ActionResult execute(HttpServletRequest request, HttpServletResponse response) throws Exception {

        // ...

        return new ForwardResult("createproduct");
    }
}

CreateProductPostActionImpl.java
public class CreateProductPostActionImpl implements Action {
    @Override
    public ActionResult execute(HttpServletRequest request, HttpServletResponse response) throws Exception {

        try {
             // ...

        } catch (ValidationException e) {
            request.setAttribute("message", e.getMessage());
            return new ForwardResult("createproduct");
        }
        return new RedirectResult("");
    }
}

ActionResult.java
public interface ActionResult {

    void execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException;

}

ForwardResult.java
public class ForwardResult implements ActionResult {
    protected final String jsp;

    public ForwardResult(String jsp) {
        this.jsp = jsp;
    }

    @Override
    public void execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        if (jsp == "") {
            request.getRequestDispatcher("/WEB-INF/home.jsp").forward(request, response);
        } else {
            request.getRequestDispatcher("/WEB-INF/" + jsp + ".jsp").forward(request, response);
        }
    }
}

RedirectResult.java
public class RedirectResult extends ForwardResult {

    public RedirectResult(String location) {
        super(location);
    }

    @Override
    public void execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.sendRedirect(jsp);
    }
}

Controller.java
public class Controller extends HttpServlet {

    public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            Action action = ActionFactory.getAction(request);

            ActionResult result = action.execute(request, response);

            result.execute(request, response);

        } catch (Exception e) {
            throw new ServletException("Executing action failed.", e);
        }
    }
}

Create custom JSTL and use it as wrapper for other JSPs (code)

Create custom tags in JSTL and use them as templates

File structure in folders:
WEB-INF
 ----jsps
 --------home.jsp
 --------createproduct.jsp
 ----tags
 --------pagewrapper.tag
 ----template
 --------product.jsp

pagewrapper.tag  (Place for static content, general for all pages)

<%@ tag description="Page Wrapper Tag" pageEncoding="UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ attribute name="title" fragment="true" %>
<%@ attribute name="header" fragment="true" %>
            <jsp:invoke fragment="title"/>
    
    
    


     

Home </body> </html>

home.jsp (It has unique attributes of home page such as "title" and "header")

<%@ page isELIgnored="false" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib prefix="t" tagdir="/WEB-INF/tags" %>
    
    
        Page title
    
    
        App name
    
    
        
    


createproduct.jsp (It includes product.jsp that are general for some pages)

<%@ page isELIgnored="false" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib prefix="t" tagdir="/WEB-INF/tags" %>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="sf" %>
    
        Create product
    
    
        Create product
    
  
      
  


product.jsp
<%@ page isELIgnored="false" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="sf" %>


    
Product name: