使用JSP和servlet以及NetBeans IDE。
我通过href传递一个参数,如下所示。
<a href="coursematerial.jsp?param1=${row.COURSEID}"><c:out value="${row.COURSEID}"/>
现在在下一个jsp中,我想保存我传递的参数。我可以这样做并将保存的参数发送到表单中的隐藏输入吗? 我尝试使用
<input type="hidden" name="courseid" value=<%= request.getParameter("param1")%> >
但由于在填充表单之前还有另一个href,因此将该值检索为null。
有没有办法将所需参数保存为会话(没有脚本但使用JSTL)并在不将其作为href传递的情况下进行检索?
答案 0 :(得分:0)
我不知道是否可以使用jstl。但是你可以使用闪存范围。该属性将根据您的要求仍然存在下一个jsp。 使用此简单代码实现闪存范围。
例如,要向flash作用域添加消息,只需使用常规语法添加请求作用域属性,然后让fiter处理它:
request.setAttribute("flash.message", "Here is the news...");
然后可以使用EL或scriptlet语法在重定向目标JSP中访问上述属性(省略'flash。'前缀):
${message}
or
<%= request.getAttribute("message") %>
以下是过滤器的代码:
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
/**
* Ensures that any request parameters whose names start
* with 'flash.' are available for the next request too.
*/
public class FlashScopeFilter implements Filter {
private static final String FLASH_SESSION_KEY = "FLASH_SESSION_KEY";
@SuppressWarnings("unchecked")
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
//reinstate any flash scoped params from the users session
//and clear the session
if (request instanceof HttpServletRequest) {
HttpServletRequest httpRequest = (HttpServletRequest) request;
HttpSession session = httpRequest.getSession(false);
if (session != null) {
Map<String, Object> flashParams = (Map<String, Object>)
session.getAttribute(FLASH_SESSION_KEY);
if (flashParams != null) {
for (Map.Entry<String, Object> flashEntry : flashParams.entrySet()) {
request.setAttribute(flashEntry.getKey(), flashEntry.getValue());
}
session.removeAttribute(FLASH_SESSION_KEY);
}
}
}
//process the chain
chain.doFilter(request, response);
//store any flash scoped params in the user's session for the
//next request
if (request instanceof HttpServletRequest) {
HttpServletRequest httpRequest = (HttpServletRequest) request;
Map<String, Object> flashParams = new HashMap();
Enumeration e = httpRequest.getAttributeNames();
while (e.hasMoreElements()) {
String paramName = (String) e.nextElement();
if (paramName.startsWith("flash.")) {
Object value = request.getAttribute(paramName);
paramName = paramName.substring(6, paramName.length());
flashParams.put(paramName, value);
}
}
if (flashParams.size() > 0) {
HttpSession session = httpRequest.getSession(false);
session.setAttribute(FLASH_SESSION_KEY, flashParams);
}
}
}
public void init(FilterConfig filterConfig) throws ServletException {
//no-op
}
public void destroy() {
//no-op
}
}
http://smartkey.co.uk/development/implementing-flash-scope-in-java-web-applications/