#need help in java ee sessions
44 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @haughty island! Please use
/closeor theClose Postbutton 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.
Are you writing a servlet?
If so you can get the session from request.getSession()
Oh ok you got it
i want to save first name and last one
session.setAttribute("first_name", first_name);
here
Looks like you're already doing the first name
How about wrapping in a User object then adding a list of Users to the session
first name | last name
first name | last name
first name | last name
first name | last name
public class User {
private final String firstName;
private final String lastName;
public User(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
}
yes
Then manage a list of Users to the session
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?
Yeah I was about to say if list of users doesn't already exist in session you will get a nullpointer
So you have to check if it's null and if so set it
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?
No because it will still be null
If students is null you want to set it to a new list in the session
at least i wont get nullpointer
i mean
You will because students will always be null
not found
what should i do then?
can u edit the code
cant really understand what to do
i found this
list = Get list from session
If list is null:
list = Create list
Set session attribute to new list
list.add(whatever)
💤 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.