How to pass a JSP file to Servlet?

Asked

Viewed 2,013 times

6

I have a page . jsp, where I have an input file. I need to open this file to use the information contained in it. Suppose it is a pdf. I receive this pdf, sent to Servlet for the purpose of using in my JAVA application. The easiest way would be to take the whole file path and send it in a string. But I don’t know if you can do it and how to do it! Another thing, was to take the file in . jsp itself and turn the whole content into string. Then pass this string to my JAVA application. But I don’t know how to do that either. Any solution? How can I take the file and send it to my application?

  • 1

    I did some tests with itext and realized that there are zilhares different ways to treat the pdf in question. From what I saw in another question you already have a certain familiarity with itext, so I ask: could you say exactly what would be your question regarding the manipulation of the pdf file? The way to send the pdf can be done as I did with the code below, the difference is that you will not use Filereader, but will use the methods that itext offers.

  • I never touched itext, but that’s okay. What I need is to open the pdf and go through all the text contained in it. I will go through each word in the pdf

  • 1

    Yes, maybe without knowing, but used, rs.. See: How to separate a PDF file row by row, in Java? and here: Class Pdftextextractor

2 answers

5


From what I understand you need to send a file through an input="file" type element and read the contents of it.

I will need to do a project like the answer I gave in your other question:

The big difference is that you will have to use a Filereader to read the file passed by your input on the home page.

Example:

index.html - a simple html that sends a file from an input file to a Servlet

<body>
    <form method="get" action="LeArq.do">
        <input type="file" name="arquivo" size="chars" />
        <input type="submit" value="Envia Arquivo" />
    </form>
</body>

Java model. - a very simple class

package com.example.model;

public class Modelo {
    public void trataArquivo(String str) {
        if(str.equals("4")) {
            System.out.println("achei a linha que tem escrito 4 nela!!");
        }
    }
}

Leinputfile.java - your Servlet, mapped to DD as "Learq.do"

package com.example.web;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.example.model.Modelo;

public class LeInputFile extends HttpServlet{
    private static final long serialVersionUID = 1L;
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        Modelo modelo = new Modelo();

        String endereco = req.getParameter("arquivo");
        ServletContext context = getServletContext();  
        String enderecoCompleto = context.getRealPath(endereco);
        BufferedReader buff = new BufferedReader(new FileReader(enderecoCompleto));
        String str;
        while((str = buff.readLine()) != null) {
            System.out.println(str); //apenas para você saber o que tem no seu arquivo
            modelo.trataArquivo(str);
        }
        buff.close();
    }
}

If you read a file that has the following content:

a test
second line
three
4
5

The exit will be:

a test
second line
three
4
I found the line that has written 4 on it!!
5

I did this example on a local server, maybe for a remote server you have to concatenate the full path where the file will be, but I have no way to test it now, as soon as I test it I return you.

  • with the Bufferedreader I can open . PDF?

  • 1

    @Peace that gives gives, but it is not as simple as reading the txt, let me take a look here and I already tell you

  • @Have you been able to read the text file? I’m running some tests with the pdf.

  • in my project, I can’t use txt, I can only use pdf.

  • discovered something @Math ?

  • @Peacemaker found that I don’t know exactly what you need, rs.. You’ve messed with itext before, so if you can upload a pdf then just deal with itext instead of Filereader. Did you see my question here?

Show 1 more comment

2

Buddy, this is called file upload =)

Here are some great references on the subject: Locaweb and codigofontes.com.br. Both have super simple code to understand, I have nothing else to add.

Apart from that it seems to me that you did not understand well the "WEB" issue of the thing. Your file . jsp is processed on the server, but ultimately it is interpreted by the client browser... Servlet is a simple class that stays on the server and accepts requests through socket. Note that one stays in the client another in the server, the communication between these two tips is done through the HTTP protocol, it is not simply "pass" from one to the other.

I suggest you try to understand how it works (HTTP) request / response before anything.

Att

Browser other questions tagged

You are not signed in. Login or sign up in order to post.