How do you create a Servlet class to handle a form inputs. It needs to get param
ID: 3820027 • Letter: H
Question
How do you create a Servlet class to handle a form inputs. It needs to get parameters from the form and then use them to create an instance of the JavaBean. It should record the JavaBean data field values in a .txt file in the WEB-INF folder with one record per line. After that, it should add the JavaBean instance as an attribute to the session and forward the request and response to the JSP.
Do I need to create the .txt file? If so, how?
Please provide a screen shot, with a step by step explanation to the whole process. Thanks.
Explanation / Answer
To create a file you need to follow the following steps
Step1 : ServletConfig scfg= getServletConfig();
Step2 : ServletContext scxt = scfg.getServletContext();
Step3 : String webInfPath = sxct.getRealPath("WEB-INF");
And then use the webInfPath to create a File object inside WEB-INF
file newFile = new File(webInfPath + "/fileName.txt");
Here is an another wy to create a .txt file in WEB-INF
String webInfPath = getServletConfig().getServletContext().getRealPath("WEB-INF");
In this case if you cant able to create a file make sure you applaction have the permissions to write.
in this case you can get the path using following
String path=Thread.currentThread().getContextClassLoader().getResource("com/youpackage/");
Now you get the path which is your class folder pathso you can get the WEB-INF path
Note : Remember when create file you must writer some content,otherwies it may not create.so it is ncessarry to create one recoed per line atleast.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.