Error running Java program - Mapping Servlet Web.xml

Asked

Viewed 313 times

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:

Console após o erro

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.

2 answers

1

The @Fulvius answer is correct, I include this to record in this question that it is possible to map a Servlet 3.0 in their own class with an annotation @WebServlet, as it was in the file Entradafile.java:

@WebServlet("/ArqSQL.do")
public class EntradaArquivo extends HttpServlet {

1


Maybe there’s a mix-up with the name of the project (ManipuladorSQL), q becomes a context in Tomcat, and Servlet (also ManipuladorSQL) you registered on web.xml.

Anyway, change this entry to web.xml from

<url-pattern>/ManipuladorSQL/ArqSQL.do*</url-pattern>

for

<url-pattern>/ArqSQL.do</url-pattern>

I believe this will work. But here’s a tip for you not to get confused: Usually put suffix in the classes according to its functionality or from whom it inherits (Manipularsqlservlet, Enderecoentity, Consultcepservice, etc.).

  • 1

    It worked, I changed the servlet name for Manipularsqlservlet and used the path you indicated, now changed the error to Error 500 nullpointerexception, I already know it’s on the line from EntradaArquivo.java String enderecoCompleto = context.getRealPath(endereco); the getRealPath cannot translate the virtual address and returns null, I will study a way to solve the problem, thank you.

  • Good studies ai @Tiagooliveiradefreitas !

Browser other questions tagged

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