user1701467
user1701467

Reputation: 285

How to make two Servlets share the same list of items

It's a simple CRUD web-app.

I have two Servlets.

One for listing(MyProjectListServlet.java) all the items on a page.

The other handles the updating(MyPprojectEditorServlet.java) of item information or adding an item to the list.

At the moment, both Servlets create and maintain their own list of same items. So, the list Servlet always shows its own list of unaltered items and update Servlet shows the updated item information(if you edit an item, otherwise it will show same information as the list servlet).

How do I make both Servlets(MyProjectListServlet and MyPprojectEditorServlet) to share the same one itemRepo list.

MyProjectListServlet.java:

package com.myproject;

import java.io.IOException;

import javax.inject.Inject;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.myproject.MyProjectRepository;

@WebServlet("/project/")
public class MyProjectListServlet extends HttpServlet {

    @Inject
    private MyProjectRepositoryImpl itemsRepo = new MyProjectRepositoryImpl(); // creates brand new list of items

    public MyProjectListServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        request.setAttribute("items", itemsRepo.listItems());

        getServletContext().getRequestDispatcher("/WEB-INF/pages/item-list.jsp").forward(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

}

MyPprojectEditorServlet.java:

package com.myproject;

import java.io.IOException;

import javax.inject.Inject;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.myproject.myitem;
import com.myproject.myprojectRepository;

@SuppressWarnings("serial")
@WebServlet("/project")
public class MyPprojectEditorServlet extends HttpServlet {

    @Inject
    private MyProjectRepositoryImpl itemRepo = new MyProjectRepositoryImpl(); //creates its own list of items

    public MyProjectEditorServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        String id = request.getParameter("id");

        if(id != null && !id.isEmpty()){
            MyItem item = itemRepo.lookupitemById(id);
            request.setAttribute("item", item);

        }

        request.setAttribute("items", itemRepo.listMyItems());

        getServletContext().getRequestDispatcher("/WEB-INF/pages/item-form.jsp").forward(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub

        String str1 = request.getParameter("str1");
        String str2 = request.getParameter("str2");
        String str3 = request.getParameter("str3");
        String str4 = request.getParameter("str4");
        String str5 = request.getParameter("str5");

        String id = request.getParameter("id");
        if(id == null || id.isEmpty()){
            itemRepo.addItem(str1, str2, str3, str4, str5);
        }else{
            itemRepo.updateItem(id, str1, str2, str3, str4, str5);
        }
        response.sendRedirect(request.getContextPath() + "/project/");
    }

}

Upvotes: 3

Views: 881

Answers (3)

Jordan Denison
Jordan Denison

Reputation: 2727

You can either use a singleton/Application or Session scoped variable, or for a more scalable solution, you can store the data in something like JPA/Redis.

Upvotes: 0

Lorenz
Lorenz

Reputation: 1303

The JPA (Java Persistence API, http://en.wikipedia.org/wiki/Java_Persistence_API) allows you to store your runtime objects in a database, making them accessible even if you restart your container or reload your servlet. To use the JPA, your classes need some annotations to define how an object should be stored in the database.

Once an object is saved, it can easily be accessed from other servlets again and modifications are also updated in the underlying database.

Upvotes: 0

Eng.Fouad
Eng.Fouad

Reputation: 117597

You have two options:

  1. Save the list as an attribute of the HttpSession object (session scope).
  2. Save the list as an attribute of the ServletContext object (application scope).

Upvotes: 2

Related Questions