jsp中怎么提取redirect中参数的值

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了jsp中怎么提取redirect中参数的值相关的知识,希望对你有一定的参考价值。

jsp中获取redirect的值一般有两种方式:
1、request.getRequestDispatch.forward( "/test.jsp ")用来转向到另一个页面,能把该页面中的数据比如:request,传入到转向的页面,实现浏览器网址不改变
2、response.sendRedict( "/test.jsp ")页面重定向到另一个页面,实现浏览器网址改变。
第1种方法才能获得页面传过去的值。
而sendRedict 是获取不到值的。要用第一种方法才能获取到值得。
用request.getParameter("name");来获取。
另外,还可以用session.setAttributeI("",""); 来传值
参考技术A jsp中怎么提取redirect中参数的值
转有两种方式:
1、request.getRequestDispatch.forward( "/test.jsp ")用来转向到另一个页面,能把该页面中的数据比如:request,传入到转向的页面,实现浏览器网址不改变
2、response.sendRedict( "/test.jsp ")页面重定向到另一个页面,实现浏览器网址改变。
第1种方法才能获得页面传过去的值。
而sendRedict 是获取不到值的。要用第一种方法才能获取到值得。
用request.getParameter("name");来获取。
另外,还可以用session.setAttributeI("",""); 来传值本回答被提问者采纳

如何在 page.redirect 从 servlet 到 jsp 上显示成功消息

【中文标题】如何在 page.redirect 从 servlet 到 jsp 上显示成功消息【英文标题】:How to show success message on page.redirect from servlet to jsp 【发布时间】:2013-11-27 03:13:00 【问题描述】:

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

【问题讨论】:

根据您的要求使用 ajax 是最好的方法 @rocking 如何通过ajax发送所有的表单输入参数 给我时间写代码 4 u 我举了一个 jquery ajax 的小例子,请看我的回答。如果您有任何疑问,请随时询问 【参考方案1】:

在 Servlet:

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

 response.sendRedirect("Automail.jsp");

Automail.jsp

  $msg

【讨论】:

这是 HttpServletRequest 会话。关闭浏览器会丢失。 我之前使用过这种方式,但在某些客户端阻止了接受 cookie,因此 session.setAttribute() 和 getattribute() 不起作用。之后我必须更改为 encodeURL()【参考方案2】:

在 servlet 中:

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

在你的jsp中:

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

【讨论】:

【参考方案3】:

根据您的要求,我建议您使用 ajax。我举了一个简单的示例,如何将数据传递给 servlet。Click here 以了解有关 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 个字符的限制

【讨论】:

【参考方案4】:

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

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

【讨论】:

这个答案与上面@Kerb 的答案有何不同? @AkinOkegbile 路边正在使用 URL 重写跟踪,而我的正在使用会话跟踪。URL 重写在重定向页面(Automail.jsp)的 URL 中添加参数以指示成功消息。因此 Automail.jsp 将检查参数success是否等于1,如果条件为真则显示成功消息。到目前为止Automail.jsp页面的这种行为是好的。现在如果用户刷新页面,即使未输入数据。在会话跟踪中,显示的消息会删除会话属性。因此刷新后不会再次出现。

以上是关于jsp中怎么提取redirect中参数的值的主要内容,如果未能解决你的问题,请参考以下文章

解决一下如何在jsp页面中获取ArrayList的值

如何在jsp页面直接获取常量类中的常量

asp中response.redirect怎么带值跳转并在另一页面怎么获得转来的值

如何在 page.redirect 从 servlet 到 jsp 上显示成功消息

jsp中怎样获取选择栏修改前和修改后的值

jsp怎么防止重复提交