Servlet测试代码:
package testservlet;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class servlet1 extends HttpServlet {
private static final String CONTENT_TYPE="text/html;charset=GBK";
public void init() throws ServletException{
}
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>让中文不再是乱码</title></head>");
out.println("<body bgcolor=\"#ffffff\">");
out.println("<span style=\"font-size:9pt;\"> 让输出的中文不再是乱码!<br><br>");
out.println("这回总算可以看到你的真面目啦</span>");
out.println("</body></html>");
}
public void doPost(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{
doGet(request,response);
}
public void destroy(){
}
}
然而在eclipse里却显示
servlet cannot be resolved to a type
servlet.http cannot be resolved to a type
找了半天资料才知道jdk 里不包括servlet-api.jar这个包,于是到tomcat里找到这个包复制到jdk\jre\lib下
再设置classpath,结果还是无济于事,后来闯进一个英文论坛看到似乎好像说工程里的路径权限高,并且eclipse并到classpath里寻找jar位置,所以我就到我的java项目里
项目名-->右键
Property-->选择
Java Build Path-->选择
Add External JARs-->选择
把servlet-api.jar的路径输入即可 该包在tomcat里有 搜索即可找到