5

我在 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");
4

4 回答 4

7

在 Servlet:

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

 response.sendRedirect("Automail.jsp");

Automail.jsp

  ${msg}
于 2013-11-14T07:39:31.113 回答
3

在小服务程序中:

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

在你的jsp中:

<c:if test="${param.success eq 1}">
     <div> success </div>
</c:if>
于 2013-11-14T07:39:57.903 回答
2

根据您的要求,我建议您使用 ajax。我举了一个简单的示例,如何将数据传递给 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);
                   }
               });

注意name-parameter name 和 name1 参数值,hour 参数名和 hour1 参数值。其他类似。不要在表单中使用 get 操作,因为参数值会显示在 url 中,并且有 2048 个字符的限制

于 2013-11-14T07:56:21.727 回答
1

在小服务程序中:

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>

不要忘记在打印后删除变量以在页面刷新后不包含消息。

于 2019-06-18T18:53:31.753 回答