Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Question about sessions

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Question about sessions

    Hi all. I have to make an HTML form, in which to enter data-first name of a student and his numerical ID.This information must be saved in the servlet's session.After entering the student's data, the user must be able to go back and enter another student, or see all names entered so far with their ID's. I am learning about JAVA servlets and sessions now, and will be very glad if you can help me with this, or at least give me some idea what to do


  2. #2
    Junior Member
    Join Date
    Oct 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Question about sessions

    That's the code of the form:

    <html>
    <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>
    <body>
    <form action="">
    <p align="center">
    Enter student name: <input name="name" type="text"/>
    Enter student ID: <input name="id" type="text"/><br>
    </p>
    <p align="center"> <input name="sb1" type="submit" value="Submit"/></p>



    </form>
    </body>
    </html>

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Question about sessions

    You need to store each user into a vector and then store the vector as a session variable.

    Take a look at java shopping carts for a better understanding


    ...
    public void doGet(HttpServletRequest req, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    HttpSession session = req.getSession(true);

    String username=(String)req.getParameter("username");
    if (username==null) { username="";}


    Vector allusers=new Vector();
    if (!username.equals("")) {
    allusers.addElement(username);

    allusers = (Vector) session.putValue("getAttribute("cart");
    session.setAttribute("AllUsers", allusers);
    }

    //retrieve
    allusers = (Vector) session.getAttribute("AllUsers");
    //Error check allusers if more than 0 {
    out.println("historic users:");
    for (int a=0; a < allusers.size(); a++) {
    String users=(String)allusers.elementAt(a);
    out.println(users);
    }

Similar Threads

  1. mantaining sessions in WebApp
    By arvindbis in forum Java Servlet
    Replies: 0
    Last Post: August 25th, 2011, 12:58 PM
  2. Replies: 1
    Last Post: May 20th, 2011, 03:58 AM