Seguindo o passo-a-passo da aula não é possível abrir a url
porque tive problemas de:
Description Resource Path Location Type Dynamic Web Module 3.1 requires Java 1.7 or newer. lojaweb line 1 Maven Java EE Configuration Problem
Então pesquisei no fórum e atualizei meu pom.xml (utilizar o java 8) para:
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>br.com.alura.maven</groupId>
<artifactId>lojaweb</artifactId>
<packaging>war</packaging>
<version>1.0.0-SNAPSHOT</version>
<name>lojaweb Maven Webapp</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.4.6.v20170531</version>
<configuration>
<scanIntervalSeconds>10</scanIntervalSeconds>
<webApp>
<contextPath>/test</contextPath>
</webApp>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<finalName>lojaweb</finalName>
</build>
</project>
Ajustei o compiler level, build path e project facets todos para utilizar o java 1.8 e servlet 3.1.
Atualizei 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>
meu servlet:
package br.com.alura.maven.lojaweb;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(urlPatterns="/contato")
public class ContatoServlet extends HttpServlet{
private static final long serialVersionUID = 1L;
@Override
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();
}
}
compilei: mvn copile executei o jetty: mvn jetty:run abri a url: http://localhost:8080/contato
resultado:
HTTP ERROR 404
Problem accessing /contato. Reason:
Not Found
Powered by Jetty:// 9.4.6.v20170531
adicionei o contexto (na aula não tem isso):
http://localhost:8080/lojaweb/contato
resultado:
HTTP ERROR 404
Problem accessing /lojaweb/contato. Reason:
Not Found
Powered by Jetty:// 9.4.6.v20170531
Editando:
após rever meu pom.xml vi que o contexto está test. Abri a url:
http://localhost:8080/test/contato
e funcionou.