当前位置: 技术问答>java相关
如何将一段.java的servlet编译成.class?
来源: 互联网 发布时间:2015-04-11
本文导语: 照书抄的一段很简单的东西:怎么样都编译不成。 import java.io.*; import java.servlet.*; import javax.servlet.*; import javax.servlet.http.*; public class helloworld extends HttpServlet { public static void doGet(HttpServletRequest request,HttpServl...
照书抄的一段很简单的东西:怎么样都编译不成。
import java.io.*;
import java.servlet.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class helloworld extends HttpServlet
{
public static void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException
{
response.setContentType("text/html");
PrintWriter out=response.getWriter();
out.println("");
out.println("");
out.println("");
out.println("Hello World!");
out.println("");
out.println("");
out.println("Hello World!");
out.println("");
out.println("");
}
}
显示结果如下:
>javac helloworld.java
>helloworld.java:2: package java.servlet does not exist
import java.servlet.*;
^
helloworld.java:8: cannot resolve symbol
symbol : class IOException
location: class helloworld
public static void doGet(HttpServletRequest request,HttpServletResponse
response) throws IOException,ServletException
^
helloworld.java:8: doGet(javax.servlet.http.HttpServletRequest,javax.servlet.ht
p.HttpServletResponse) in helloworld cannot override doGet(javax.servlet.http.H
tpServletRequest,javax.servlet.http.HttpServletResponse) in javax.servlet.http.
ttpServlet; doGet(javax.servlet.http.HttpServletRequest,javax.servlet.http.Http
ervletResponse) and doGet(javax.servlet.http.HttpServletRequest,javax.servlet.h
tp.HttpServletResponse) are static
public static void doGet(HttpServletRequest request,HttpServletResponse
response) throws IOException,ServletException
^
helloworld.java:11: cannot resolve symbol
symbol : class PrintWriter
location: class helloworld
PrintWriter out=response.getWriter();
^
import java.io.*;
import java.servlet.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class helloworld extends HttpServlet
{
public static void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException
{
response.setContentType("text/html");
PrintWriter out=response.getWriter();
out.println("");
out.println("");
out.println("");
out.println("Hello World!");
out.println("");
out.println("");
out.println("Hello World!");
out.println("");
out.println("");
}
}
显示结果如下:
>javac helloworld.java
>helloworld.java:2: package java.servlet does not exist
import java.servlet.*;
^
helloworld.java:8: cannot resolve symbol
symbol : class IOException
location: class helloworld
public static void doGet(HttpServletRequest request,HttpServletResponse
response) throws IOException,ServletException
^
helloworld.java:8: doGet(javax.servlet.http.HttpServletRequest,javax.servlet.ht
p.HttpServletResponse) in helloworld cannot override doGet(javax.servlet.http.H
tpServletRequest,javax.servlet.http.HttpServletResponse) in javax.servlet.http.
ttpServlet; doGet(javax.servlet.http.HttpServletRequest,javax.servlet.http.Http
ervletResponse) and doGet(javax.servlet.http.HttpServletRequest,javax.servlet.h
tp.HttpServletResponse) are static
public static void doGet(HttpServletRequest request,HttpServletResponse
response) throws IOException,ServletException
^
helloworld.java:11: cannot resolve symbol
symbol : class PrintWriter
location: class helloworld
PrintWriter out=response.getWriter();
^
|
同上,首先要建立好支持Servlet的环境,这样才能正确编译Servlet
建议安装tomcat
或使用JBuilder之类的集成开发环境,直接支持Servlet/Ejb等
建议安装tomcat
或使用JBuilder之类的集成开发环境,直接支持Servlet/Ejb等
|
在classpath里面设置servlet.jar。
servlet.jar并不带在jdk里面,一般tomcat之类的含有。
servlet.jar并不带在jdk里面,一般tomcat之类的含有。
|
路径没有设置正确,真的!