views:

26

answers:

2

I have a JSP with some fields. When I fill in the fields and click the send button, I have to go check that the data in database exists; if it does not, I display a popup alerting the user that the data does not exist in the database. If they choose to continue anyway it goes on the screen; if not it returns to the starting point.

I'm stuck at where I display the popup, since the server can not display the popup on the client

A: 

I would do somethign along the lines of the following:

First, set a boolean flag while doing your database check

<%
    boolean POPUP_FLAG = /*condition check*/
%>

Then later, if your rendering code, you can check the flag and include a window.open in the page as appropriate

<%
  if (POPUP_FLAG) {
%>
    <script>
    window.open("popup.jsp", "_blank");
    </script>
    /*
      Include here any special details to display on the main page in popup mode
    */
<%
  } else {
%>
    /*
      Include here the normal information you would want displayed when not in popup mode
    */
<%
  }
%>
Mike Clark
[How to avoid Java code in JSP](http://stackoverflow.com/questions/3177733/howto-avoid-java-code-in-jsp-files).
BalusC
A: 

Just let Servlet store the condition in request scope and let JSP print the Javascript code conditionally.

Servlet:

boolean exist = yourDAO.exist(parameters);
request.setAttribute("exist", exist);
request.getRequestDispatcher("page.jsp").forward(request, response);

JSP (using JSTL):

<c:if test="${!exist}">
    <script>
        if (confirm('Data does not exist, do you want to continue?')) {
            // Do whatever you want when user want to continue ("goes on screen").
        } else {
            // Do whatever you want when user don't want to continue ("returns to starting point").
        }
    </script>
</c:if>
BalusC
request.setAttribute("exist", exist);not work i have an error in eclipse setAttribute(String,Object) no request.setAttribute(String,boolean)
Mercer
I didn't expect at all that you're still on the dead Java 1.4 or older :) Use `Boolean` instead or upgrade to at least 5.0 which supports [autoboxing](http://java.sun.com/j2se/1.5.0/docs/guide/language/autoboxing.html). This is nowadays a very rare case. In future questions I think it's smart to mention as well that you're still on 1.4 or older. Java 5.0 brought pretty a lot of improvements alongwith.
BalusC
I'm on an existing project I can not change
Mercer
No problem, just use `Boolean` instead of `boolean` :)
BalusC