0 Replies Latest reply: Mar 20, 2009 12:56 PM by 843807 RSS

    Servlet + Java beans

    843807
      Im working on a school asignment for which im building a servlet which must redirect to a jsp if a register is found in a databse and to another one if its not found. I havnt done the jsp's yet, btu i worked on the servlet and on the bean needed for this. the bean is done and is the next:
      package coreservlets; 
       
      import java.util.*; 
      
      /* Bean to represent an Author */
       
      public class Author { 
      
      private int AuthorID;
      private String FirstName, LastName, YearBorn; 
      
      
      public void setAuthorID(int AuthorID) {this.AuthorID=AuthorID;}
      
      public void setFirstName(String FirstName) { this.FirstName=FirstName;} 
      
      public void setLastName(String LastName){this.LastName=LastName;} 
      
      public void setYearBorn (String YearBorn){this.YearBorn=YearBorn;}
      
      
      public int getId() { return(AuthorID);} 
      
      public String getFirstName() { return(FirstName);} 
      
      public String getLastName() { return(LastName);} 
      
      public String getYearBorn() { return(YearBorn);} 
      
      
      
      
      }
      The directory for this file is: /Library/Tomcat/webapps/Lab8/WEB-INF/classes/coreservlets

      plus, my servlet is the next:
      import java.io.*;
      import javax.servlet.*;
      import javax.servlet.http.*;
      import java.sql.*;
      import coreservlets.*;
      
      public class servlet1 extends HttpServlet {
      
        public void doGet(HttpServletRequest req, HttpServletResponse res)
                                     throws ServletException, IOException {
      
          res.setContentType("text/html");
          PrintWriter out = res.getWriter();
      
      
          out.println("<HTML>");
          out.println("<BODY>");
          out.println("<form name=\"forma1\" method=\"POST\" action=\"servlet1\">");
          out.println("<h1> Buscador de autores Prentice Hall </h1> <br />");
          out.println("Author id: <input type=\"text\" name=\"id\" id=\"id\" /> <br>");
          out.println("<input type=\"submit\" id=\"button\" value=\"Buscar\" >");
          out.println("</form></BODY></HTML>");
        }
      
         public void doPost(HttpServletRequest req, HttpServletResponse res)
                                     throws ServletException, IOException {
               System.out.println("jojo");                           
          res.setContentType("text/html");
          PrintWriter out = res.getWriter();
          Connection connection=null;
      
           //llamar al bean 1
           
           int idDelAutor = Integer.parseInt(req.getParameter("id"));
           
           //conexion a la base de datos
           try { System.out.println("jojo1");
                Class.forName( "com.mysql.jdbc.Driver" ); 
                connection = DriverManager.getConnection("jdbc:mysql://localhost/Books","root", "");     
                System.out.println("jojo2"); }
                
      
           catch (Exception cnfex ) { System.out.println("jojo3");
                System.err.println("Failed to load JDBC/ODBC driver."); 
                cnfex.printStackTrace(); 
                System.exit( 1 );  // terminate program 
           } 
      
          Statement statement; 
           ResultSet resultSet; 
           
           try { 
                String query = "SELECT * FROM Authors WHERE AuthorID = " + idDelAutor; 
                statement = connection.createStatement(); 
                resultSet = statement.executeQuery( query );  
                
                String adress;
                Author author = new Author();
                
                
                
                
                if(resultSet.next()){
                     
                     adress="/WEB-INF/jsps/Existente.jsp";
                     request.setAtribute("Author", author);
                }
                else{
                     adress="/WEB-INF/jsps/NoExistente.jsp";
      
                }
                     
                               
                
                
                statement.close(); 
           }
            
           catch ( SQLException sqlex ) { 
                sqlex.printStackTrace(); 
           } 
          
          RequestDispatcher dispatcher=req.getRequestDispatcher(address);
          dispatcher.forward(req, res);
          
        }
      }
      this file is located at /Library/Tomcat/webapps/Lab8/WEB-INF/classes

      but when i try to compile this last file, i get an error which is the next:
      marcos$ javac servlet1.java 
      servlet1.java:58: cannot access Author
      bad class file: ./Author.java
      file does not contain class Author
      Please remove or make sure it appears in the correct subdirectory of the classpath.
                Author author = new Author();
                      ^
      1 error
      Iv been trying everything for hours, and i can not fix it. Can somebody help me please?