2013-09-21 107 views
2

当我点击jspA中的链接时,它将重定向到jspB,查询字符串为srcsrc的消息将显示在jspB中没有问题。但是,为什么我尝试点击提交,我无法在我的servlet页面中检索src的值。有没有办法让我在servlet中检索src的值?谢谢。从JSP检索数据

里面我JSPB页:

<img src="<%= request.getParameter("src") %>" /> 
<table>  
    <form name="frmTest" action="test" method="post"> 
     <input type="submit" value="sub" name="sub" /> 
    </form> 
</table> 

在我的Servlet的测试:

@Override 
protected void doPost(HttpServletRequest req, HttpServletResponse resp) 
     throws ServletException, IOException{ 

    String imgUrl = req.getParameter("src"); 

我从imrUrl检索空值。

回答

2

当您提交html form时,仅发送inputselect元素作为参数。您没有任何name属性设置为src

您可以使用一个隐藏input

<form name="frmTest" action="test" method="post"> 
    <input type="submit" value="sub" name="sub" /> 
    <input type="hidden" name="src" value="<%= request.getParameter("src") %>" /> 
</form> 

It is generally discouraged to use scriptlets.上JSTL和EL,并利用这些技术,而不是阅读起来。

+0

感谢,并隐藏字段解决了我的问题。我也改为$ {param.src}。 – Sky

0

我假设你的意思是从jspB提交?如果是这样,您需要将src值存储在表单的隐藏字段中,以便在提交时调用servlet时可用。像下面

<form name="frmTest" action="test" method="post"> 
    <input type="hidden" value="<%= request.getParameter("src") %>" name="src" /> 
    <input type="submit" value="sub" name="sub" /> 
</form> 

PS您应该避免使用scriptlet的东西(即<%和%之间的代码>),而使用JSP表达式语言

+0

感谢您的回答,它的工作。 – Sky