Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Local.java
- @Local
- public interface MovieBeansLocal {
- void addmovie(String moviename, Collection<Integer> tids);
- Collection<Movie> getallmovies();
- Movie getMovieById(Integer id);
- void deletemoviebyid(Integer id);
- Collection<Theater> getalltheaters();
- Collection<Theater> getalltheatersbymovie(Integer id);
- Collection<Movie> getallmoviesbytheater(Integer id);
- }
- //Beans.java
- @Stateless
- public class MovieBeans implements MovieBeansLocal {
- @PersistenceContext(unitName = "my_persistence_unit")
- EntityManager em;
- @Override
- public void addmovie(String moviename, Collection<Integer> tids) {
- Movie m = new Movie();
- m.setName(moviename);
- em.persist(m);
- Collection<Theater> t = new ArrayList<>();
- for (Integer q : tids) {
- Theater o = em.find(Theater.class, q);
- if (o != null) {
- Collection<Movie> s = o.getMovieCollection();
- s.add(m);
- t.add(o);
- m.setTheaterCollection(t);
- o.setMovieCollection(s);
- em.merge(o);
- }
- }
- }
- @Override
- public Collection<Movie> getallmovies() {
- return em.createNamedQuery("Movie.findAll").getResultList();
- }
- @Override
- public void deletemoviebyid(Integer id) {
- Movie m = em.find(Movie.class, id);
- Collection<Theater> theater = m.getTheaterCollection();
- for (Theater t : theater) {
- if (t != null) {
- Collection<Movie> d = t.getMovieCollection();
- d.remove(m);
- t.setMovieCollection(d);
- }
- }
- em.remove(m);
- }
- @Override
- public Collection<Theater> getalltheaters() {
- return em.createNamedQuery("Theater.findAll").getResultList();
- }
- @Override
- public Collection<Theater> getalltheatersbymovie(Integer id) {
- Movie m = em.find(Movie.class, id);
- return m.getTheaterCollection();
- }
- @Override
- public Collection<Movie> getallmoviesbytheater(Integer id) {
- Theater t = em.find(Theater.class, id);
- return t.getMovieCollection();
- }
- @Override
- public Movie getMovieById(Integer id) {
- return em.find(Movie.class, id);
- }
- }
- //List-Servlet.java
- @WebServlet(name = "MovieServlet", urlPatterns = {"/MovieServlet"})
- public class MovieServlet extends HttpServlet {
- @EJB
- MovieBeansLocal mbl;
- Collection<Movie> movies = new ArrayList<>();
- Collection<Theater> theaters = new ArrayList<>();
- protected void processRequest(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- response.setContentType("text/html;charset=UTF-8");
- try (PrintWriter out = response.getWriter()) {
- /* TODO output your page here. You may use following sample code. */
- out.println("<!DOCTYPE html>");
- out.println("<html>");
- out.println("<head>");
- out.println("<title>Servlet MovieServlet</title>");
- out.println("</head>");
- out.println("<body>");
- out.println("<h1>Servlet MovieServlet at " + request.getContextPath() + "</h1>");
- out.println("<a href='AddServlet'>Add Servlet</a>");
- out.println("<table border='1'><tr><td>Moviename</td><td>Theaters</td><td>City</td><td>State</td><td>location</td><td>TO Delete</td></tr>");
- movies = mbl.getallmovies();
- for (Movie k : movies) {
- out.println("<tr>");
- out.println("<td>" + k.getName() + "</td>");
- theaters = mbl.getalltheatersbymovie(k.getId());
- Theater temp = new Theater();
- out.println("<td><ui>");
- for (Theater t : theaters) {
- out.println("<li>" + t.getName() + "</li>");
- temp = t;
- }
- out.println("</ui></td>");
- out.println("<td>" + temp.getState() + "</td>");
- out.println("<td>" + temp.getLocation() + "</td>");
- out.println("<td>" + temp.getCity() + "</td>");
- out.println("<td><a href='EditMovieServlet?id=" + k.getId() + "'>Edit Movie</a></td>");
- out.println("<td><a href='MovieServlet?delid=" + k.getId() + "'>Delete Movie</a></td>");
- out.println("</tr>");
- }
- out.println("</table>");
- out.println("</body>");
- out.println("</html>");
- }
- }
- @Override
- protected void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- String del_id = request.getParameter("delid");
- if (del_id != null) {
- mbl.deletemoviebyid(Integer.parseInt(del_id));
- response.sendRedirect("MovieServlet");
- } else {
- processRequest(request, response);
- }
- }
- @Override
- protected void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- processRequest(request, response);
- }
- @Override
- public String getServletInfo() {
- return "Short description";
- }
- }
- //Add-Servlet.java
- @WebServlet(name = "AddServlet", urlPatterns = {"/AddServlet"})
- public class AddServlet extends HttpServlet {
- @EJB
- MovieBeansLocal mbl;
- protected void processRequest(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- response.setContentType("text/html;charset=UTF-8");
- try (PrintWriter out = response.getWriter()) {
- /* TODO output your page here. You may use following sample code. */
- out.println("<!DOCTYPE html>");
- out.println("<html>");
- out.println("<head>");
- out.println("<title>Servlet AddServlet</title>");
- out.println("</head>");
- out.println("<body>");
- out.println("<h1>Servlet AddServlet at " + request.getContextPath() + "</h1>");
- out.println("<form action=\"AddServlet\" method=\"POST\">\n"
- + "<input type=\"text\" name=\"name\" value=\"\" />\n"
- + "<input type=\"checkbox\" name=\"theater\" value=\"1\" checked=\"checked\" />Rupam\n"
- + "<input type=\"checkbox\" name=\"theater\" value=\"2\" />Rajhans\n"
- + "<input type=\"checkbox\" name=\"theater\" value=\"3\" />PVR\n"
- + "<input type=\"checkbox\" name=\"theater\" value=\"4\" />INOR\n"
- + "<input type=\"submit\" value=\"add\" />"
- + "</form>\n");
- out.println("</body>");
- out.println("</html>");
- }
- }
- @Override
- protected void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- processRequest(request, response);
- }
- @Override
- protected void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- String moviename = request.getParameter("name");
- if (moviename != null) {
- String[] s = request.getParameterValues("theater");
- Collection<Integer> c = new ArrayList<>();
- for (String p : s) {
- Integer a = Integer.parseInt(p);
- c.add(a);
- }
- mbl.addmovie(moviename, c);
- }
- response.sendRedirect("MovieServlet");
- }
- @Override
- public String getServletInfo() {
- return "Short description";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement