简体   繁体   中英

How to show success message on page.redirect from servlet to jsp

I have a html form in jsp page which on submit is going to servlet ..After executing the functions in servlet i am again redirecting it to the same jsp page from which it has been invoked with a success message to display now on the same jsp page but i don't know how to do this ...

Here is my jsp form code..

 <form action="CallTimer" method="GET">
    <label class="button2">Set Date: </label>
    <input type="text" name="date" id="date">
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <label class="button2">Set Hour </label>
    <input type="text" name="hour" id="hour">
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <label class="button2">Set Minute: </label>
    <input type="text" name="minute" id="minute">
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <input type="Submit" name="Submit" value="Submit" id="Submit">
    <br/><br/>
    <label class="button2">Set File-Path: </label>
    <input type="text" name="filepath" id="filepath">
</form>

And here is my servlet redirect code.

response.sendRedirect("Automail.jsp");

At Servlet:

 // You need to set value in session for redirection.
 session.setAttribute("msg","Success");

 response.sendRedirect("Automail.jsp");

At Automail.jsp

  ${msg}

In servlet:

response.sendRedirect("Automail.jsp?success=1");

In your jsp:

<c:if test="${param.success eq 1}">
     <div> success </div>
</c:if>

As per your requirement I would suggest you to go for ajax.I gave a simple example how to pass data to servlet. Click here to know more about jquery ajax

$.ajax(
               {
                   type: "get",
                   url: "CallTimer", //Your full URL goes here
                   data: { name: name1, date: date1,hour:hour1,filepath:filepath1,minute:minute1},
                   success: function(data, textStatus, jqXHR){
                       alert("success");                  
                   },
                   error: function(jqXHR){
                       alert(jqXHR.responseStatus);
                   }
               });

note name-parameter name and name1 parameter value,hour parameter name and hour1 parameter value.Similarily for others.Dont use get action in forms because parameter values will be displayed in the url and also there is a limit of 2048 characters

In servlet:

session.setAttribute("message","successful");
response.sendRedirect("Automail.jsp");

In JSP:

<c:if test="${not empty message}">
    <h3 style='color:green'>${message}</h3>
    <c:remove var="message"/>
</c:if>

Don't forget to remove variable after printing to not include message after page refresh.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM