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
I have Designer 6i installed and have the message: 'This user does not have an installed Repositary. Invole the Repository Administrator Utility' when I try to start it up. I am trying to develope a database on a database on the network, not one that [More]
hello i need some help. i am tring to make a attendence register as an assignment. what i am tring to do is that i can add student name in a file and then retrive them from a file and then mark there presents or absents.the part i cant get done is ho [More]
Here's the story... when i import, from disks that i have previously burned, and i try to name the tracks that i want and it won't play them for me to preview them. then it freezes and i have to use task manager to restart iTunes. i'm importing some [More]
Alright, here's the Deal: I have a Powerbook 160 I just bought from a Yard sale, and one off of eBay. Yes, it's another couple of powerbooks! Just last summer I bought a 180 with a dead drive which I regret throwing out. It turns out that the one fro [More]
Ok, I got my new Droid X on September 9th and since I've had it the phone seems to reboot itself 3-4 times a day without reason.  I call verizon to tell them the problem and i get "its news to us"...Strange as I read several forums about many pe [More]
How do I sync my iCal across these devices?  I am on iCloud and I thought they would sync automatically.  Very frustrating!Hi mebjk,  Thanks for visiting Apple Support Communities.  It appears that iCloud is not syncing your iCloud calendar between y [More]
Hi! What is the diffrerence between Norrmal ABAP(SD, MM FICO) and ABAP HR? If somebody working in suppose SD Module then cant he work on ababp HR module. Regards...Chang..............Hi, SD MM Business flow  You'd better get more information at these [More]
Hi, I have a specific purchase order with wrong release strategy. I had been creating others documents with the same total value and the release strategy work correctly. The release strategy configuration are right. How I can correct the release stra [More]
Oracle 9i release 2 xmldb What is the recommended way to update the contents of a binary resource via JDBC. Pointers to documentation would be very helpful. The XML DB User's Guide doesn't say much about binary resources, except for using WebDAV and [More]
My Airport Express disconnects me randomly, without any reason, and it even disappears for some time from the available networks. I was wondering if anyone had a fix for that.I just wanted to echo the problem. I also have occasional disappearances of [More]