hola, mira te hago dos servlets uno control y otro presentacion, que van sumando números cada vez que los llamas.
control
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.PrintWriter;
import java.io.IOException;
public class ServletExamen extends HttpServlet
{
private static final String CONTENT_TYPE = "text/html; charset=windows-1252";
public void init(ServletConfig config) throws ServletException
{
super.init(config);
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
doGetPost(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
doGetPost(request,response);
}
public void doGetPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
String question;
HttpSession sesion = request.getSession();
question = request.getParameter("question");
if (question==null) DoNoQuestion(request,response);
else if (question.equals("contador")) DoQuestionContador(request,response);
else DoError(request,response);
}
public void DoNoQuestion (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
request.getRequestDispatcher("servletpresentacion").forward(request,response);
}
public void DoQuestionContador (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
HttpSession sesion = request.getSession();
Integer contador;
if (sesion.isNew())
{
contador=new Integer(1);
sesion.setAttribute("valor",contador);
}
else
{
contador = (Integer)sesion.getAttribute("valor");
if (contador==null) contador = new Integer(1);
else contador = new Integer(contador.intValue() + 1);
sesion.setAttribute("valor",contador);
}
request.getRequestDispatcher("servletpresentacion").forward(request,response);
}
public void DoError (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
PrintWriter out = response.getWriter();
StringBuffer dir = new StringBuffer();
out.println("<html>");
out.println("<head><title>Error</title></head>");
out.println("<body>");
out.println("<p><h1>Error</h1></p>");
out.println("</body></html>");
out.close();
}
presentacion
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.PrintWriter;
import java.io.IOException;
public class ServletPresentacion extends HttpServlet
{
private static final String CONTENT_TYPE = "text/html; charset=windows-1252";
public void init(ServletConfig config) throws ServletException
{
super.init(config);
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
doGetPost(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
doGetPost(request,response);
}
public void doGetPost (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
String question;
HttpSession sesion = request.getSession();
question = request.getParameter("question");
if (question==null) DoNoQuestion(request,response);
else if (question.equals("contador")) DoQuestionContador(request,response);
else DoError(request,response);
}
public void DoNoQuestion (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
PrintWriter out = response.getWriter();
StringBuffer dir = new StringBuffer();
out.println("<html>");
out.println("<head><title>Servlet Presentacion</title></head>");
out.println("<body>");
out.println("<p><h1>Servlet Presentacion</h1></p>");
dir = dir.append(request.getRequestURI().substring(0,request.getRequestURI().lastIndexOf("/"))+"/servletexamen");
out.println("<p><a href='" + dir + "?question=contador" + "'>Entrar</a></p>");
out.println("</body></html>");
out.close();
}
public void DoQuestionContador (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
PrintWriter out = response.getWriter();
StringBuffer dir = new StringBuffer();
Integer contador;
HttpSession sesion = request.getSession();
if (sesion.isNew()) DoError(request, response);
else
{
contador = (Integer) sesion.getAttribute("valor");
out.println("<html>");
out.println("<head><title>Servlet Presentacion</title></head>");
out.println("<body>");
out.println("<p><h1>Servlet Presentacion</h1></p>");
out.println("<p>Valor: " + contador.intValue() + "</p>");
dir = dir.append(request.getRequestURI().substring(0,request.getRequestURI().lastIndexOf("/"))+"/servletexamen");
out.println("<p><a href='" + dir + "?question=contador" + "'>Seguir</a></p>");
out.println("</body></html>");
out.close();
}
}
public void DoError (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
PrintWriter out = response.getWriter();
StringBuffer dir = new StringBuffer();
out.println("<html>");
out.println("<head><title>Error</title></head>");
out.println("<body>");
out.println("<p><h1>Error</h1></p>");
out.println("</body></html>");
out.close();
}
}
espero haberte ayudado.
Un saludo