-1

I wrote very simple dynamic web application in eclipse Mars(4.5.1) but i cannot start the tomcat server from eclipse. Below is the root cause of the error:

Caused by: java.lang.IllegalArgumentException: The servlets named [myservlet] and [MyServlet] are both mapped to the url-pattern [/MyServlet] which is not permitted
      at org.apache.tomcat.util.descriptor.web.WebXml.addServletMapping(WebXml.java:308)
      at org.apache.catalina.startup.ContextConfig.processAnnotationWebServlet(ContextConfig.java:2342)
      at org.apache.catalina.startup.ContextConfig.processAnnotationsStream(ContextConfig.java:2024)
      at org.apache.catalina.startup.ContextConfig.processAnnotationsWebResource(ContextConfig.java:1918)
      at org.apache.catalina.startup.ContextConfig.webConfig(ContextConfig.java:1139)
      at org.apache.catalina.startup.ContextConfig.configureStart(ContextConfig.java:771)
      at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:305)
      at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:95)
      at org.apache.catalina.util.LifecycleBase.fireLifecycleEvent(LifecycleBase.java:90)
      at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5154)
      at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
      ... 6 more

I followed this steps- addeding libraries in Properties -> Java Build Path -> Add Libraries -> Server runtime -> Apache. After that i added Windows -> Preferences -> Serevr -> Runtime Environments -> Apache. Tomcat v8.0 Server is perfectly starting from **C:\Program Files\Apache Software Foundation\Tomcat 8.0\bin **.

My 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>2J2EEProcessingFromData</display-name>

  <servlet>
    <servlet-name>myservlet</servlet-name>
    <servlet-class>Testing</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>myservlet</servlet-name>
    <url-pattern>/MyServlet</url-pattern>
  </servlet-mapping>


  <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>
</web-app>

MyServlrt.java

public class MyServlet extends HttpServlet {

protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
    response.setContentType("text/html;charset=ISO-8859-1");
    PrintWriter pw = response.getWriter();
    try{
        String username = request.getParameter("username");
        String password = request.getParameter("pass");

        pw.write("Hello "+username);
        pw.write("Your password is:"+password);
    } catch(Exception e){
        e.printStackTrace();
    } finally {
        pw.close();
    }
}

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
    PrintWriter pw = response.getWriter();
    pw.write("doGet called");
    processRequest(request, response);
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
    PrintWriter pw = response.getWriter();
    pw.write("doPost called");
    processRequest(request, response);
}

@Override
public String getServletInfo(){
    return "Shord description";
}

}

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Shashi Shiva L
  • 495
  • 11
  • 27

2 Answers2

0

Click on Servers Tab double-click on Tomcat then change HTTP port in Ports section to any others . or Open Server.xml file and change Connector Port . Restart the Server and then Check

Anand Dwivedi
  • 1,452
  • 13
  • 23
0

It's funny because i'm answering my own question i replaced url-pattern with (.java) extension as below.

<url-pattern>/MyServlet.java</url-pattern>
<url-pattern>/MyServlet</url-pattern>
Shashi Shiva L
  • 495
  • 11
  • 27