How to pass a file from the JSP to the servlet?


I have a .jsp page where I have an input file. I need to open this file to use the information in it. Suppose it's a pdf. I get this pdf, sending it to the servlet for the purpose of using it in my JAVA application. The easiest way would be to take the whole path of the file and send it in a string. But I do not know if you can do this or how you do it! Another thing, was to get the file in the same .jsp and turn the whole content into string. So pass this string to my JAVA application. But I do not know how to do that either. Any solution? How can I get the file and send it to my application?

asked by anonymous 08.07.2014 / 17:01

2 answers


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

I'll 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.


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

    <form method="get" action="">
        <input type="file" name="arquivo" size="chars" />
        <input type="submit" value="Envia Arquivo" />
</body> - 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!!");
} - your servlet, mapped to DD as ""

package com.example.web;

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

If you read a file that has the following content:


a test
  second line

The output will be:


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

I made 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 do not have how to test it now, so I test I'll return you.

08.07.2014 / 21:56

Friend, this is called file upload =)

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

It seems to me that you did not quite understand the "WEB" issue of the thing. Your .jsp file is processed on the server, but at the end of the day it is interpreted by the client browser ... The servlet is a simple class that stays on the server and accepts socket requests. Note that one is in the other client on the server, communication between these two points is done through the HTTP protocol, it is not simply "move" from one to another.

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


08.07.2014 / 21:29