gpt4 book ai didi

GWT 将表单参数发送到 servlet

转载 作者:行者123 更新时间:2023-12-01 11:54:44 25 4
gpt4 key购买 nike

我试图在 servlet 中捕获接下来两个突出显示的字段,我可以从中获取上传的文件。

源代码与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();
}
}));

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

但两者都返回我 NULL

我该怎么办?

TIA!

最佳答案

大多数 servlet 容器不会自动解码 multipart/form-data,因此 req.getParameter(或 getParameterValues 或类似的 getter)不会不返回任何东西。
您必须使用诸如 Apache Commons FileUpload 之类的库, 或 Jetty's MultiPartFilter解码 multipart/form-data 有效载荷。

作为旁注,req.getAttribute 与从请求中获取数据无关;它用于在服务器组件之间(例如,在 servlet 容器和 servlet 之间,或者在过滤器和 servlet 之间)传递与请求相关的数据

关于GWT 将表单参数发送到 servlet,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8324028/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com