[英]Java servlet to write text file in Windows 10, NetBeans and Glassfish
I am trying to write a Java servlet that receives a simple post and writes the contents as a text file.我正在尝试编写一个 Java servlet,它接收一个简单的帖子并将内容写入文本文件。 I am developing in NetBeans 8.2 with Glassfish on a Windows 10 machine.
我正在 Windows 10 机器上使用 Glassfish 在 NetBeans 8.2 中进行开发。 I can write a text file via a Java applet , but the same file-write code does not work in the servlet.
我可以通过 Java applet编写文本文件,但是相同的文件写入代码在 servlet 中不起作用。 I am fairly new to NetBeans and Glassfish.
我对 NetBeans 和 Glassfish 相当陌生。
I have html that does the simple post.我有做简单帖子的html。 I generated the skeleton code of the servlet in NetBeans.
我在 NetBeans 中生成了 servlet 的框架代码。 I believe that the html is making a post to the correct location because it launches the index.html file that was automatically created by NetBeans.
我相信 html 正在将帖子发布到正确的位置,因为它启动了由 NetBeans 自动创建的 index.html 文件。 However, no text file is created.
但是,不会创建任何文本文件。 I do not see error messages within Netbeans, but I might not know where to look.
我在 Netbeans 中没有看到错误消息,但我可能不知道在哪里查看。 I occasionally see an empty file named gfv3ee6.dpf created in the /build/web folder after a post.
我偶尔会看到一个名为 gfv3ee6.dpf 的空文件在发布后在 /build/web 文件夹中创建。 I am running NetBeans and the html as administrator.
我以管理员身份运行 NetBeans 和 html。
I enclose my html and current Java code.我附上了我的 html 和当前的 Java 代码。 I have tried many variations (different functions, different ways of defining the file path) discussed on Stackoverflow, but have not found the correct combination.
我尝试了 Stackoverflow 上讨论的许多变体(不同的功能,定义文件路径的不同方式),但没有找到正确的组合。 I am enclosing one of the more simple examples that I have tried.
我附上了我尝试过的更简单的例子之一。 Once I am past the hurdle of file-write, I would like to make future writes an append operation.
一旦我克服了文件写入的障碍,我想让未来的写入成为附加操作。
I would also appreciate any suggestions for best practices for when I ultimately deploy my html and servlet to a server.当我最终将我的 html 和 servlet 部署到服务器时,我也很感激任何关于最佳实践的建议。 My overall goal is for the html be a long questionnaire that posts the responses to the Java servlet, wherein the servlet then scores the responses and sends a pdf file to the user.
我的总体目标是让 html 成为一个长问卷,将响应发布到 Java servlet,然后 servlet 对响应进行评分并将 pdf 文件发送给用户。 I have actually completed all this business logic (without the post) in an applet, but I cannot perform a simple file-write in a servlet.
我实际上已经在小程序中完成了所有这些业务逻辑(没有 post),但是我无法在 servlet 中执行简单的文件写入。
To me, it appears that servlets and applets write files differently and that I cannot find the correct code for servlets.对我来说,servlet 和 applet 编写文件的方式似乎不同,而且我找不到 servlet 的正确代码。
I do not think this is an issue, but the name of the project is JustReact5, whereas the name of the class is Just5React.我不认为这是一个问题,但是项目的名称是 JustReact5,而类的名称是 Just5React。
HTML HTML
<form action="http://localhost:8080/JustReact5" method="post">
Your name:<br>
<input type="text" name="yourname" <br>
<input type="submit" value="Submit">
</form>
Java爪哇
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
* @author joseph
*/
public class Just5React extends HttpServlet {
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter pw = response.getWriter();
String yourname = request.getParameter("yourname");
try{
File file = new File("C:/temp/outputfile.txt");
FileWriter fstream = new FileWriter(file,true);
BufferedWriter out = new BufferedWriter(fstream);
out.write(yourname);
out.newLine();
out.close();
}
catch (Exception e){
System.err.println("Error: " + e.getMessage());
}
}
// NetBeans doGet, doPost, getServletInfo code
// omitted for brevity
}
web.xml网页.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<servlet>
<servlet-name>Just5React</servlet-name>
<servlet-class>Just5React</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Just5React</servlet-name>
<url-pattern>/Just5React</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
</web-app>
Kindly find the piece of code that might help.请找到可能有帮助的代码。 I have used Java 8 for storing response to a file.
我使用 Java 8 来存储对文件的响应。
response.getWriter().append("Served at: ").append(request.getContextPath());
String emailID = request.getParameter("email");
String password = request.getParameter("psw");
String password_repeat = request.getParameter("psw-repeat");
String remember = request.getParameter("remember");
List<String> responseList = new ArrayList<>();
responseList.add(emailID);
responseList.add(password);
responseList.add(password_repeat);
responseList.add(remember);
System.out.println("Writing data : " + responseList);
writeResponseToFile(responseList);
private void writeResponseToFile(List<String> responseList) {
String filePath = "/Users/sunilchityala/Desktop/writeResponseToFile.txt";
try {
Files.write(Paths.get(filePath), responseList, StandardCharsets.UTF_8, StandardOpenOption.CREATE, StandardOpenOption.APPEND);
} catch (IOException e) {
e.printStackTrace();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.