You can create a helper class that will write the name and age parameters to given file.
For example
- Create a helper class that will have a method like writeToFile which takes three parameters (path, name, age)
- The method would open a file using given path, and append the name and age
- The servlet will use the helper to write the name and age information to the file.
For How to get the real path of a file, See
ServletContext.getRealPath
See
how to process the html form submission
- Do not write the IO code directly in servlet
- You may want to consider thread safety issues
And, why do you need to write it to a file ? If you want to just store it and retrieve later , a database is better at storing and querying then a simple text file.