1

サーバーを起動すると、次のエラーが発生します

localhost のサーバー Tomcat v7.0 サーバーを起動できませんでした。

コンソールでエラーを調べると、これは「[Myclass] および [mypropackage.Myclass] という名前のサーブレットが両方とも、許可されていない URL パターン [/myclass] にマップされている」という問題だと思います。

ここで何が問題で、どのように修正するのですか?

Web.xml コード

<?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">
   <display-name>myproject</display-name>
   <welcome-file-list>
      <welcome-file>index.html</welcome-file>
      <welcome-file>index.htm</welcome-file>
      <welcome-file>index.jsp</welcome-file>
      <welcome-file>default.html</welcome-file>
      <welcome-file>default.htm</welcome-file>
      <welcome-file>default.jsp</welcome-file>
   </welcome-file-list>
   <servlet>
      <servlet-name>Myclass</servlet-name>
      <servlet-class>mypropackage.Myclass</servlet-class>
   </servlet>
   <servlet-mapping>
      <servlet-name>Myclass</servlet-name>
      <url-pattern>/myclass</url-pattern>
   </servlet-mapping>
</web-app>

サーブレットクラスのコード:

package mypropackage;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
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 Myclass
 */
 @WebServlet("/myclass")
 public class Myclass extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public Myclass() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see Servlet#init(ServletConfig)
     */
    public void init(ServletConfig config) throws ServletException {
        // TODO Auto-generated method stub
    }

    /**
     * @see HttpServlet#service(HttpServletRequest request,    
  HttpServletResponse response)
     */
    protected void service(HttpServletRequest request,    
    HttpServletResponse response) throws ServletException, IOException {
        System.out.println("service method");
        String uname = "user";
        String pass = "abcd";
        String un = request.getParameter("username");
        String pw = request.getParameter("password");
        String msg = " ";
        if(un.equals(uname) && pw.equals(pass))
        {
            msg = "hello" + un + "login successful";
        }
        else
        {
            msg = "hello" + pw + "login failed";
        }

        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println(msg);

    }

  }

これはコンソールのログです:

Caused by: java.lang.IllegalArgumentException: The servlets named [Myclass] and [mypropackage.Myclass] are both mapped to the url-pattern [/myclass] which is not permitted
    at org.apache.catalina.deploy.WebXml.addServletMapping(WebXml.java:293)
    at org.apache.catalina.startup.ContextConfig.processAnnotationWebServlet(ContextConfig.java:2428)
    at org.apache.catalina.startup.ContextConfig.processAnnotationsStream(ContextConfig.java:2103)
    at org.apache.catalina.startup.ContextConfig.processAnnotationsFile(ContextConfig.java:2064)
    at org.apache.catalina.startup.ContextConfig.processAnnotationsFile(ContextConfig.java:2057)
    at org.apache.catalina.startup.ContextConfig.webConfig(ContextConfig.java:1304)
    at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:889)
    at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:386)
    at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:117)
    at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5412)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    ... 6 more
4

1 に答える 1

1

実際、サーブレットを 2 回定義しました。

  1. @WebServlet("/myclass")クラスへの注釈の使用mypropackage.MyClass

  2. web.xmlservlet を定義した場所を使用しますMyclass

サーブレットの両方のインスタンスが同じ URL にマップされており、これがコンテナーから通知されます。サーブレットを定義する方法の 1 つだけを使用する必要があります: 注釈の使用またはweb.xml. 私は使用することを好みますweb.xmlが、おそらくこれは、注釈が発明される何年も前にサーブレットを定義していたためです。そのため、ご自身で選択していただくようお願いいたします。

私見 web.xml を使用すると、より柔軟になります。開発時にサーブレットをマッピングする URL を決定する必要はなく、同じサーブレットを複数回デプロイして複数の URL にマッピングできます。

于 2015-02-24T18:08:33.217 に答える