Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

doGet method is called twice in a servlet running in tomcat 7 and created with IntelliJ Idea 12

I have created a simple servlet with only one System.out.println() method in doGet body, but when I am running it inside Tomcat 7 using IntelliJ Idea 12 I get the message that System.out.println() method prints twice.

This is my web.xml :

<?xml version="1.0" encoding="UTF-8"?>
<web-app 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_3_0.xsd"
           version="3.0">

    <servlet>
        <description>A simple servlet</description>
        <display-name>SimpleServlet</display-name>
        <servlet-name>SimpleServlet</servlet-name>
        <servlet-class>org.skiabox.myservlet.SimpleServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>SimpleServlet</servlet-name>
        <url-pattern>/SimpleServletPath</url-pattern>
    </servlet-mapping>
</web-app>

This is SimpleServlet.java :

package org.skiabox.myservlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class SimpleServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("Hello from GET method.");
    }
}

This is SimpleServletProject.iml :

<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
  <component name="FacetManager">
    <facet type="web" name="Web">
      <configuration>
        <descriptors>
          <deploymentDescriptor name="web.xml" url="file://$MODULE_DIR$/web/WEB-INF/web.xml" />
        </descriptors>
        <webroots>
          <root url="file://$MODULE_DIR$/web" relative="/SimpleServletProject" />
        </webroots>
      </configuration>
    </facet>
  </component>
  <component name="NewModuleRootManager" inherit-compiler-output="true">
    <exclude-output />
    <content url="file://$MODULE_DIR$">
      <sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
    </content>
    <orderEntry type="inheritedJdk" />
    <orderEntry type="sourceFolder" forTests="false" />
    <orderEntry type="library" scope="PROVIDED" name="Tomcat 7.0" level="application_server_libraries" />
  </component>
</module>

..and this is an image of Tomcat 7 settings :

Tomcat settings

like image 590
skiabox Avatar asked Nov 04 '22 02:11

skiabox


1 Answers

I've changed the url mapping to / and now I have both my simple jsp page running at http://localhost:8080/SimpleServerProject and my servlet running the doGet method just once!

like image 107
skiabox Avatar answered Nov 09 '22 06:11

skiabox