Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/43.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Servlets 如何显示从.txt到servlet的所有数据_Servlets - Fatal编程技术网

Servlets 如何显示从.txt到servlet的所有数据

Servlets 如何显示从.txt到servlet的所有数据,servlets,Servlets,如何将.txt文件中的数据显示到servlet中,我遇到了一个问题 我要做的是只显示从.txt文件到servlet的最后一个数据 下面是示例代码 //index.html <!DOCTYPE html> <html> <head> <meta charset="ISO-8859-1"> <title>Insert title here</title> </head> <body> <fo

如何将.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>

根据我对代码的理解,随着while循环的进行,用于显示结果的变量将被最新值覆盖,最终只存储文件的最后一行

您可以使用集合变量来存储字符串输出的每次出现(对应于文本文件中的一行),这不会导致覆盖相同变量上的数据

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");

    }

}
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();
    }

}
kings of nuts,nuts,premium,small
kings of nuts2,nuts,premium,medium