I'm a little confused. Is the first part
servlet code? ...Or is this whole listing a
JSP with a
Java scriptlet? The basic process would be something like the following:
You have a controller class that you would call initially to set up any required data and possibly handle the submit from your form. In this model,
you should define a class that represents your form backing object. Note that there are a myriad of ways to define a controller. This is just one of the simpler ways to do it.
Then you could initialize it in a controller
Now in the JSP that is your form page, you just need the following snippet
When you define things this way, Spring does most of the work of binding the data from the form backing object onto the form and back into the object on submission.
If you need more complex binding, then you should consider a property editor. It's possible to actually bind a custom object to the form using custom property editors, which is where the power of Spring and MVC are realized.
In your situation, it might be that the UserSession might be your form backing object that would be set from the controller. All you need to do is set it in the model with a name that you will use to reference it by in your form. Behind the scenes, the framework is setting this in either a request attribute or a session attribute. By the way, I demonstrated using a request attribute. You can also make this a session attribute by adding one annotation.
I'd recommend getting and reading the latest Spring Reference documentation at
http://www.springsource.org/documentation or the book "Spring Recipes" by Gary Mak as a starting point. I'd also recommend looking at the sample code provided by SpringSource. They have some really nice examples of how to do this. Have a look at:
http://blog.springsource.com/2010/07/22/spring-mvc-3-showcase/?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+Interface21TeamBlog+%28SpringSource+Team+Blog%29