Error running Java program - Mapping Servlet Web.xml

0

I have the following software, it should allow me to pass a file, it will issue a message in the console just to positively access a class and list the result on the page, I'm starting and I do not know the reason for the following error:

Thefilesareasfollows:

Principal.htmlNestearquivoiniciooprograma,eletemuminputparaoarquivoeumsubmit

<!DOCTYPEhtml><html><head><metacharset="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>

Entry.java 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>

SQL.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 do not know where I'm going wrong, what I want is to get a file to work on the content and list it on the page, but at the moment just passing on an OK is already good, if you need more information just talk.

    
asked by anonymous 28.10.2015 / 23:42

2 answers

1

Maybe you have a confusion with the project name ( ManipuladorSQL ), which makes a context in tomcat, and the servlet (also ManipuladorSQL ) that you have registered in web.xml .

However, change this entry in the web.xml from

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

for

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

I believe that this will work. But here's the tip for you to get in the way: custom put suffix in the classes according to the functionality of it or who it inherits (SQLServer, EnderecoEntity, QueryEPService, etc.).

    
30.10.2015 / 19:48
1

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

@WebServlet("/ArqSQL.do")
public class EntradaArquivo extends HttpServlet {
    
31.10.2015 / 20:10