如何配置注释以映射servlet?

时间:2019-06-13 09:48:51

标签: java servlets annotations

我正在尝试在AppEngine上的项目中使用批注,但它们根本不起作用。我已经尝试了几乎所有配置它们的方法,但是它们仍然无法正常工作。

我正在将servlet 3.1.0与运行时Java8配合使用。

当我尝试在控制台中查找与批注映射的servlet时,我会读到:

 [java] WARNING: No file found for: /testServlet
     [java] giu 13, 2019 11:50:45 AM com.google.appengine.tools.development.jetty9.LocalResourceFileServlet doGet

这是我的文件:

pom.xml

    <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>com.mycompany</groupId>
  <artifactId>ProgettoEsame</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>

  <name>ProgettoEsame</name>
  <url>http://maven.apache.org</url>

        <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>


  <dependencies>
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
      <scope>provided</scope>
    </dependency>

    <dependency>
      <groupId>javax.servlet.jsp</groupId>
      <artifactId>jsp-api</artifactId>
      <version>2.1</version>
      <scope>provided</scope>
    </dependency>

    <dependency>
      <groupId>com.googlecode.objectify</groupId>
      <artifactId>objectify</artifactId>
      <version>5.1.1</version>
    </dependency>

        <dependency>
      <groupId>com.google.appengine.tools</groupId>
      <artifactId>appengine-gcs-client</artifactId>
      <version>0.7</version> 
    </dependency>

    <dependency>
      <groupId>com.google.code.gson</groupId>
      <artifactId>gson</artifactId>
      <version>2.8.5</version>
    </dependency>


  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
      <plugin>
        <groupId>com.google.appengine</groupId>
        <artifactId>appengine-maven-plugin</artifactId>
        <version>1.9.74</version>
      </plugin>    
    </plugins>
  </build>
</project>

尝试使用version =“ 3.0”和metadata-complete =“ false”,但一无所获 web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <display-name>ProgettoEsame</display-name>
    <servlet>
        <servlet-name>registra</servlet-name>
        <servlet-class>registra</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>registra</servlet-name>
        <url-pattern>/registra</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

appengine-web.xml

<?xml version="1.0" encoding="UTF-8"?>
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0"
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xsi:schemaLocation='http://kenai.com/projects/nbappengine/downloads/download/schema/appengine-web.xsd appengine-web.xsd'>
    <application>ProgettoEsame</application>
    <version>1</version>
    <runtime>java8</runtime>
    <sessions-enabled>true</sessions-enabled>
    <threadsafe>true</threadsafe>
</appengine-web-app>

Servlet:registra.java

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

import java.io.IOException;

import java.io.PrintWriter;
import javax.servlet.annotation.WebServlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Gabriel
 */

@WebServlet(name = "registra", urlPatterns = "/testServlet")
public class registra extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here. You may use following sample code. */
            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet registra</title>");            
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet registra at " + request.getContextPath() + "</h1>");
            ServletContext sc = request.getSession().getServletContext();
            out.println("Servlet version = " + sc.getMajorVersion() + "." + sc.getMinorVersion());
            out.println("</body>");
            out.println("</html>");
        } finally {
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}

1 个答案:

答案 0 :(得分:0)

您需要在{}中指定urlPatterns

@WebServlet(name =“ registra”,urlPatterns =“ {/ testServlet}”)

在这里您需要编辑成web.xml

  <servlet-name>registra</servlet-name>
  <url-pattern>/testServlet</url-pattern>

可以更改为web.xml

由于要使用多个属性,因此必须将网址格式指定为花括号。

来源:https://javatutorial.net/servlet-annotation-example