java - How writing list from servlet to .txt -


I created the servlet that sorts 10 random values ​​and prints this value. This value is adding Linkedlists when I close the application so how can I save the link of the file to LinkedList in file.txt? I try to write the file in the servlet and make some but my code is not making the file.

  Protects Zero ProcessRequest (HttpServletRequest request, HttpServletResponse response) throws, ServletException, IOException {response.setContentType ("text / html; charset = UTF-8"); PrintWriter out = response.getWriter (); {/ * Try Todd Output on your page. You can use the following sample code * / out.printline ("& lt; html & gt;"); Out.println ("top> gt;"); Out.println ("& gt; Title & gt; Servlet NewServlet & lt; / Title & gt;"); Out.println ("& lt; / head>"); Out.println ("k & lt; body & gt;"); Out.println ("& lt; h1 & gt; Servlet Newsletter" + request.getContextPath () + "& lt; / h1 & gt;"); & Lt; Integer & gt; Lista = (list & lt; integer & gt;) this.getServletContext (). GetAttribute ("list"); Out.println ("& lt; ul & gt;"); For (integer l: list) {out.println ("
  • " + l + ""); } Out.println ("& lt; / ul>"); Out.println ("& lt; form action = \" newservelet 1 \ "& gt;"); Out.println ("& lt; input type = \" text \ "name = \" liczba \ "& gt;"); Out.println ("& lt; input type = \" submit \ "value = \" add \ "& gt;"); Out.println ("& lt; / form & gt;"); Out.println ("& lt; / body & gt;"); Out.println ("& lt; / html>"); } Finally {out.close (); } PrintWriter zapis = null; Zapis = new print-वाटर ("test.txt"); Zapis.println ("Type Type"); Zapis.close (); }

  •   public class NewServletListener applies ServletContextListener {permanent last public string defFileName = "file.txt"; String filename; @ Override Public Voice Context Start (Circalactex Event) {List & lt; Integer & gt; Lista = New Linkedist & lt; Integer & gt; (); Random rand = new random (); Int rozmiar = integer Parasent (sce.getServletContext (). GetInitParameter ("Rosier")); For (int i = 0; i  

    It is weird but is not making the file. Should I add to web.xml SAVE_FILE? Or do I do something wrong when I close the overlap then make the file?

    OK, I believe that somewhere you have a list & lt; String & gt; With the name, the servlet reference is stored in the form of attribute "lista" . You want to save it when the application stops in the text file, one string per line.

    You must use the ContextLoaderListener . Suppose you use a servlet 3.0+ container you can register it with annotation (if not, you will have to register it in the deployment descriptor web.xml . OnDestroy write file in: Method:

      @WebListener Public class SaveListener ServletContextListener {Fixed Last Public String defFileName = "file.txt"}; String fileName; @Override Public Zero contextInitialized (ServletContextEvent SCE) {/ .init parameters fileName = sce.getServletContext () getInitParameter ("SAVE_FILE") Allow file name to be detected, if (filename == faucet) {fileName = defFileName;}} @Override Public Zero contextDestroyed (ServletContextEvent SCE) {From the list & lt; string & gt; list = (list (String & gt;) sce.getServletContext () .getAttribute ("Lists"); File File = New File (File Name), Try (PrintWriter printWriter = New PrintWriter (File)) {string string: List) {printWriter.println (string); }} Hold (FileNotFoundException E) {New RuntimeException (E); }}}  

    Comments

    Popular posts from this blog

    java - Can't add JTree to JPanel of a JInternalFrame -

    javascript - data.match(var) not working it seems -

    javascript - How can I pause a jQuery .each() loop, while waiting for user input? -