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
Hi, please tell me how to create PF status in ALV. I know how to create PF status but not sure in ALV. can some one give simple report with code please. ex: spfli table. I want to use 'REUSE_ALV_LIST_DISPLAY' instead of grid. Please help. Thanks in a [More]
Hi Friends, There is one ABAP Mapping used in our system. The outbound interface is based on one message in external definition and inbound interface is IDoc. When I try to test this mapping in our PI System using the transaction "SXI_MAPPING_TEST&qu [More]
Having a problem attempting to install the forte community edition on my Dell Windows XP laptop. I have the runtime installed - 1.4.0, no problems with that. When I attempt ide, the installshield comes up, messes around for a few seconds, then exits. [More]
is apple tv coming chinese subtitbles  when i m watching any movies?Welcome to the Apple Community. Intermittent problems are often a result of interference. Interference can be caused by other networks in the neighbourhood or from household electric [More]
Hello, I'm trying to create an interactive newsletter to be sent to clients by email, and I was wondering how to get a video file to load upon opening. Currently you have to download the pdf to view the video. I'm working on OSX 10.9.5 in InDesign CS [More]
Hi Gurus, I would like to know what is the use of return table in bw.. can any one give me the perfect answer.... RameshHii Imagine a situation where 1 record coming into a cube needs to be split into multiple records. Lets say you get values total v [More]
Where can I get in touch with developers and/or beta testers of the MMS update that is coming out? I have some feedback and issues to share with them that needs to be addressed before the MMS rolls out, and would like to share with them so they can h [More]
Dear All, In standard Transaction COR2,when i release my process order at that time I got Message. So how can i change this message? And also i want to check that which type of message exist in SAP. Is there any T code frm where i got all msg list?De [More]
Exporting a Numbers file to Excel, that has 1 Freeze Column and 5 Freeze Headers, somehow adds an additional Row & Column when opened in Excel. Thoughts?I found the answer to this question with help from SAP Support. In the Cross-Tab Expert, Customiz [More]
I have a ton of effects on a particular loop; it'd be nice to mix it all down so the source loop has all the processing already on it. That way i can free up resources and use the post-processed loop in the future. any ideas??Not sure if you understo [More]