0

Here i want to display the alert message using js and in that alert message i want to use the values of the request.getParameter which is coming to my current jsp page. i want to disply a small message window if current jsp page is getting any value in dbresult and in that window i want to provide the link to other jsp page.I am new to jsp and got stucked at this point. please help.How we can provide the value of 'dbresult' to js function? and execute the function only if value is not null?? Here my question is how we can display the 'dbresult' and 'link to othe jsp page' using js alert? how we can pass this to js function? or is there any other way to do this? i am not using jquery.

my code is as below

<div id="header">
    <h1>Contingency Form &#45; New/Existing/Home move Customers</h1>
    <%
        if (request.getParameter("dbResult") != null &&
            !"".equals(request.getParameter("dbResult"))) {
    %>
        <h1>
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <%=request.getParameter("dbResult")%>
    <%
        }
    %>
        </h1>
</div>
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
Rohan
  • 521
  • 4
  • 9
  • 24

3 Answers3

1

Pass it to the page like any other HTML:

<div id="header">
    <h1>Contingency Form &#45; New/Existing/Home move Customers</h1>
    <%
        if (request.getParameter("dbResult") != null && !"".equals(request.getParameter("dbResult"))) {
    %>
    <h1>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <script>
            var dbResult = "<%=request.getParameter("dbResult").replace("\"", "\\\"")%>";
            alert(dbResult);
            window.location.href = "http://www.imdb.com"; // or your other JSP
        </script>
    </h1>
    <%
        }
    %>
</div>

In the case above, dbResult will be assigned as a string. To take care so the request variable does not contain double quotes ("), we escape them using the replace() function of the String class.

Also, as you can see, I placed your ending h1 tag inside the if, as it makes no sense to close a h1 when it was not opened (the case when the if condition is not met).

To redirect, just use window.location.href after the alert().

acdcjunior
  • 132,397
  • 37
  • 331
  • 304
  • It will work only if dbResult is a number. Otherwise you have to wrap it quotation mark. `var dbResult = "<%=request.getParameter("dbResult")%>"`; – aldux Jul 18 '13 at 16:17
  • @aldux you are absolutely right, I stand corrected. Also added the `replace()` function for escaping. – acdcjunior Jul 18 '13 at 16:23
  • I just want to navigate to other jsp page once user clicks on ok button of alert.How to do this? – Rohan Jul 18 '13 at 16:44
  • @Rohan I added the navigation to other page when the OK button is pressed. Check it out. – acdcjunior Jul 18 '13 at 17:34
0

Stop using scriptlets (more info: How to avoid Java code in JSP files?). You should also fix your current HTML since it will be generated in the wrong way.

The result would be like this:

<c:set var="dbResult" value="${empty param.dbResult ? '' : param.dbResult}" />
<div id="header">
    <h1>Contingency Form &#45; New/Existing/Home move Customers</h1>
    <c:if test="${not dbResult eq ''}">
        <h1>${dbResult}</h1>
    </c:if>
</div>
Community
  • 1
  • 1
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
0

One way,

<% if(request.getParameter("dbResult")!=null){ %>   

    <script type="text/javascript">
       var dbResult = "<%=request.getParameter("dbResult")%>";
       alert(dbResult);     
    </script>  

<% } %>
Jayesh
  • 6,047
  • 13
  • 49
  • 81