无法将servlet打印到文本文件

时间:2015-04-21 22:49:09

标签: java servlets

到目前为止,这是我的代码,想要在没有本地路径的情况下进行打印,但只在我从

写入路径时打印
C:\Users\Javier\Documents\NetBeansProjects\HW5JavierSantana\web\WEB-INF\inputs.txt

似乎无法以任何其他方式使用它。非常感谢任何和所有的帮助。提前谢谢。

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    Writer writer = null;

    String usersLogin = request.getParameter("name");
    String usersTitle = request.getParameter("title");
    String usersName = request.getParameter("username");
    String usersSupervisor = request.getParameter("supervisor");
    String usersPassword = request.getParameter("password");

    response.setContentType("text/plain");

    String file = this.getServletContext().getRealPath("/WEB-INF/inputs.txt");
    System.out.println(file);//<-- prints correct/expected path            
    try{
        writer = new BufferedWriter(new PrintWriter(file));
        writer.write("Employee Info ");
        writer.write(" | UserName: " + usersLogin +
        " | Title: " + usersTitle +
        " | Name: " + usersName +
        " | Supervisor: " + usersSupervisor + 
        " | Password: " + usersPassword);
    } catch (FileNotFoundException e){
        e.printStackTrace();
    } catch (IOException e){
        e.printStackTrace();
    } finally {
        writer.close();
    }

    String url = "/WEB-INF/registered.jsp";
    getServletContext().getRequestDispatcher(url).forward(request, response);

}

日志输出为:

...
INFO: Reloading Context with name [/HW5JavierSantana] has started Apr 21, 2015 7:33:19 PM org.apache.catalina.core.StandardContext reload INFO: Reloading Context with name [/HW5JavierSantana] is completed
C:\Users\Javier\Documents\NetBeansProjects\HW5JavierSantana\build\web\WEB-INF\in‌​puts.txt

1 个答案:

答案 0 :(得分:0)

您是否注意到打印路径中的额外构建目录?它不存在于您提问的路径中。