6
<%@ page import ="java.util.*" %>
<%@ page import ="book.*" %>


ArrayList movies = (ArrayList) request.getAttribute("movieinfo");

    if(movies!=null){
        for(int i=0;i<movies.size();i++){
                    movie b = (movie) movies.get(i);
            out.println("<tr>");
            out.println("<td>" + b.getMovieID() + "</td>");
            out.println("<td>" + b.getMovieTitle() + "</td>");
            out.println("<td>" + b.getReleaseDate() + "</td>");
            out.println("<td>" + b.getDescription() + "</td>");
            out.println("<td>" + b.getImage() + "</td>");

             }
        }

Hi i am trying to loop through the arraylist of movies. However i am getting an error:

movie is a java value bean

org.apache.jasper.JasperException: Unable to compile class for JSP: 

An error occurred at line: 80 in the jsp file: /index.jsp
movie cannot be resolved to a type
77:     
78:     if(movies!=null){
79:         for(int i=0;i<movies.size();i++){
80:                     movie b = (movie) movies.get(i);
81:             out.println("<tr>");
82:             out.println("<td>" + b.getMovieID() + "</td>");
83:             out.println("<td>" + b.getMovieTitle() + "</td>");

Thanks for the help. Please let me know if i should put of additional information from other beans e.g. the utility bean

src/movie/movie.java

package movie;

import java.util.Date;

public class movie {

    private int MovieID;
    private String MovieTitle;
    private String Description;
    private String Image;
    private Date ReleaseDate;

    public int getMovieID() {
        return MovieID;
    }
    public void setMovieID(int movieID) {
        MovieID = movieID;
    }
    public String getMovieTitle() {
        return MovieTitle;
    }
    public void setMovieTitle(String movieTitle) {
        MovieTitle = movieTitle;
    }
    public String getDescription() {
        return Description;
    }
    public void setDescription(String description) {
        Description = description;
    }
    public String getImage() {
        return Image;
    }
    public void setImage(String image) {
        Image = image;
    }
    public Date getReleaseDate() {
        return ReleaseDate;
    }
    public void setReleaseDate(Date releaseDate) {
        ReleaseDate = releaseDate;
    }

}

src/movie/MovieDB.java

    package movie;
    import java.sql.*;
    import java.util.ArrayList;

public class MovieDB {
    public ArrayList movies (String query){
        ArrayList movies= new ArrayList();

        try {
            // step 1 : load JDBC Driver
            Class.forName("com.mysql.jdbc.Driver");

            //step2 : define Connection URL
            String connURL = "jdbc:mysql://localhost:3306/sp_movie?user=root&password=root";

            //step3 establish connection url
            Connection conn = DriverManager.getConnection(connURL);

            String sql="{call "+query+"}";
            CallableStatement cs=conn.prepareCall(sql);

            ResultSet rs=cs.executeQuery();

            while(rs.next()){
                movie movies1 = new movie();
                movies1.setMovieID(rs.getInt("Movie_ID"));
                movies1.setMovieTitle(rs.getString("Movie_Title"));
                movies1.setReleaseDate(rs.getDate("Release_Date"));
                movies1.setImage(rs.getString("Image"));
                movies1.setDescription(rs.getString("Description"));

                movies.add(movies1);
            }
            } catch (Exception e){
            } finally {
                try {

                } catch (Exception e) {}
            }
            return movies;
        }
}

src/movie/Moviesearch.java

package movie;

import java.io.IOException;
import java.util.ArrayList;

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

/**
 * Servlet implementation class Moviesearch
 */
@WebServlet("/Moviesearch")
public class Moviesearch extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public Moviesearch() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String search = (String) request.getAttribute("search");
        String searchtype = (String) request.getAttribute("searchtype");
        String query = null;

        if (searchtype.equals("title")){
            query = "titlesearch('"+search+"')";
        }else if(searchtype.equals("genre")){
            query = "genresearch('"+search+"')";
        }else if(searchtype.equals("actor")){
            query = "actorsearch('"+search+"')";
        }

        ArrayList movies = null;
        try{
            String userid = request.getParameter("userid");
            MovieDB getinfo = new MovieDB();
            movies =  getinfo.movies(query);

            request.setAttribute("movieinfo",movies);
            RequestDispatcher rd = request.getRequestDispatcher("index.jsp");
            rd.forward(request, response);
        } catch (Exception e){

        } finally {
        }

    }

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

}
KAKAK
  • 879
  • 7
  • 16
  • 32
  • Is the movie class in the book package you imported? – ChadNC Jul 18 '13 at 18:13
  • @ChadNC i have only imported `<%@ page import ="book.*" %>` how should i import the book (value bean) – KAKAK Jul 18 '13 at 18:15
  • You need a type declaration there..something like Movie movie = (Movie) movies.get(i). The better thing to do is use Generics and define a type to the ArrayList (like ArrayList()) or ...the best thing to do is to use EL and forget scriplets. – Mahesh Guruswamy Jul 18 '13 at 18:15
  • @MaheshGuruswamy i think my code is doing the same thing `movie b = (movie) movies.get(i);`? movie is a value bean holind the array of tables of information – KAKAK Jul 18 '13 at 18:17
  • If your Movie class is in the "book" package then you should have access to it in the jsp page and if it's not in the "book" package then you need to import it also. example <%@ page import="yourpackage.Movie" %>. Java is case sensitive so if the name of the class is "Movie" then the compiler will not find the class if you type the class names as "movie". – ChadNC Jul 18 '13 at 18:20
  • @DeepakTiwari Show us the `movie` class definition , at least the first few lines including package definition . – AllTooSir Jul 18 '13 at 18:23
  • if you familiar with Servlet code, You can check the generated Servlet file to see, import is correct or not. it will definitely not thatsy you are getting error but sometime easy to track in generated Servlet code. – Jayesh Jul 18 '13 at 18:24
  • @DeepakTiwari Your code should be inside scriptlets `<% %>` ! – AllTooSir Jul 18 '13 at 18:26
  • @TheNewIdiot it is inside just that i did not show the full code ..i have also updated the post to include the classes and the bean thanks – KAKAK Jul 18 '13 at 18:32
  • This is not a JSP problem. This is just a basic Java problem. You'd have had exactly the same problem when putting that code in a normal Java class with a `main()` method (which by the way also allows so much easier unit-testing). Eclipse would have errored the same way and suggested to `import` the offending class. – BalusC Jul 18 '13 at 18:32
  • As an additional advice, [avoid Java code in JSP files](http://stackoverflow.com/questions/3177733/how-to-avoid-java-code-in-jsp-files/3180202#3180202)! Just use JSTL ``. – BalusC Jul 18 '13 at 18:35

1 Answers1

8

Look at your movie class definition . It is packaged inside movie package.

package movie; // full class name will be movie.movie
import java.util.Date;
public class movie { ......
}

You should import the movie class as :

<%@ page import ="movie.movie" %>

Even then writing Java code inside JSP is highly discouraged . Read How to avoid Java Code in JSP-Files?.

For your purpose you can use JSTL's <c:forEach> loop.

Community
  • 1
  • 1
AllTooSir
  • 48,828
  • 16
  • 130
  • 164
  • Thank you for your help. i have tried this, seems i got some serious error with my codes.. – KAKAK Jul 18 '13 at 18:38