0

Environment:

  • Ubuntu 12.04
  • Apache Tomcat 7.0.27
  • Eclipse 4.2 (Juno)
  • Servlet 3.0
  • Openjdk 7

Trying to do a FileCounter with Servlet 3.0, I get this error on URL

http://localhost:8080/wtp.filecounter/.

Wouldn't have to be http://localhost:8080/wtp.filecounter/FileCounter?

Estado HTTP 404 -

type Informe de estado

mensaje

descripción El recurso requerido () no está disponible.

Apache Tomcat/7.0.26

Structure

wtp.filecounter
   dao
      FileDao.java
   servlets
      FileCounter.java

FileDao.java read and update visitors to a file

package wtp.filecounter.dao;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;

public class FileDao {

  public int getCount() {
    int count = 0;
    // Load the file with the counter
    FileReader fileReader = null;
    BufferedReader bufferedReader = null;
    PrintWriter writer = null ; 
    try {
      File f = new File("FileCounter.initial");
      if (!f.exists()) {
        f.createNewFile();
        writer = new PrintWriter(new FileWriter(f));
        writer.println(0);
      }
      if (writer !=null){
        writer.close();
      }

      fileReader = new FileReader(f);
      bufferedReader = new BufferedReader(fileReader);
      String initial = bufferedReader.readLine();
      count = Integer.parseInt(initial);
    } catch (Exception ex) {
      if (writer !=null){
        writer.close();
      }
    }
    if (bufferedReader != null) {
      try {
        bufferedReader.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
    return count;
  }

  public void save(int count) throws Exception {
    FileWriter fileWriter = null;
    PrintWriter printWriter = null;
    fileWriter = new FileWriter("FileCounter.initial");
    printWriter = new PrintWriter(fileWriter);
    printWriter.println(count);

    // Make sure to close the file
    if (printWriter != null) {
      printWriter.close();
    }
  }
} 

FileCounter.java servlet that read counter and write in text plain

package wtp.filecounter.servlets;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import wtp.filecounter.dao.FileDao;

/**
 * Servlet implementation class FileCounter
 */
@WebServlet("/FileCounter")
public class FileCounter extends HttpServlet {
  private static final long serialVersionUID = 1L;

  int count;
  private FileDao dao;

  public void init() throws ServletException {
    dao = new FileDao();
    try {
      count = dao.getCount();
    } catch (Exception e) {
      getServletContext().log("An exception occurred in FileCounter", e);
      throw new ServletException("An exception occurred in FileCounter"
          + e.getMessage());
    }
  }

  protected void doGet(HttpServletRequest request,
      HttpServletResponse response) throws ServletException, IOException {
    // Set a cookie for the user, so that the counter does not increate
    // everytime the user press refresh
    HttpSession session = request.getSession(true);
    // Set the session valid for 5 secs
    session.setMaxInactiveInterval(5);
    response.setContentType("text/plain");
    PrintWriter out = response.getWriter();
    if (session.isNew()) {
      count++;
    }
    out.println("This site has been accessed " + count + " times.");
  }

  public void destroy() {
    super.destroy();
    try {
      dao.save(count);
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
}

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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
  <display-name>wtp.filecounter</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>
</web-app>

Any idea about what's wrong?

When I run on server the project open ''http://localhost:8080/wtp.filecounter/'' instead of ''http://localhost:8080/wtp.filecounter/FileCounter'' and the second URL works

Joe
  • 7,749
  • 19
  • 60
  • 110

5 Answers5

0

Your servlet is mapped to /FileCounter (that's what the @WebServlet("/FileCounter") annotation does). And it is part of a webapp, deployed under some context path (let's say the context path is /myFirstWebApp). So the URL to invoke the servlet is:

http://localhost:8080/myFirstWebApp/FileCounter
JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • And where is defined the context path? Because what I did was ran on server, and http://localhost:8080/wtp.filecounter is the URL error I got. What it is missed is /FileCounter – Joe Nov 17 '12 at 16:49
  • Select your web project in eclipse, choose Project - Properties, go to "Web project settings", and you'll find the context root. – JB Nizet Nov 17 '12 at 16:54
  • I was follow this example but with the change of servlet 3.0 http://www.vogella.com/articles/EclipseWTP/article.html And there nothing is defined as context root and it worked ¿? – Joe Nov 17 '12 at 17:00
  • By default, the context root is the name of the war file deployed in tomcat, which is also the name of the eclipse project, AFAIK. – JB Nizet Nov 17 '12 at 17:01
  • I'm missing something because in his example the project is named de.vogella.wtp.filecounter and the URL of the servlet is de.vogella.wtp.filecounter/FileCounter (but in his case with servlet 2.5) eclipse adds the servlet settings to the web.xml file automatically, so we can't know what is in there – Joe Nov 17 '12 at 17:10
  • WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:wtp.filecounter' did not find a matching property. – Joe Nov 17 '12 at 17:23
  • When I run on server the project open http://localhost:8080/wtp.filecounter/ (doesn't work) instead of **http://localhost:8080/wtp.filecounter/FileCounter** and the second URL works – Joe Nov 17 '12 at 19:30
0

The name of your application is wtp.filecounter and you have a servlet with mapping "/FileCounder". That is why if you hit "http://localhost:8080/wtp.filecounter/FileCounter" in your browser, the doGet method of the servlet with mapping "/FileCounter" is invoked and you get the response.

Sazzadur Rahaman
  • 6,938
  • 1
  • 30
  • 52
0

'http://localhost:8080/wtp.filecounter/FileCounter' works because the servlet is found there.

The "wtp.filecounter" portion is your application context. If you want to access the servlet using 'http://localhost:8080/wtp.filecounter/' then you need to change your welcome file to point to the FileCounter servlet instead of using those default welcome files.

Joe
  • 7,749
  • 19
  • 60
  • 110
0

Just add FileCounter as one of 'welcome-file' in web.xml

0

The tutorial you are referring is using dynamic web module version 2.5 while you might be using 3.0. Take a close look at your web.xml and you'll see that no servlet mapping is created there. It might happen if you don't use eclipse's default servlet creation tool. Here's my web.xml looks like:

<?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>com.vogella.web.filecounter</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>FileCounter</servlet-name>
        <servlet-class>com.vogella.web.filecounter.servlet.FileCounter</servlet-class> 
    </servlet>

    <servlet-mapping>
        <servlet-name>FileCounter</servlet-name>
        <url-pattern>/FileCounter</url-pattern>
    </servlet-mapping>
</web-app>
Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98