簡體   English   中英

如何在page.redirect上從servlet到jsp顯示成功消息

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

我在jsp頁面中有一個html表單,在提交時將轉到servlet ..在servlet中執行函數后,我再次將它重定向到同一個jsp頁面,從中調用它成功消息,現在在同一個jsp上顯示頁面,但我不知道如何做到這一點......

這是我的jsp表單代碼..

 <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>

這是我的servlet重定向代碼。

response.sendRedirect("Automail.jsp");

在Servlet:

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

 response.sendRedirect("Automail.jsp");

Automail.jsp

  ${msg}

在servlet中:

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

在你的jsp中:

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

根據你的要求,我建議你去找ajax.I給出了一個簡單的例子,說明如何將數據傳遞給servlet。 單擊此處以了解有關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);
                   }
               });

注意名稱 - 參數名稱和名稱1參數值,小時參數名稱和小時1參數值。對於其他人。不要在表單中使用get動作,因為參數值將顯示在URL中,並且還有2048個字符的限制

在servlet中:

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

在JSP中:

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

不要忘記在打印后刪除變量以不包括頁面刷新后的消息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM