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.

9
  • using doGet method
    – S M
    Commented Nov 25, 2018 at 18:27
  • If you want a simple System.out.println statement in your servlet is that getting executed ? Is your servlet 100% working ? Commented Nov 25, 2018 at 18:29
  • 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. Commented Nov 25, 2018 at 18:35
  • 1
    @SM use a fixed path (put the file on the desktop for example). Commented Nov 25, 2018 at 18:36
  • 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. Commented Nov 25, 2018 at 18:55

1 Answer 1

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.

2
  • 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?, and many other similar questions on SO.
    – skomisa
    Commented Nov 25, 2018 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
    Commented Nov 25, 2018 at 20:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.