I want to send request from html page to servlet then create a list in java servlet then i want to return this list to the same html page .
Asked
Active
Viewed 1,146 times
-2
-
2**Don't put HTML in Java code. Don't put Java code in HTML.** – Sotirios Delimanolis Mar 25 '14 at 19:34
-
1Don't handle state in a Servlet, it will lead to thread safety problems. – Luiggi Mendoza Mar 25 '14 at 19:35
-
@SotiriosDelimanolis I wouldn't say you never have to put HTML code in your servlet. I would instead say to not handle a whole HTML page from servlet side. – Luiggi Mendoza Mar 25 '14 at 19:36
-
A "null error exception"? Never heard of one of those. Maybe you can give us the stack trace of the null error exception, and indicate which line numbers it's referring to? – Dawood ibn Kareem Mar 25 '14 at 19:36
-
@LuiggiMendoza Right, if you're going to write HTML, do it in a dedicated component. – Sotirios Delimanolis Mar 25 '14 at 19:37
-
@Sotirios This is probably a case of "our teacher told us to do it this way". – Dawood ibn Kareem Mar 25 '14 at 19:38
-
@DavidWallace I wish someone had told me this a few years ago. I'd like to think I would have looked into it and came back and told off (asked) my professor. – Sotirios Delimanolis Mar 25 '14 at 19:39
-
Often, the best way to learn not to do something is to experience the pain first hand. Possibly, the very next lesson that the teacher has in mind will be "this was painful because ..., but here's a better way of doing it." – Dawood ibn Kareem Mar 25 '14 at 19:40
-
@DavidWallace IMO these teachers also suffered from this bad design and they can only teach about what they know... – Luiggi Mendoza Mar 25 '14 at 19:52
-
@Luiggi you may well be right. But I've been a teacher myself and I prefer to give teachers the benefit of the doubt. I would hope that it's simply the case that setting up everything to get a servlet working properly was too much to cover in one lesson, and that the next lesson will cover the correct way to separate out the Java from the HTML. – Dawood ibn Kareem Mar 25 '14 at 19:56
-
@DavidWallace this is easily covered in stackoverflow servlet wiki and can be handled in a single lesson. I'm not against teachers because I also have suffered from learning this design at workplace rather than in classroom, but it's never too late to learn new things to teach to the students :). – Luiggi Mendoza Mar 25 '14 at 19:59
4 Answers
1
The problem is that you never initialize your HttpSession s
variable:
@WebServlet(urlPatterns = {"/ShowPersonServlet"})
public class ShowPersonServlet extends HttpServlet {
HttpSession s; //null by default
//...
protected void processRequest(...) {
//...
//since never initialized, s is null
user.add((Person) s.getAttribute("person"));
}
@Override
protected void doGet(...) {
//...
//since never initialized, s is null
s.setAttribute("person",person);
}
}
Make-it-work solution: Set the value of s
s = request.getSession();
Real world solution:
- Remove all the fields from the Servlet, NEVER try to handle state in your servlet unless they are resources managed by the container like EJBs.
- Change the scope of your
HttpSession s
to be local per method. Also, change its name froms
tosession
or something more useful. - Move your HTML code to a component that deals with view details like a JSP, then perform a forward to the view.
So, your code would look like this:
@WebServlet(urlPatterns = {"/ShowPersonServlet"})
public class ShowPersonServlet extends HttpServlet {
/*
HttpSession s ; //moved as local variable
Person person = new Person(); //moved as local variable
private List<Person> user = new ArrayList<Person>(); //not sure where you want to store this
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
HttpSession session = request.getSession();
List<Person> personList = (List<Person>) session.getAttribute("personList");
if (personList == null) {
personList = new ArrayList<>();
session.setAttribute("personList", personList);
}
personList.add((Person) session.getAttribute("person"));
/*
try (PrintWriter out = response.getWriter()) {
//removed to shorten this answer
}
*/
request.getRequestDispatcher("/showPerson.jsp").forward(request, response);
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Person person = new Person();
person.setKey(request.getParameter("txt_Key"));
person.setFirstName(request.getParameter("txt_firstName"));
person.setLastName(request.getParameter("txt_lastName"));
processRequest(request, response);
HttpSession session = request.getSession();
session.setAttribute("person",person);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
}
More info:

Community
- 1
- 1

Luiggi Mendoza
- 85,076
- 16
- 154
- 332
-
but how to return a list to the html page to print it there ? I should print in the same html page all users added. – user3459148 Mar 26 '14 at 11:39
-
I got an error here : if (personList == null) { **personList = new List<>();** session.setAttribute("personList", personList); List is abstract cannot be instantiated. – user3459148 Mar 26 '14 at 12:11
-
-
@user3459148 fixed the `new List<>` error to `new ArrayList<>`, my mistake (fast typing). – Luiggi Mendoza Mar 26 '14 at 14:22
0
You need to get the session from the request, you cannot simply expect to get it with HttpSession s ;
- There s
is null
.
// Something like this (in processRequest), although I
// would prefer a local session variable.
s = request.getSession();

Elliott Frisch
- 198,278
- 20
- 158
- 249
0
You need to provide stacktrace of your exception. But it looks like it could be in 2 places:
- s.setAttribute("person",person); where s is never set;
- out.println("" + p.getKey() + ""); where attribute "person" is never set;
Good luck.

Artem
- 7,275
- 15
- 57
- 97
0
The value of var s
is not defined. It is null so using s.getAttribute("str")
will throw an exception.
HttpSession s ;
s = request.getSession(false);

Arjit
- 3,290
- 1
- 17
- 18