0
I have the following software, it should allow me to pass a file, will send a message on the console to just positive access to a class and list the result on the page, I’m starting and do not know the reason for the following error:
The files are as follows::
Main.html
Neste arquivo inicio o programa, ele tem um input para o arquivo e um submit
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Manipulador</title>
</head>
<body>
<form method="get" action="ArqSQL.do">
<input type="file" name="arquivo" size="chars" />
<input type="submit" value="Envia Arquivo" />
</form>
</body>
</html>
Input.java file.
Este é o Servlet deve listar o conteúdo do arquivo, acredito que faça isto no console
package Model;
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;
public class EntradaArquivo extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
ArquivoSQL arqsql = new ArquivoSQL();
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); //Lista o que tem no seu arquivo
arqsql.trataArquivo(str);
}
buff.close();
}
}
web xml.
Este arquivo deve apontar para o servidor qual arquivo executar (EntradaArquivo.java) quando a função ArqSQL.do for usada
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<servlet>
<servlet-name>ManipuladorSQL</servlet-name>
<servlet-class>Model.EntradaArquivo</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ManipuladorSQL</servlet-name>
<url-pattern>/ManipuladorSQL/ArqSQL.do*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>Principal.html</welcome-file>
</welcome-file-list>
</web-app>
Arquivosql.java
Este só escreve uma mensagem no console, mas para frente deverá trabalhar no conteúdo antes de devolver à página
package Model;
public class ArquivoSQL {
public void trataArquivo(String str) {
if(str.equals("o")) {
System.out.println("achei a linha que tem escrito SELECT nela!!");
}
}
}
It’s my first Web program and I don’t know where I’m going wrong, what I want is to get a file to work on the content and list on the page, but at the moment just pass a OK is already good, if you need more information just say.
It worked, I changed the
servlet name
for Manipularsqlservlet and used the path you indicated, now changed the error toError 500 nullpointerexception
, I already know it’s on the line fromEntradaArquivo.java
String enderecoCompleto = context.getRealPath(endereco);
thegetRealPath
cannot translate the virtual address and returnsnull
, I will study a way to solve the problem, thank you.– Tiago Oliveira de Freitas
Good studies ai @Tiagooliveiradefreitas !
– Fulvius