#need help in java ee sessions

44 messages · Page 1 of 1 (latest)

trim mesaBOT
#

This post has been reserved for your question.

Hey @haughty island! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

mortal bobcat
#

Are you writing a servlet?

#

If so you can get the session from request.getSession()

#

Oh ok you got it

haughty island
#

i want to save first name and last one

#

session.setAttribute("first_name", first_name);

#

here

mortal bobcat
#

Looks like you're already doing the first name

haughty island
#

but i want to put many first and last names

#

and then print them all together

mortal bobcat
#

How about wrapping in a User object then adding a list of Users to the session

haughty island
#

first name | last name
first name | last name
first name | last name
first name | last name

mortal bobcat
#
public class User {
    private final String firstName;
    private final String lastName;

    public User(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }
}
haughty island
#

yes

mortal bobcat
#

Then manage a list of Users to the session

haughty island
#

and then i will save them in list

#

ty

mortal bobcat
#

Yeah

#

No probs

#

Close post when done

haughty island
#
package servlets;

import dao.Student;
import dao.StudentDAO;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@WebServlet(name = "CreateServlet", value = "/create")
public class AddServlet extends HttpServlet {
    List<Student> students = null;
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String first_name = request.getParameter("first_name");
        String last_name = request.getParameter("last_name");
        if (first_name != null && last_name != null) {
            try {
                HttpSession session = request.getSession();
                students = (List<Student>) session.getAttribute("listStudent");
                Student s = new Student(first_name,last_name);
                students.add(s);
                session.setAttribute("listStudent", students);
            } catch (Exception e) {
                response.sendError(HttpServletResponse.SC_BAD_REQUEST);
            }

        }

        response.sendRedirect(request.getContextPath() + "/show");
    }
}
#

@mortal bobcat like this right?

#

and also when the user enters the website for the first type listStudent won't be created

#

so where do i create it?

mortal bobcat
#

So you have to check if it's null and if so set it

haughty island
#

HttpSession session = request.getSession();
students = (List<Student>) session.getAttribute("listStudent");
if (students == null) {
session.setAttribute("listStudent", students);
}
Student s = new Student(first_name,last_name);
students.add(s);
session.setAttribute("listStudent", students);

#

@mortal bobcat like this?

mortal bobcat
#

No because it will still be null

#

If students is null you want to set it to a new list in the session

haughty island
#

i mean

mortal bobcat
#

You will because students will always be null

haughty island
#

not found

haughty island
haughty island
#

cant really understand what to do

#

i found this

mortal bobcat
haughty island
#

ok

#

ty

trim mesaBOT
#

💤 Post marked as dormant

This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.