Sitemap

Amicuk Programming Answers

Session does not end even when i close browser

-0001-11-30   Views:0

Advertisement

when i close firefox/IE the session continues ,What i need is that i am adding amount in shopping cart ,but the amount adds to old amount even when i open a new browser window , i hv checked that my browser will remove cookies when i will close mozil

when i close firefox/IE the session continues ,What i need is that i am adding amount in shopping cart ,but the amount adds to old amount even when i open a new browser window ,
i hv checked that my browser will remove cookies when i will close mozilla
i hv also printed total=0 in servlet's ini method but the init method is called only once and total does not reset to 0,does 'th the call to init method is made everytime we start a new session via browser ,it does not get called whn i close the current browser and open new one.how to destroy the older servlet?
I m using s=request.getSession()
and s.setAttribute("billamnt",total)
the code of the servlet is
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class addtocart extends HttpServlet
     HttpSession s;
     PrintWriter pw;
     int total,cam,mob;
     public void init()
          total=0;
          cam=0;
          mob=0;
          System.out.println("inside  cart init");
public void service(HttpServletRequest req,HttpServletResponse res) throws IOException
     s=req.getSession();
    res.setContentType("text/html");
     pw=res.getWriter();
     pw.println("<html><body><a href="+"mobs"+">Mobile</a><br><a href="+"cams"+">Camera</a><form method = "+"post "+"action ="+"bill"+"><br><input type= "+"submit"+"></form></body></html>");
    if(req.getParameter("csony")!=null)
    cam=cam+Integer.parseInt(req.getParameter("csony"));
    if(req.getParameter("clg")!=null)
    cam=cam+Integer.parseInt(req.getParameter("clg"));
    if(req.getParameter("cnokia")!=null)
    mob=mob+Integer.parseInt(req.getParameter("cnokia"));
    if(req.getParameter("cerricson")!=null)
    mob=mob+Integer.parseInt(req.getParameter("cerricson"));
    s.setAttribute("cambill",new Integer(cam));
    s.setAttribute("mobbill",new Integer(mob));
    s.setAttribute("billamnt",new Integer(cam+mob));
public void doPost(HttpServletRequest req,HttpServletResponse res)
public void doGet(HttpServletRequest req,HttpServletResponse res)
}Wats the problem?
Message was edited by:
pooja_k_online

The replay answer
Advertisement
The servlet is not created on a session basis. All users share the same servlet object. The servlet is created once when the servlet is first called in the context, then is maintaned until the server shuts down.
You should not store any state in the servlet. You should use the HttpSession object you get from the request object to store the totals, and all other values. You should not use the servlet to hold on to a session object, as multiple users would see the SAME session that way. If you need to use the session in multiple methods, then you should pass that object as parameter, not by making it a class member.
You also shouldn't put the work in the service method of the HttpServlet. You should put it in either the doGet or doPost method (and if you want to make it happen for both type requests, call doPost from the doGet method and put the work in the doPost method).
For example:
public class ShoppingCartServlet extends HttpServlet {
  public void doGet(HttpServletRequest request, HttpServletResponse response) {
    doPost(request, response);
  public void doPost(HttpServletRequest request, HttpServletResponse response) {
    HttpSession session = request.getSession();
    Integer cambill = (Integer)session.getAttribute("cambill");
    int cam = 0;
    if(cambill != null) cam = cambill.intValue();
    Integer mobbill = (Integer)session.getAttribute("mobbill");
    int mob = 0;
    if(mobbill != null) mob = mobbill.intValue();
    Integer billamnt = (Integer)session.getAttribute("billamnt");
    int total = 0;
    if(billamnt != null) total = billamnt.intValue();
    String csony = request.getParameter("csony");
    if (csony != null) cam += Integer.parseInt(csony);
    String clg = request.getParameter("clg");
    if (clg != null) cam+= Integer.parseInt(clg);
    String cnokia = request.getParameter("cnokia");
    if (cnokia != null) mob += Integer.parseInt(cnokia);
    String cerricson = request.getAttribute("cerricson");
    if (cerricson != null) mob += Integer.parseInt(cerricson);
    total = cam + mob;
    session.setAttribute("cambill",new Integer(cam));
    session.setAttribute("mobbill",new Integer(mob));
    session.setAttribute("billamnt",new Integer(total));
    PrintWriter pw=response.getWriter();
    pw.println("<html><body><a href="+"mobs"+">Mobile</a><br><a href="+"cams"+">Camera</a><form method = "+"post "+"action ="+"bill"+"><br><input type= "+"submit"+"></form></body></html>");
    pw.close();
}Message was edited by:
stevejluke

Go to See the other 5 answers

I am loading Magic Jack. The error message I get is "Session cookies have been disabled for your web browser. Please enable session cookies so you can register your device." I have followed you process several times. This advice doesn't work. Pls help

Category:DefaultRelease time:-0001-11-30Views:130

Session cookies have been disabled for your web browser. Please enable session cookies so you can register your device. The URL is not specified.Session cookies have been disabled for your web browser. Please enable session cookies so you can registe[More]

How can i restore a Previous session if someone has just opened the Firefox browser and closed it bfore I got on the same computer?

Category:DefaultRelease time:2015-10-11Views:130

I had 2 lots of windows with multiple tabs thatI wanted to re-open the next timethe computer was on, but someone else was on the computer and opened firefox and now i can't restore my previous session of tabs in the 2 windows.hello, can you go to the[More]

I close Firefox session and then try to open a new browser window - get message that Firefox is still running and I need to shut down and restart. Need a solution

Category:DefaultRelease time:2015-10-11Views:130

I close a Firefox browser and at a later time want to open a new browser window but get message "Firefox is still running and I need to shut down before I can open a new session" I do not want to shut down as this is laborious - I can go into ta[More]

Session does not end even when i close browser

Category:DefaultRelease time:-0001-11-30Views:130

when i close firefox/IE the session continues ,What i need is that i am adding amount in shopping cart ,but the amount adds to old amount even when i open a new browser window , i hv checked that my browser will remove cookies when i will close mozil[More]

Session not closed on closing the IE 8 browser.

Category:DefaultRelease time:-0001-11-30Views:130

hi, We are facing this strange problem in IE 8 and 7 browser, but it works fine in  IE 6, When i close the IE window the session remains open, and then if i try to open the portal link it take me to the open session with out asking ID password. Our p[More]

I have my Firefox set to restore my previous session, but when I go to restart my browser, I am always at my home page, no tabs to be seen. How can I fix this?

Category:DefaultRelease time:-0001-11-30Views:130

I have no extra details, Its just something that has recently stopped happening to me since the last update to this version of FFMake sure that you do not use [[Clear Recent History]] to clear the "Browsing History" if Firefox is closed.Read oth[More]

Session timeout skillbuilders question

Category:DefaultRelease time:2015-10-11Views:130

Application security attributes settings Session Timeout  Maximum Session Length in Seconds   60 On session timeout direct to this URL   <url> Maximum Session Idle Time in Seconds   45 On session idle time timeout direct to this URL   <url> Se[More]

Firefox crashed before I could load my previous session - how may I restore it? I have backed up relevant session files, but have no idea how to use them.

Category:DefaultRelease time:2015-10-11Views:130

I was using Firefox shortly before deciding to restore my previous session (100+ tabs!), but the browser froze and went blank with the "Not Responding" label on top. This happened to me before, and I decided to just quit the session. I knew that[More]

Session problem in ADF BC

Category:DefaultRelease time:2015-10-11Views:130

We have an application developed in Jdev 10.1.3.4 (JSP, Struts, ADF BC) and running on OAS. Now we have a big problem with session, hope somebody can help with some ideas. We set session time to 45 min in the web.xml. The problem is that sometimes so[More]

Session Count not dropping in BOE XI

Category:DefaultRelease time:2015-10-11Views:130

Iam calling crystal reports using url reporting. ie. calling OpenDocument.aspx page from a custom asp webpage. Iam able to create a token and then redirect to the OpenDocument.aspx correctly. But iam facing 2 issues : 1. The session created does not[More]

New session created with every request

Category:DefaultRelease time:2015-10-11Views:130

Hi there, I have an web application that we used to deploy on tomcat, websphere and bea without any trouble. We are trying to move to support OAS and the application gives this peculiar behavior: A new session is created with every single request by[More]

Hot
Good day Guys, I'm having problems on starting an Essbase application. When I try to start the application, I get this error message: *Cannot load application [App Name] with error number [1052003] - see server log file.*+ I tried the solutions in th [More]
Hello, I got this issue where I'm logged into more then on computer at a time. If I have Firefox running on one computer I'm prevented from running Firefox on anther computer. This because my user account has its home directory mounted across all the [More]
We will have 1000s of site collections. Why doesn't SharePoint Online 2013 offer a way to grant a user or a group Site admin rights to all site collections? And.. if we must add the user to every single site, can this be done by an o365 or ADFS group [More]
Hi there, I have a wrt45G with dhcp running and working. Connecting the linkstation out of the box has no effect. Connecting the linkstation directy to the laptop works fine. I changed the config to a fixed IP outside of the DHCP range, but withing t [More]
I'm creating a pdf in Bridge and it becomes washed out once saved.  I've tried sRGB color space as well as Adobe 1998.  Both are washed out.  Does anybody have a solution to this issue? Listen, mbarnesphoto, You are not addressing Adobe here in this [More]
I have been using bridge CS3 (w/ PSE 6) on a mac for a year without a problem.  But all of a suddenly the find is not looking in subfolders when the checkbox look in all subfolders is checked.  I have over 20,000 items in the database without a find [More]
Just started creating Blu Ray DVDs from MXF OP1a AVC-I 100 Master files. I use AME to convert to BR matching the settings of the master. It converts successfully creating  video .m4v and PCM files. When I import them into Encore 6.02, the Project win [More]
Hi, I want to know if it possible (maybe in DRS advances options) to exclude a Host from the DRS. I got one cluster with 8 hosts. I want to let the DRS on the cluster for all the host except one. I don't want to create a rule to exclude VMs from the [More]
hai experts, can u plz give me step by step procedure to create subscreens in tabstrip control using  module pool,hi, You can check this program <b>  DEMO_DYNPRO_TABSTRIP_LOCAL </b> This will help you in understanding the concept and how to cr [More]
My first post, I can't find a solution for these problems, hopefully someone can help. I've been helping a friend with her Windows 8.1 PC and various issues she has had with it. The one area that seems to be a persistent issue is using Skype. The iss [More]