GWT将表单参数发送到一个servlet [英] GWT Send form parameters to a servlet
问题描述
源代码与其中的代码是一样的。
我试图在一个servlet中捕获接下来两个突出显示的字段,我可以获取上传的文件。显示在 GWT FormSubmit class Javadoc
form.setEncoding(FormPanel.ENCODING_MULTIPART);
form.setMethod(FormPanel.METHOD_POST);
//创建一个面板来容纳所有的表单小部件。
VerticalPanel面板=新的VerticalPanel();
form.setWidget(panel);
//创建一个文本框,给它一个名字以便它被提交。
final TextBox tb = new TextBox();
tb.setName(WorkTitle);
tb.setValue(WorkTitle);
panel.add(tb);
//创建一个ListBox,给它一个名字和一些与
//和
//其选项关联的值。
ListBox lb = new ListBox();
lb.setName(listBoxFormElement);
lb.addItem(foo,fooValue);
lb.addItem(bar,barValue);
lb.addItem(baz,bazValue);
panel.add(lb);
//创建一个FileUpload小部件。
FileUpload upload = new FileUpload();
upload.setName(uploadFormElement);
panel.add(上传);
//添加一个'提交'按钮。
panel.add(new Button(Submit,new ClickListener(){
public void onClick(Widget sender){
form.setAction(GWT.getModuleBaseURL()+uploadWork) ;
form.submit();
}
}));
我在servlet中使用这些代码行获取这些参数:
protected void doPost(HttpServletRequest req,HttpServletResponse resp)
抛出ServletException,IOException {
System.out.println(ENTRAA BIENNNN );
System.out.println(___ELEMENTO1+ req.getAttribute(WorkTitle));
System.out.println(___ELEMENTO3+ req.getParameterValues(WorkTitle));
但两者都会返回 NULL
p>
我该怎么办?
TIA!
大多数servlet容器不会自动解码 multipart / form-data
,所以 req.getParameter
(或 getParameterValues
或类似的getter)不会返回任何内容。
您必须使用库 Apache Commons FileUpload 或 Jetty的 MultiPartFilter
来解码 multipart / form-data
有效载荷。 / p>
请注意, req.getAttribute
与从请求中获取数据无关;它用于在服务器组件之间(servlet容器和servlet之间,或者过滤器和servlet之间)传递与请求相关的数据。
I´m trying to catch the next two highlighted fields in a servlet where I can get the uploaded file.
The source code is just the same as which is shown in a GWT FormSubmit class Javadoc
form.setEncoding(FormPanel.ENCODING_MULTIPART);
form.setMethod(FormPanel.METHOD_POST);
// Create a panel to hold all of the form widgets.
VerticalPanel panel = new VerticalPanel();
form.setWidget(panel);
// Create a TextBox, giving it a name so that it will be submitted.
final TextBox tb = new TextBox();
tb.setName("WorkTitle");
tb.setValue("WorkTitle");
panel.add(tb);
// Create a ListBox, giving it a name and some values to be associated
// with
// its options.
ListBox lb = new ListBox();
lb.setName("listBoxFormElement");
lb.addItem("foo", "fooValue");
lb.addItem("bar", "barValue");
lb.addItem("baz", "bazValue");
panel.add(lb);
// Create a FileUpload widget.
FileUpload upload = new FileUpload();
upload.setName("uploadFormElement");
panel.add(upload);
// Add a 'submit' button.
panel.add(new Button("Submit", new ClickListener() {
public void onClick(Widget sender) {
form.setAction(GWT.getModuleBaseURL()+"uploadWork");
form.submit();
}
}));
I´m getting these parameters with this code lines in my servlet:
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
System.out.println("ENTRAA BIENNNN");
System.out.println(" ___ELEMENTO1" + req.getAttribute("WorkTitle"));
System.out.println(" ___ELEMENTO3" + req.getParameterValues("WorkTitle"));
But both returns me NULL
.
How could I do?
TIA!
Most servlet containers do not decode multipart/form-data
automatically, so req.getParameter
(or getParameterValues
or similar getters) won't return anything.
You'll have to use a library such as Apache Commons FileUpload, or Jetty's MultiPartFilter
to decode multipart/form-data
payload.
As a side note, req.getAttribute
has nothing to do with getting data out of the request; it's used to pass data, related to a request, between server components (between the servlet container and servlets, or between a filter and a servlet, for instance)
这篇关于GWT将表单参数发送到一个servlet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!