gpt4 book ai didi

java - 无法让 servlet 打印到文本文件

转载 作者:太空宇宙 更新时间:2023-11-04 13:56:18 26 4
gpt4 key购买 nike

到目前为止,这是我的代码,希望在没有本地路径的情况下打印它,但它仅在我写入路径时才打印

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

最佳答案

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

关于java - 无法让 servlet 打印到文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29784558/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com