This discussion is archived
4 Replies Latest reply: Jul 26, 2012 4:18 PM by DrClap RSS

Interface compiled but calling class does not see it why?

hh*394375*91 Newbie
Currently Being Moderated
The following interface compiled clean

public interface SharedConstants {

     static final String CURRENT_SITE = "http://localhost/emotherearth";
     static final String DB_URL = "jdbc:mysql://localhost";
static final String DB_DRIVER = "com.mysql.jdbc.Driver";
}


The following class did not

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;


public class Welcome extends HttpServlet implements SharedConstants {
     
     static final private String CONTENT_TYPE = "text/html";
     
     public void init(ServletConfig config) throws ServletException {
          super.init(config);
     }

     public void doGet(HttpServletRequest request,HttpServletResponse response) throws
ServletException, IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
out.println("<html>");
     out.println("<head><title>Welcome to eMotherEarth.cmo</title></head>");
     out.println("<body>");
     out.println("<h1>Welcome to eMotherEarth.com</h1>");
     out.println("<p><h3>Your 1-stop shop for Earth Products</h3>");
     out.println("Please enter your login name:<p>");
*     out.println("<form action=\"" + CURRENT_SITE + "/Login\" method=\"post\"><p>");*
     out.println("<p>Name:<input type=\"text\" name=\"username\"><p>");
     out.println("<input type=\"submit\" name=\"Submit\" value=\"Login\">");
     out.println("</form>");
          out.println("</body></html>");
     }
     
     public void destroy(){
     }
}


The bold line out.println("<form action=\"" CURRENT_SITE + "/Login\" method=\"post\"><p>");+


+gives the following error when compiling+

C:\myClass\servcart\src>javac Welcome.java
Welcome.java:7: error: cannot find symbol
public class Welcome extends HttpServlet implements SharedConstants {
^
symbol: class SharedConstants
Welcome.java:25: error: cannot find symbol
out.println("<form action=\"" + CURRENT_SITE + "/Login\" method=\"post\"
<p>");
^
symbol: variable CURRENT_SITE
location: class Welcome
2 errors


Why does'nt it see the CURRENT_SITE.

Howard
  • 1. Re: Interface compiled but calling class does not see it why?
    DrClap Expert
    Currently Being Moderated
    Here's the relevant error message:

    Welcome.java:7: error: cannot find symbol
    public class Welcome extends HttpServlet implements SharedConstants {
    ^
    symbol: class SharedConstants

    And would you mind using the
     tags around your code in future? If you have looked at your post after you made it, you surely must agree that it's an unreadable mess.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
  • 2. Re: Interface compiled but calling class does not see it why?
    DrClap Expert
    Currently Being Moderated
    And by the way could I point out a few of the more egregious bad practices that I can see there?

    First of all implementing an interface just so that you can use constants without specifying a class name for them is an anti-pattern. If you don't like spending the time to type the class name, at least use the static import feature, but don't misuse an interface this way.

    And second, hard-coding "localhost" into the URLs which you generate is a decision you're going to regret. You shouldn't be generating absolute URLs in the first place, but generating them based on hard-coded values is a really bad idea.
  • 3. Re: Interface compiled but calling class does not see it why?
    hh*394375*91 Newbie
    Currently Being Moderated
    I see serveral examples including java's and it is the same. I do not understand what is wrong with it. Since SharedConstants compile it should see it correct?
      
    
    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.util.*;
    
    
    public class Welcome extends HttpServlet implements SharedConstants {
         
         static final private String CONTENT_TYPE = "text/html";
         
         public void init(ServletConfig config) throws ServletException {
              super.init(config);
         }
    
         public void doGet(HttpServletRequest request,HttpServletResponse response) throws
                 ServletException, IOException           {
            response.setContentType(CONTENT_TYPE);
            PrintWriter out = response.getWriter();
            out.println("<html>");
                out.println("<head><title>Welcome to eMotherEarth.cmo</title></head>");
         out.println("<body>");
         out.println("<h1>Welcome to eMotherEarth.com</h1>");
         out.println("<p><h3>Your 1-stop shop for Earth Products</h3>");
         out.println("Please enter your login name:<p>");
         out.println("<form action=http://localhost/emotherearth/Login\" method=\"post\"><p>");
         out.println("<p>Name:<input type=\"text\" name=\"username\"><p>");
         out.println("<input type=\"submit\" name=\"Submit\" value=\"Login\">");
         out.println("</form>"); 
              out.println("</body></html>");
         }
         
         public void destroy(){
         }
    }
    
    
    {code}
    
    {code}
    
    public interface SharedConstants {
    
         static final String CURRENT_SITE = "http://localhost/emotherearth";
         static final String DB_URL = "jdbc:mysql://localhost";
             static final String DB_DRIVER = "com.mysql.jdbc.Driver";
    }
    
    {code}                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
  • 4. Re: Interface compiled but calling class does not see it why?
    DrClap Expert
    Currently Being Moderated
    That means the interface (compiled version) isn't in your classpath when you compile your servlet.

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points