0

I would like to programmatically create a form with fields etc, however i have not been able to find a public factory etc to create a WebForm(class). Once this is done i would like to then submit the form and have my servlet do stuff with the form.

One approach i noticed the tests use is to create a PseudoServer which is a simple socket server. The tests then eventually make a request to some url which replies with some arbitrary html which includes a form. The problem with this is i cant register my own custom servlet to do stuff.

Im thus stuck between wanting a form but being unable to create one, if i wish to unit servletunit.

  • Is there a way to submit forms to a servlet inside servlet unit ?
  • Is there a way to combine parts of httpunit the form submitting stuff w/ servlet unit ?

Im guessing probably not because it(httpunit) wants to submit a form via socket and servletunit does not use sockets at all.

As per Andrey's suggestion and my past experimenting i have attempted to to call numerous methods on WebRequest to attempt to communicate the stuff that exists in a form being posted to a server.

  • selectFile() - to pick the file to be uploaded
  • setHeaderField() to set content type/charset/encoding.
mP.
  • 18,002
  • 10
  • 71
  • 105

1 Answers1

1

You can use PostMethodWebRequest to send POST request to any HTTP URL:

WebRequest request = new PostMethodWebRequest(serverUrl);

And then just set form parameters directly in the request object:

request.setParameter('name', 'user1');
request.setParameter('password', '123456');
Andrey Adamovich
  • 20,285
  • 14
  • 94
  • 132
  • But i want that means i need a web server to return the "initial page". Why cant i just build the initial page in a simpler way...sorry im confused...atm, seems like a lot of stuffing about for something that should be simple. This page will thus return the page which is then submitted to servlet unit.. is that right ? – mP. May 25 '11 at 11:29
  • I was using something ver y similoar to what i have given but unfortunately it doesnt work if one attempts to submit a form with a file. Lots of headers on the server(inside servletunit) are missing like the charset etc. – mP. May 25 '11 at 11:30
  • multipart requests are slightly more complicated than an average form submit. – Adriaan Koster May 25 '11 at 12:22
  • @mP, I'm confused either :) May be you can update your question with some code example that you've tried? – Andrey Adamovich May 25 '11 at 18:20