Reputation: 3723
I followed a tutorial showing how to submit a form in GAE and directed it to a servlet in the same app. When I submit the form the servlet responds but I can't read the data using getParameters() or any other method I searched for; I don't know if the problem is client or server or both. here is the entrypoint code:
public class HelloWorld implements EntryPoint {
public void onModuleLoad() {
// Create a FormPanel and point it at a service.
final FormPanel form = new FormPanel();
form.setAction("http:// won let me write the host:8888/helloworld/receive");
form.setEncoding(FormPanel.ENCODING_MULTIPART);
form.setMethod(FormPanel.METHOD_POST);
// Create a panel to hold all of the form widgets.
VerticalPanel panel = new VerticalPanel();
panel.setSpacing(10);
form.setWidget(panel);
// Create a TextBox, giving it a name so that it will be submitted.
final TextBox tb = new TextBox();
tb.setWidth("220");
tb.setName("textBoxFormElement");
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("item1", "item1");
lb.addItem("item2", "item2");
lb.addItem("item3", "item3");
lb.setWidth("220");
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 ClickHandler() {
@Override
public void onClick(ClickEvent event) {
form.submit();
}
}));
// Add an event handler to the form.
form.addSubmitHandler(new FormPanel.SubmitHandler() {
@Override
public void onSubmit(SubmitEvent event) {
// This event is fired just before the form is submitted.
// We can take this opportunity to perform validation.
if (tb.getText().length() == 0) {
Window.alert("The text box must not be empty");
event.cancel();
}
}
});
form.addSubmitCompleteHandler(new FormPanel.SubmitCompleteHandler() {
@Override
public void onSubmitComplete(SubmitCompleteEvent event) {
// When the form submission is successfully completed,
// this event is fired. Assuming the service returned
// a response of type text/html, we can get the result
// here.
Window.alert(event.getResults());
}
});
DecoratorPanel decoratorPanel = new DecoratorPanel();
decoratorPanel.add(form);
// Add the widgets to the root panel.
RootPanel.get().add(decoratorPanel);
}
I tried 20 different things on the servlet to no avail. here is the code
public class FormHandler extends HttpServlet
{
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
super.doPost(req, resp);
System.out.println("got to servlet doPost");
System.out.println(req.getParameter("textBoxFormElement"));
Enumeration e = req.getParameterNames();
while(e.hasMoreElements())
{
String s = e.nextElement().toString();
System.out.println(s);
}
System.out.println("done for now");
}
}
this is the output: got to servlet doPost....... ............null ........done for now
Upvotes: 0
Views: 1465
Reputation: 1221
your all code is right only one mistake you did.On servlet you get encrypted data so you can not get value so that's why you need to write code as follow because i already done in my project.
You need two jar file
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
try{
ServletFileUpload upload = new ServletFileUpload();
FileItemIterator iterator = upload.getItemIterator(req);
while (iterator.hasNext()) {
FileItemStream item = iterator.next();
InputStream stream = item.openStream();
if (item.isFormField()) {
byte[] str = new byte[stream.available()];
stream.read(str);
String pFieldValue = new String(str,"UTF8");
if(item.getFieldName().equals("textBoxFormElement")){
System.out.println("text Value : "+pFieldValue);
}
}
}
}catch (Exception e) {
e.printStackTrace();
}
}
hope it will help you
Upvotes: 3