我遇到了如何将全部数据从我的.txt文件显示到我的servlet的问题。
我的目的是显示仅我的.txt文件中的最后一个数据到servlet
这里是示例代码
// index.html的
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="add.html" method="POST">
<table>
<tr>
<td>Product name:</td>
<td><input type="text" name="pname" required></td>
</tr>
<tr>
<td>Category:</td>
<td><input type="text" name="cat" required></td>
</tr>
<tr>
<td>Sub:</td>
<td><input type="text" name="subcat" required></td>
</tr>
<tr>
<td>Size:</td>
<td><input type="radio" name="size" value="small">Small
<br>
<input type="radio" name="size" value="medium">Medium
<br>
<input type="radio" name="size" value="large">Large
</td>
</tr>
<tr>
<td><input type="submit" value="Add"></td>
</tr>
</table>
</form>
<a href = "view.html">view products</a>
</body>
</html>
// AddProductServlet.java
package com.nova.ed.process.admin;
import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/add.html")
public class AddProductServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String pname="";
String cat="";
String subcat="";
String size="";
if(request.getParameter("pname") != null)
{
pname = request.getParameter("pname");
}
if(request.getParameter("cat") != null)
{
cat = request.getParameter("cat");
}
if(request.getParameter("subcat") != null)
{
subcat = request.getParameter("subcat");
}
if(request.getParameter("size") != null)
{
size = request.getParameter("size");
}
File myFile = new File("product.txt");
if(!myFile.exists())
{
myFile.createNewFile();
}
PrintWriter pw = new PrintWriter(new FileWriter(myFile, true));
pw.println(pname+","+cat+","+subcat+","+size);
pw.close();
out.print("<p>Registration Successfull</p>");
out.print("click "+"<a href='index.html'>Here</a>"+" to add again");
}
}
// ViewProductServlet.java
package com.nova.ed.process.admin;
import java.io.IOException;
import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/view.html")
public class ViewProductServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public String pname = "";
public String cat = "";
public String subcat = "";
public String size = "";
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
File myFile = new File("product.txt");
BufferedReader br = new BufferedReader(new FileReader(myFile));
boolean isExist = false;
String str;
while((str = br.readLine()) != null)
{
String [] details = str.split(",");
pname = details[0];
cat = details[1];
subcat = details[2];
size = details[3];
isExist = true;
}
if(isExist){
out.print("<html>");
out.print("<head>");
out.print("<title></title>");
out.print("</head>");
out.print("<body>");
out.print("<h3>Products," + pname + " " + cat + " " + subcat + " " + size +"<h3>");
out.print("<h5>Click <a href='add.html'>here</a> to add more.</h5>");
out.print("</body>");
out.print("</html>");
}else{
}
br.close();
}
}
我还没有使用Bean和Beanfactory ..但是我会在解决这个问题后确保这样做..
注意:仍然开始请耐心等待..谢谢!
顺便说一句,这是我正在谈论的.txt文件:
// product.txt
kings of nuts,nuts,premium,small
kings of nuts2,nuts,premium,medium
答案 0 :(得分:0)
根据我对您的代码的理解,用于显示结果的变量随着while循环的进行而被覆盖最新值,最终最终只存储文件的最后一行。
您可以使用Collection变量来存储每次出现的String输出(对应于文本文件中的一行),这不会导致覆盖相同变量上的数据。