Oimundoservlet.java - #algaworks

Asked

Viewed 289 times

0

I am following the E-book "Java EE7 With JSF, Primefaces and CDI". I created the Maven project, edited the file pom.xml how the workbook guides, when creating the example file OiMundoServlet.java, I checked that appeared the errors that the booklet informs, but while editing again the file pom.xml to eliminate errors, these same errors are not gone. How to fix this?

  • Note 1: I am using the latest version of Eclipse Java EE (Mars.1 Release (4.5.1))
  • Note 2: When I added apache server 8, when linking the Maven project I found the following error: Project facet Java version 1.8 is not supported.
  • Note 3: The file web.xml was not created automatically. I had to create it because it was an error in the file pom.xml in line with <packaging>war</packaging>.

Follows the code:

package br.com.farmsystem.servlet;//essa linha apresenta erro

import java.io.IOException;//essa linha apresenta erro
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* Servlet implementation class OiMundoServlet
 */
@WebServlet("/oi-mundo")
public class OiMundoServlet extends HttpServlet { //essa linha apresenta erro

    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public OiMundoServlet() {
        super();//essa linha apresenta erro
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//essa linha apresenta erro
        // TODO Auto-generated method stub
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//essa linha apresenta erro
        // TODO Auto-generated method stub
    }

}

3 answers

1

For the latest version of the platform Java EE 7, the web.xml is no longer mandatory, is opitional.

But just in case, choose to use for reasons like other frameworks settings, you can use the latest version of Servlet (3.1).

Servlet 3.1 - Descriptor

<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>

No eclipse, when adding a new server in the tab New Server, should you choose Apache Tomcat 8, click on the button Add, in JRE choose version 8 of Java, but first make sure that on your machine, you have the latest version of Java installed.

inserir a descrição da imagem aqui

Another alternative is to go on Properties of the project, in Project Facets on the flap Runtime button New and choose the application server.

inserir a descrição da imagem aqui

Also make sure that in Project Facets is the version 1.8 java.

In the pom.xml to specify version 8 of Java, can be done by maven-compiler-plugin

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.3</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
      </plugins>
   </build>

And to make it clear Maven that the web.xml does not need to be verified or detected, can be done through the maven-war-plugin so it will not generate any error if it does not exist.

<plugin>
     <groupId>org.apache.maven.plugins</groupId>
     <artifactId>maven-war-plugin</artifactId>
     <version>2.6</version>
     <configuration>
          <failOnMissingWebXml>false</failOnMissingWebXml>
     </configuration>
</plugin>

Other Developers - Java EE 7

http://antoniogoncalves.org/2013/06/04/java-ee-7-deployment-descriptors/

0

See if this helps you. Creating the WEB-INF folder and the web.xml file

inserir a descrição da imagem aqui

-1

error: Project Facet Java version 1.8 is not supported.

Which version of Java do you use? to find out: in the terminal type java -version. Change the pom.xml according to the version you use.

Ex.: java -version -> 1.7.x

in the pom.xml: replace the references of java 1.8 for 1.7

Browser other questions tagged

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