1

I new with servlet programming and I am trying to create and write a file from following java method-

public void file() throws FileNotFoundException, UnsupportedEncodingException {
    PrintWriter writer = new PrintWriter("the-file-name.txt", "UTF-8");
    writer.println("The first line");
    writer.println("The second line");
    writer.close();
    System.out.println("file created");
}

And I want to call it from servlet like this

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    TryMethod tryMethod = new TryMethod();
    tryMethod.file();
}

can anyone suggest why it is not getting executed from servlet? This method gets called when called from the main method.

S M
  • 100
  • 12
  • using doGet method – S M Nov 25 '18 at 18:27
  • If you want a simple `System.out.println` statement in your servlet is that getting executed ? Is your servlet 100% working ? – Andrei Ciobanu Nov 25 '18 at 18:29
  • Is it not called or don't you see the output / the file? – Hauke Ingmar Schmidt Nov 25 '18 at 18:31
  • Hi @AndreiCiobanu ,yes servlet is working well, tried calling method that prints hello world – S M Nov 25 '18 at 18:31
  • @HaukeIngmarSchmidt cant see an output file – S M Nov 25 '18 at 18:32
  • 3
    @SM Are you sure the file is not created ? I suspect it's being created but you are looking in a different folder. As you are creating right now, you are creating it with a relative path. When you run the code as an web application (Servlet Container) your "current folder" might be different than the current folder of your IDE Main run. – Andrei Ciobanu Nov 25 '18 at 18:35
  • 1
    @SM use a fixed path (put the file on the desktop for example). – Andrei Ciobanu Nov 25 '18 at 18:36
  • @AndreiCiobanu worked after specifying path of desktop ,thank you so much – S M Nov 25 '18 at 18:47
  • 1
    @SM glad it work. I am sure that you will find the other files created "somewhere" (the "current dir" of your web app). To find out where they were created just println (from your servlet) the current path: `Paths.get("").currentRelativePath.toAbsolutePath().toString()` - you will find them there. – Andrei Ciobanu Nov 25 '18 at 18:55

1 Answers1

1

No there is typically no problem with running constructors or methods from a servlet (at least not for classes which are part of your WebApp). And if there where a problem you would get an Exception/error.

So in case you do not see the print output, it is likely the code was not executed. This could be a different method (doPost instead of doGet) or a different servlet (check your URL and servlet mapping) or it can be that your updated class was not deployed or found - or it was deployed but used another one.

The first thing I would to is to add a System.out to the doGet() method. As long as that one is not printed you do not need to worry about your own class.

Oh and to just state the obvious, your code uses the current run-directory of the web server (relative file name) which you might not have checked for the file. Try an absolute path or make it configurable. Your system.out shold however be printed regardless.

eckes
  • 10,103
  • 1
  • 59
  • 71
  • 1
    Somewhat off topic, but although there is _"no problem with running constructors...from a servlet"_, there is no compelling reason to do that; any initialization is usually done in `init()`. See the answers to [why we can't initialize a servlet using constructor itself?](https://stackoverflow.com/q/2920616/2985643), and many other similar questions on SO. – skomisa Nov 25 '18 at 19:44
  • Unfortunately servlets are multi threaded, so an object initialized in init() has to be threadsafe. Would be a big win if servlet standard offers pooled servlet instances like EJB used to do. – eckes Nov 25 '18 at 20:33