Solved

can you tell me why this class doesn't get compiled

Posted on 2009-04-07
7
448 Views
Last Modified: 2013-12-02
hi all,
I'm using tomcat 6 on linux centos 5
I got this .java file for a servlet  it's code is attached
here is the compilation error message please help what is the problem and how to fix it

[root@localhost classes]# javac -classpath /opt/tomcat6/lib/servlet-api.jar -d ./ ./firstservlet.java
----------
1. WARNING in ./firstservlet.java (at line 5)
        public class firstservlet extends HttpServlet
                     ^^^^^^^^^^^^
The serializable class firstservlet does not declare a static final serialVersionUID field of type long
----------
2. ERROR in ./firstservlet.java (at line 9)
        HttpServletResponse response) throws ServletException,
                                             ^^^^^^^^^^^^^^^^
ServletException cannot be resolved to a type
----------
3. ERROR in ./firstservlet.java (at line 49)
        HttpServletResponse response) throws ServletException,
                                             ^^^^^^^^^^^^^^^^
ServletException cannot be resolved to a type
----------
4. ERROR in ./firstservlet.java (at line 53)
        Enumeration paramNames = request.getParameterNames( );
        ^^^^^^^^^^^
Enumeration cannot be resolved to a type
----------
4 problems (3 errors, 1 warning)[root@localhost classes]#





import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
 public class firstservlet extends HttpServlet 
{
    
  public void doGet(HttpServletRequest request, 
    HttpServletResponse response) throws ServletException,
      java.io.IOException 
  {
    
    //set the MIME type of the response, "text/html"
    response.setContentType("text/html");
    
    //use a PrintWriter to send text data to the client who has requested the
    //servlet
    java.io.PrintWriter out = response.getWriter( );
 
    //Begin assembling the HTML content
    out.println("<html><head>");
    
    out.println("<title>Help Page</title></head><body>");
    out.println("<h2>Please submit your information</h2>");
   
   //make sure method="post" so that the servlet service method
   //calls doPost in the response to this form submit
    out.println(
        "<form method=\"post\" action =\"" + request.getContextPath( ) +
            "/firstservlet\" >");
 
    out.println("<table border=\"0\"><tr><td valign=\"top\">");
    out.println("Your first name: </td>  <td valign=\"top\">");
    out.println("<input type=\"text\" name=\"firstname\" size=\"20\">");
    out.println("</td></tr><tr><td valign=\"top\">");
    out.println("Your last name: </td>  <td valign=\"top\">");
    out.println("<input type=\"text\" name=\"lastname\" size=\"20\">");
    out.println("</td></tr><tr><td valign=\"top\">");
    out.println("Your email: </td>  <td valign=\"top\">");
    out.println("<input type=\"text\" name=\"email\" size=\"20\">");
    out.println("</td></tr><tr><td valign=\"top\">");
 
    out.println("<input type=\"submit\" value=\"Submit Info\"></td></tr>");
    out.println("</table></form>");
    out.println("</body></html>");
 
    }//doGet
  public void doPost(HttpServletRequest request, 
    HttpServletResponse response) throws ServletException,
    java.io.IOException {
    
    //display the parameter names and values
    Enumeration paramNames = request.getParameterNames( );
 
    String parName;//this will hold the name of the parameter
    
    boolean emptyEnum = false;
 
    if (! paramNames.hasMoreElements( ))
        emptyEnum = true;
    
        
    //set the MIME type of the response, "text/html"
    response.setContentType("text/html");
    
    //use a PrintWriter to send text data to the client 
    java.io.PrintWriter out = response.getWriter( );
    
    //Begin assembling the HTML content
    out.println("<html><head>");
    out.println("<title>Submitted Parameters</title></head><body>");
    
    if (emptyEnum){
        out.println(
           "<h2>Sorry, the request does not contain any parameters</h2>");
    } else {
    out.println(
        "<h2>Here are the submitted parameter values</h2>");
    }
    
    while(paramNames.hasMoreElements( )){
    
        parName = (String) paramNames.nextElement( );
 
        out.println(
            "<strong>" + parName + "</strong> : " +
                request.getParameter(parName));
 
        out.println("<br />");
 
    }//while
        
    out.println("</body></html>");
    
  }// doPost
}

Open in new window

0
Comment
Question by:hatem_from_mesr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 7

Expert Comment

by:gudii9
ID: 24092538
check all the filed types to make sure defined correctly
0
 
LVL 16

Accepted Solution

by:
warturtle earned 500 total points
ID: 24095234
Add the following lines to correct this problem:

import javax.servlet.ServletException;
import java.util.Enumeration;

And recompile the class again. Let us know, what you get.
0
 

Author Comment

by:hatem_from_mesr
ID: 24095839
hi warturtle,
it compile but there is a warning can you tell me what to do to avoid it
[root@localhost classes]# javac -classpath /opt/tomcat6/lib/servlet-api.jar -d ./ ./firstservlet.java
----------
1. WARNING in ./firstservlet.java (at line 7)
        public class firstservlet extends HttpServlet
                     ^^^^^^^^^^^^
The serializable class firstservlet does not declare a static final serialVersionUID field of type long
----------
1 problem (1 warning)[root@localhost classes]# ls
firstservlet.class  firstservlet.java  firstservlet.java~
[root@localhost classes]#
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

Author Comment

by:hatem_from_mesr
ID: 24095862
hi gudii9,
>>>check all the filed types to make sure defined correctly
I don't understand what you say
0
 
LVL 16

Expert Comment

by:warturtle
ID: 24096646
Yes, this warning is not coming from within JDK. It is possibly coming from within an IDE like Eclipse. Have a look at the solution within this thread and see if it works for you:

http://www.experts-exchange.com/Programming/Languages/Java/Q_21186340.html

If not let me know and provide more information on the enviroment within which this program is executing like IDE and JDK exact version.
0
 

Author Closing Comment

by:hatem_from_mesr
ID: 31567790
thanks man
0
 
LVL 16

Expert Comment

by:warturtle
ID: 24113957
Thanks for the feedback and happy to be of help :)
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Suggested Courses

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question