日期:2014-05-18  浏览次数:20880 次

这段代码为什么编译不过去?
import   java.io.*;  
import   javax.servlet.*;  
import   javax.servlet.http.*;  
public   class   hello   extends   httpservlet{  
public   void   init(servletconfig   config)   throws   servletexception{  
super.init(config);  
}  
public   void   service(httpservletrequest   request,httpservletresponse   response)   throws   ioexception{  
printwriter   out=response.getwriter();  
response.setcontenttype( "text/html;charset=gb2312 ");  
out.println( " <html> <body> ");  
out.println( "hello! ");  
out.println( " </body> </html> ");  
}  
}  

保存为.java文件,编译的时候居然出现了10个错误!大家帮忙看看啊

------解决方案--------------------
当然
------解决方案--------------------
你的程序中的类名怎么都是小写的啊,类名都不对,那样肯定编译不过的啊
------解决方案--------------------
问题1:你的 javax.servlet 包没有加载到 classpath 中,包在 %TOMCAT_HOM%/common/lib 下;

问题2:你所有的代码全写成小的了,在 Java 中变量、类等都是必须区分大小的。比如:httpservlet 应为 HttpServlet 之类的,你这一段里面还有很多很多类似的问题,我修改了一下:

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Hello extends HttpServlet {
  
  public void init(ServletConfig config) throws ServletException {
    super.init(config);
  }

  public void service(HttpServletRequest request, HttpServletResponse response)
      throws IOException {
    PrintWriter out = response.getWriter();
    response.setContentType( "text/html;charset=gb2312 ");
    out.println( " <html> <body> ");
    out.println( "hello! ");
    out.println( " </body> </html> ");
  }
}
------解决方案--------------------
运行servlet需要在环境变量里加classpath chasspath类容是路径\Tomcat 5.5\common\lib\servlet-api.jar 加了这个才能运行servlet
------解决方案--------------------
你的代码把大小写都混乱了
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class hello extends HttpServlet {
public void init(ServletConfig config) throws ServletException {
super.init(config);
}

public void service(HttpServletRequest request, HttpServletResponse response)
throws IOException {
PrintWriter out = response.getWriter();
response.setContentType( "text/html;charset=gb2312 ");
out.println( " <html> <body> ");
out.println( "hello! ");
out.println( " </body> </html> ");
}
}
------解决方案--------------------
你就不能用个好点的IDE么
------解决方案--------------------
把包 servlet-api.jar
加入到classpath 再编译