将数据传输到一个servlet
问题描述:
我有一个输入在我的JSF页面类似这样的将数据传输到一个servlet
<html:inputText id="ResponseOK" value="#{bean.ResponseOK}" binding="#{bean.ResponseOKInput}" />
我想(通过用request.getParameter(“ResponseOK”))时获得一个servlet的价值我点击一个命令按钮
<html:commandButton value="Valider" action="#{bean.callServlet}"/>
它调用一个函数
public void callServlet()
{
String url = "http://localhost:8080/TestOne/Timers"; //servlet
FacesContext context = FacesContext.getCurrentInstance();
try {
context.getExternalContext().redirect(url);
}catch (Exception e) {
e.printStackTrace();
}
finally{
context.responseComplete();
}
}
不幸的是,在我的servlet变好了,回复O NLY空
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String Ok = request.getParameter("ResponseOK");// return null
System.out.println(timerOk);
}
非常感谢你
答
为了让你能够得到从请求的属性,输入的名称必须为属性:
<html:inputText id="ResponseOK" value="#{bean.ResponseOK}" binding="#{bean.ResponseOKInput}" name="ResponseOK"/>
更新:
我不太熟悉JSF框架,但我认为你的问题是操作按钮。
此按钮不是提交按钮,所以输入的值不会发送到请求。
当调用一个GET请求,你需要通过在URL中的参数,所以你需要的URL看起来像:
http://localhost:8080/TestOne/Timers?ResponseOK=value
所以,你需要的ResponseOK输入的值传递给callServlet
方法。
希望有所帮助。
而不是重定向,你不应该做一个rd.forward(请求,响应); – Satya 2012-07-24 10:40:02
@Satya感谢您的帮助,但requestDispatcher允许从另一个servlet获取数据,并且在这里我需要从我的jsf页面获取数据。 – ulquiorra 2012-07-24 10:49:51