wiki:snippets/JSP/Avoid_Multiposts

Write Actions to avoid Multiposts

In order write something in the corinis system you call functions like "Guestbook.createEntry()". Normally you would include the function call in the same page as the display function. But most of the time this is not a good practice, since this way it is easy to produce double-entries by just hitting the refresh button.

The best way to avoid this is to create an extra jsp page for the write actions. This has two advantages: you have all write code in one place and can easily add additional fixes (like spam-preventing measures), show a nice error page if something happened plus you remove the multi-post problem.

The JSP

Create a new jsp - we call it process.jsp with the content as follows:

<%@ page language="java" import="java.util.*,corinis.*,corinis.modules.*" %>
<%@ taglib uri="/corinisTags" prefix="corinis" %>
<%
Core core = new Core (pageContext); // init the core module

// Modules to use
// init modules:
Guestbook gb = (Guestbook)core.getModule(Guestbook.class);

// in print we hold all error messages (if any) already piped though an xsl
StringBuffer print = new StringBuffer();

if (checkReturn(gb.createEntry(), print) == 0)
{
        response.sendRedirect(request.getHeader("Referer"));
}

%>


<%!

int checkReturn (org.w3c.dom.Node ret, StringBuffer out)
{
        // successfull
        return 0;
}
%>