Sessions
Hvordan sletter jeg data fra en session??jeg har lavet en indkøbskurv som jeg gemmer mine ting i. Men når jeg så vil slette dem hvordan henter jeg lige præcis det data frem jeg vil slette!!
Koden ser sådanne her ud:
import java.awt.*;
import java.awt.event.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class Butiksvogn extends HttpServlet
{
public void doPost(HttpServletRequest req, HttpServletResponse rep) throws
ServletException, IOException
{
HttpSession session = req.getSession(true);
//String res;
Integer antalVarer = (Integer) session.getAttribute(\"antalVarer\");
if(antalVarer == null)
{
antalVarer = new Integer(0);
}
PrintWriter out = rep.getWriter();
rep.setContentType(\"text/html\");
String[] temp = req.getParameterValues(\"slet\");
String VareValgt;
if(temp != null)
{
for(int i=0; i<temp.length;i++)
{
VareValgt= temp[i];
temp.remove(VareValgt);
System.out.println(VareValgt);
}
}
String[] itemsSelected;
String itemName;
itemsSelected = req.getParameterValues(\"VARER\");
System.out.println(\" antal \" + antalVarer );
System.out.println(\"OK ooooooooooooo\");
if(itemsSelected != null)
{
for(int i=0; i<itemsSelected.length;i++)
{
itemName = itemsSelected[i];
antalVarer = new Integer(antalVarer.intValue() + 1);
session.setAttribute(\"Vare\" + antalVarer, itemName);
session.setAttribute(\"antalVarer\", antalVarer);
System.out.println(antalVarer);
}
}
out.println(\"<html><head><title>Vogn</title><LINK href=\'C:/tomcat/webapps/lars/main.css\' rel=stylesheet></head><BODY bgcolor=\'#ffffff\'>\");
out.println(\"<FORM METHOD = \'POST\' NAME=\'udregn\' ACTION =\'Butiksvogn\'>\");
out.println(\"<br><br><CENTER><font size=5><b>Varer i din vogn: </b></font><br><br><br>\");
out.println(\"<TABLE width=\'60%\'><TBODY><TR><TH>Slet</TH><TH>Varetekst</TH><TH>Antal</TH> \"+
\"<TH>Pris / stk.</TH><TH>Beløb (DKK)</TH></TR>\");
int antal = 1;
int x = 0;
for(int i=1;i<=antalVarer.intValue();i++)
{
String item = (String) session.getAttribute (\"Vare\" + i);
int tal = item.indexOf(\';\');
int length = item.length();
String navn = item.substring(0, tal);
String pris0 = item.substring(tal +1, length);
int pris = Integer.parseInt(pris0);
out.println(\"<TR><TD><INPUT type=checkbox value=\"+ navn +\" name=slet></TD><TD>\" + navn + \"</TD>\"+
\"<TD align=right><INPUT size=4 value=\"+ antal +\" name=antal></TD><TD align=right>\" + pris + \",-</TD>\"+
\"<TD align=right>\" + pris * antal +\",-</TD></TR>\");
x = x + (pris * antal);
}
out.println(\"<TR><TD colSpan=4><STRONG>Beløb i alt</STRONG></TD><TD align=right>\"+ x +\",-</TD></TR></TBODY></TABLE></CENTER><br><br>\");
out.println(\"<center><INPUT type=SUBMIT value=Opdater></FORM><a href= target=_parent> Til kassen</a></TD><TD><a href=\'http://192.168.1.66:8080/lars/servlet/TomVogn\' target=_parent> Tøm vogn</a></center></BODY></HTML>\");
out.close();
}
public void doGet(HttpServletRequest req, HttpServletResponse rep) throws
ServletException, IOException
{
}
}