Armazenando visitas em um arquivo txt

Olá pessoal, boa noite!

Preciso armazenar o contador de acessos a uma página em um arquivo txt. Caso alguém puder me ajudar a resolver isso ficaria muito grato.

Obrigado!

Segue abaixo o código do servlet:

package exercicio3;

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

public class exercicio3 extends HttpServlet {

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    //  response.setContentType("text/html;charset=UTF-8");

    response.setContentType("text/html");

    javax.servlet.http.HttpSession session = request.getSession();
    String heading;
    Integer accessCount = (Integer) session.getAttribute("accessCount");
    if (accessCount == null) {
        accessCount = new Integer(0);
        heading = "Welcome, Newcomer";
    } else {
        heading = "Welcome Back";
        accessCount = new Integer(accessCount.intValue() + 1);
    }

    session.setAttribute("accessCount", accessCount);

    PrintWriter out = response.getWriter();
    String docType = null;
    String title = null;
    try {

        out.println("<html>");
        out.println("<head>");
        out.println("<title>Servlet exercicio3</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>Servlet exercicio3 at " + request.getContextPath() + "</h1>");

        out.println(docType
                + "<HTML>\n"
                + "<HEAD><TITLE>" + title + "</TITLE></HEAD>\n"
                + "<BODY BGCOLOR=\"#FDF5E6\">\n"
                + "<CENTER>\n"
                + "<H1>" + heading + "</H1>\n"
                + "<H2>Information on Your Session:</H2>\n"
                + "<TABLE BORDER=1>\n"
                + "<TR BGCOLOR=\"#FFAD00\">\n"
                + " <TH>Info Type<TH>Value\n"
                + " <TD>Number of Previous Accesses\n"
                + " <TD>" + accessCount + "\n"
                + "</TABLE>\n"
                + "</CENTER></BODY></HTML>");


        out.println("</body>");
        out.println("</html>");

    } finally {
        out.close();
    }
}


@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    processRequest(request, response);
}


@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    processRequest(request, response);
}


@Override
public String getServletInfo() {
    return "Short description";
}// </editor-fold>

}

por favor, selecione o seu código e clique no botão CODE…

Não consigo entender nada do seu código cara…
Coloque seu código entre as tags CODE. Exemplo:

código aqui

[quote=fernandosavio]Não consigo entender nada do seu código cara…
Coloque seu código entre as tags CODE. Exemplo:

código aqui

[/quote]

Ok Amigo… veja se apareceu blz agora por favor…

[code]package exercicio3;

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

public class exercicio3 extends HttpServlet {

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// response.setContentType(“text/html;charset=UTF-8”);

response.setContentType(“text/html”);

javax.servlet.http.HttpSession session = request.getSession();
String heading;
Integer accessCount = (Integer) session.getAttribute(“accessCount”);
if (accessCount == null) {
accessCount = new Integer(0);
heading = “Welcome, Newcomer”;
} else {
heading = “Welcome Back”;
accessCount = new Integer(accessCount.intValue() + 1);
}

session.setAttribute(“accessCount”, accessCount);

PrintWriter out = response.getWriter();
String docType = null;
String title = null;
try {

out.println("");
out.println("");
out.println(“Servlet exercicio3”);
out.println("");
out.println("");
out.println("

Servlet exercicio3 at " + request.getContextPath() + “

”);

out.println(docType

  • “\n”
  • “” + title + “\n”
  • “<BODY BGCOLOR=”#FDF5E6">\n"
  • “\n”
  • ” + heading + “

    \n”
  • Information on Your Session:

    \n”
  • “\n”
  • “<TR BGCOLOR=”#FFAD00">\n"
  • "
  • Info Type Value\n"
  • "
  • Number of Previous Accesses\n"
  • "
  • " + accessCount + “\n”
  • \n”
  • “”);

out.println("");
out.println("");

} finally {
out.close();
}
}

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

@Override
public String getServletInfo() {
return “Short description”;
}//
} [/code]

Ok amigo! Veja se vai aparece blz agora…

[code]
package exercicio3;

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

public class exercicio3 extends HttpServlet {

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");


    javax.servlet.http.HttpSession session = request.getSession();
    String heading;
    Integer accessCount = (Integer) session.getAttribute("accessCount");
    if (accessCount == null) {
        accessCount = new Integer(0);
        heading = "Welcome, Newcomer";
    } else {
        heading = "Welcome Back";
        accessCount = new Integer(accessCount.intValue() + 1);
    }

    session.setAttribute("accessCount", accessCount);

    PrintWriter out = response.getWriter();
    String docType = null;
    String title = null;
    try {

        out.println("<html>");
        out.println("<head>");
        out.println("<title>Servlet exercicio3</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>Servlet exercicio3 at " + request.getContextPath() + "</h1>");

        out.println(docType
                + "<HTML>\n"
                + "<HEAD><TITLE>" + title + "</TITLE></HEAD>\n"
                + "<BODY BGCOLOR=\"#FDF5E6\">\n"
                + "<CENTER>\n"
                + "<H1>" + heading + "</H1>\n"
                + "<H2>Information on Your Session:</H2>\n"
                + "<TABLE BORDER=1>\n"
                + "<TR BGCOLOR=\"#FFAD00\">\n"
                + " <TH>Info Type<TH>Value\n"
                + " <TD>Number of Previous Accesses\n"
                + " <TD>" + accessCount + "\n"
                + "</TABLE>\n"
                + "</CENTER></BODY></HTML>");


        out.println("</body>");
        out.println("</html>");

    } finally {
        out.close();
    }
}

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    processRequest(request, response);
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    processRequest(request, response);
}

@Override
public String getServletInfo() {
    return "Short description";
}// </editor-fold>

}

Ok, vai aqui:

[code]package exercicio3;

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

public class exercicio3 extends HttpServlet {

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");


    javax.servlet.http.HttpSession session = request.getSession();
    String heading;
    Integer accessCount = (Integer) session.getAttribute("accessCount");
    if (accessCount == null) {
        accessCount = new Integer(0);
        heading = "Welcome, Newcomer";
    } else {
        heading = "Welcome Back";
        accessCount = new Integer(accessCount.intValue() + 1);
    }

    session.setAttribute("accessCount", accessCount);

    PrintWriter out = response.getWriter();
    String docType = null;
    String title = null;
    try {

        out.println("<html>");
        out.println("<head>");
        out.println("<title>Servlet exercicio3</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>Servlet exercicio3 at " + request.getContextPath() + "</h1>");

        out.println(docType
                + "<HTML>\n"
                + "<HEAD><TITLE>" + title + "</TITLE></HEAD>\n"
                + "<BODY BGCOLOR=\"#FDF5E6\">\n"
                + "<CENTER>\n"
                + "<H1>" + heading + "</H1>\n"
                + "<H2>Information on Your Session:</H2>\n"
                + "<TABLE BORDER=1>\n"
                + "<TR BGCOLOR=\"#FFAD00\">\n"
                + " <TH>Info Type<TH>Value\n"
                + " <TD>Number of Previous Accesses\n"
                + " <TD>" + accessCount + "\n"
                + "</TABLE>\n"
                + "</CENTER></BODY></HTML>");


        out.println("</body>");
        out.println("</html>");

    } finally {
        out.close();
    }
}

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    processRequest(request, response);
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    processRequest(request, response);
}

@Override
public String getServletInfo() {
    return "Short description";
}// </editor-fold>

}
[/code]

Está bem melhor…
Qual é o problema que está acontecendo?
Lança exception? Nao grava o arquivo? Nao grava o que voce quer no arquivo?

Boa noite Fernando!

Preciso fazer com que o contador de acessos fique salvo em um arquivo .txt, mas não tenho muita experiência em programação. Não sei como fazer ele gravar em um arquivo txt entende?
Será que pode me dar uma ajuda?

Ps: Estou fazendo o trabalho usando o IDE Netbeans 7.0 com o Servidor Apache Tomcat

Abraços.