Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Unusual Problem  RSS feed

 
anand chawla
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following servlet has an unusal problem
when i run this and click on the button ut doesnt update or increment the counter.
but if i refresh the page it increments it
why does this happen
whats wrong with this simple servlet
pls help..I am using resin server 2.02..which I feel is very simple to use.
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Counter extends HttpServlet
{
static String Counter_key;
int count=1;
public void doGet(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException
{
HttpSession session=req.getSession(true);
res.setContentType("text/html");

PrintWriter out=res.getWriter();

Integer i=(Integer)session.getValue(Counter_key);

if(i!=null)
{
count=i.intValue()+1;
}
session.putValue(Counter_key,new Integer(count));
out.println("<html>");
out.println("<head>");
out.println("<title>Session Counter</title>");
out.println("<body>");
out.println("Your Sessoin id is <b>"+session.getId());
out.println("You Have hit the page <b>"+count+"</b>times during this session") ;
out.println("<form method=get action='"+req.getRequestURI()+"'>");

out.println("<input type=submit value='Hit Again'>");
out.println(req.getRequestURI());
out.println("</form>");
out.println("</body>");
out.println("</html>");
}
}
 
Kareem Gad
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Three things i spot when i first look at it :
1. your problem can be just caching. this resin server might be out doing itself in caching and not showing the new results( but let's put this one aside)
2. the second thing is that i can't see in your html form an input by the name of Counter_Key.
3. Integer i=(Integer)session.getValue(Counter_key);
why don't u just use req.getParameter() ?
much easier

------------------
KaReEm
 
anand chawla
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Pal,
U havent seen the program properly
Counter_key is declared as variable right at the begining of the program and i want to do this program without using getparameter
anyone out there
 
Patricia Fulk
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think what is happening is you are trying to get the value out of the session variable before putting the value in the session variable. When you click on the submit button then you try to get the value out of the session before putting it in there. Then when you click on refresh the value is in the session.
I hope this helps.
 
Kalaiselvan Selvamani
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hii,
I want to confirm that ur servelt is running fine.
Because u are not assigning any value for ur Variable
"static String Counter_key"
Regards
-Kalaiselvan.S
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!