2
respostas

Problema com a servlet

Ao acessar http://localhost:8080 eu consigo acesso à página index.jsp perfeitamente. Mas não consigo acessar a servlet através de http://localhost:8080/contato e nem http://localhost:8080/loja/contato. Aparece a mensagem: HTTP ERROR 404, Problem accessing /contato, Reason: Not Found.

Segue o pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>br.com.alura.maven</groupId>
    <artifactId>lojaweb</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>lojaweb Maven Webapp</name>
    <!-- FIXME change it to the project's website -->
    <url>http://www.example.com</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
        </dependency>
    </dependencies>

    <build>
        <finalName>lojaweb</finalName>
        <pluginManagement><!-- lock down plugins versions to avoid using Maven 
                defaults (may be moved to parent pom) -->
            <plugins>
                <plugin>
                    <artifactId>maven-clean-plugin</artifactId>
                    <version>3.0.0</version>
                </plugin>
                <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
                <plugin>
                    <artifactId>maven-resources-plugin</artifactId>
                    <version>3.0.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.7.0</version>
                </plugin>
                <plugin>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <version>2.20.1</version>
                </plugin>
                <plugin>
                    <artifactId>maven-war-plugin</artifactId>
                    <version>3.2.0</version>
                </plugin>
                <plugin>
                    <artifactId>maven-install-plugin</artifactId>
                    <version>2.5.2</version>
                </plugin>
                <plugin>
                    <artifactId>maven-deploy-plugin</artifactId>
                    <version>2.8.2</version>
                </plugin>
                <plugin>
                    <groupId>org.eclipse.jetty</groupId>
                    <artifactId>jetty-maven-plugin</artifactId>
                    <version>9.3.7.v20160115</version>
                    <configuration>
                        <scanIntervalSeconds>10</scanIntervalSeconds>
                        <webApp>
                            <contextPath>/loja</contextPath>
                        </webApp>
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

O web.xml

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
         http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
</web-app>

E a servlet

@SuppressWarnings("serial")
@WebServlet(urlPatterns={"/contato"})
public class ContatoWeb extends HttpServlet {
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PrintWriter writer = resp.getWriter();
        writer.println("<html><h2>Entre em contato</h2></html>");
        writer.close();
    }
}
2 respostas

E não estou conseguindo dar o jetty:run

[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building lojaweb Maven Webapp 1.0.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] >>> jetty-maven-plugin:9.3.7.v20160115:run (default-cli) > test-compile @ lojaweb >>>
[INFO] 
[INFO] --- maven-resources-plugin:3.0.2:resources (default-resources) @ lojaweb ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\Users\Junior\OneDrive\Documentos\Desenvolvimento Java\Alura\Workspace\lojaweb\src\main\resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.7.0:compile (default-compile) @ lojaweb ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:3.0.2:testResources (default-testResources) @ lojaweb ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory C:\Users\Junior\OneDrive\Documentos\Desenvolvimento Java\Alura\Workspace\lojaweb\src\test\resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.7.0:testCompile (default-testCompile) @ lojaweb ---
[INFO] No sources to compile
[INFO] 
[INFO] <<< jetty-maven-plugin:9.3.7.v20160115:run (default-cli) < test-compile @ lojaweb <<<
[INFO] 
[INFO] --- jetty-maven-plugin:9.3.7.v20160115:run (default-cli) @ lojaweb ---
[INFO] Logging initialized @4067ms
[INFO] Configuring Jetty for project: lojaweb Maven Webapp
[INFO] webAppSourceDirectory not set. Trying src\main\webapp
[INFO] Reload Mechanic: automatic
[INFO] Classes = C:\Users\Junior\OneDrive\Documentos\Desenvolvimento Java\Alura\Workspace\lojaweb\target\classes
[INFO] Context path = /loja
[INFO] Tmp directory = C:\Users\Junior\OneDrive\Documentos\Desenvolvimento Java\Alura\Workspace\lojaweb\target\tmp
[INFO] Web defaults = org/eclipse/jetty/webapp/webdefault.xml
[INFO] Web overrides =  none
[INFO] web.xml file = file:///C:/Users/Junior/OneDrive/Documentos/Desenvolvimento%20Java/Alura/Workspace/lojaweb/src/main/webapp/WEB-INF/web.xml
[INFO] Webapp directory = C:\Users\Junior\OneDrive\Documentos\Desenvolvimento Java\Alura\Workspace\lojaweb\src\main\webapp
[INFO] jetty-9.3.7.v20160115
[INFO] Started o.e.j.m.p.JettyWebAppContext@1767781{/loja,file:///C:/Users/Junior/OneDrive/Documentos/Desenvolvimento%20Java/Alura/Workspace/lojaweb/src/main/webapp/,AVAILABLE}{file:///C:/Users/Junior/OneDrive/Documentos/Desenvolvimento%20Java/Alura/Workspace/lojaweb/src/main/webapp/}
[INFO] Jetty server exiting.
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.864 s
[INFO] Finished at: 2018-05-10T14:45:36-03:00
[INFO] Final Memory: 11M/28M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.eclipse.jetty:jetty-maven-plugin:9.3.7.v20160115:run (default-cli) on project lojaweb: Failure: Address already in use: bind -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

Boa tarde Hilton, olha pelo erro que deu no maven a porta que você está subindo o jetty já tem algum serviço rodando