当前位置: 技术问答>java相关
簡單的程序,不解的問題。
来源: 互联网 发布时间:2015-10-04
本文导语: 我的程序: import java.io.*; import java.util.*; import javax.servlet.*; public class Morning extends HttpServlet { public void service(HttpServletRequest req,HttpServletResponse res) throws ServletException,IOException { res.setContentType("text/html;charset=Bi...
我的程序:
import java.io.*;
import java.util.*;
import javax.servlet.*;
public class Morning extends HttpServlet
{
public void service(HttpServletRequest req,HttpServletResponse res)
throws ServletException,IOException
{
res.setContentType("text/html;charset=Big5");
PrintWriter pw=res.getWriter();
pw.println("");
pw.println("");
pw.println("");
pw.println("");
pw.println("GOOD MORNING!!
");
pw.println("");
pw.println("");
}
}
*******編譯成class出現的問題*********************************
D:WorkingTomcathowjspWeb-INFclassesMorning.java:5: cannot resolve symbol
symbol : class HttpServlet
location: class Morning
public class Morning extends HttpServlet
^
D:WorkingTomcathowjspWeb-INFclassesMorning.java:7: cannot resolve symbol
symbol : class HttpServletRequest
location: class Morning
public void service(HttpServletRequest req,HttpServletResponse res)
^
D:WorkingTomcathowjspWeb-INFclassesMorning.java:7: cannot resolve symbol
symbol : class HttpServletResponse
location: class Morning
public void service(HttpServletRequest req,HttpServletResponse res)
^
3 errors
Tool completed with exit code 1
問題在哪里?怎么解決?HELP
import java.io.*;
import java.util.*;
import javax.servlet.*;
public class Morning extends HttpServlet
{
public void service(HttpServletRequest req,HttpServletResponse res)
throws ServletException,IOException
{
res.setContentType("text/html;charset=Big5");
PrintWriter pw=res.getWriter();
pw.println("");
pw.println("");
pw.println("");
pw.println("");
pw.println("GOOD MORNING!!
");
pw.println("");
pw.println("");
}
}
*******編譯成class出現的問題*********************************
D:WorkingTomcathowjspWeb-INFclassesMorning.java:5: cannot resolve symbol
symbol : class HttpServlet
location: class Morning
public class Morning extends HttpServlet
^
D:WorkingTomcathowjspWeb-INFclassesMorning.java:7: cannot resolve symbol
symbol : class HttpServletRequest
location: class Morning
public void service(HttpServletRequest req,HttpServletResponse res)
^
D:WorkingTomcathowjspWeb-INFclassesMorning.java:7: cannot resolve symbol
symbol : class HttpServletResponse
location: class Morning
public void service(HttpServletRequest req,HttpServletResponse res)
^
3 errors
Tool completed with exit code 1
問題在哪里?怎么解決?HELP
|
改为
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class GetEnvVar extends GenericServlet
{
public void service(ServletRequest req,ServletResponse res)
throws ServletException,IOException
{
ServletContext sc=getServletContext();
res.setContentType("text/plain");
ServletOutputStream sos=res.getOutputStream();
PrintWriter pw=new PrintWriter(sos,true);
pw.println("名稱:"+req.getServerName());
pw.println("端口:"+req.getServerPort());
pw.println("網頁路徑"+sc.getRealPath("/"));
pw.println("遠端主機名稱"+req.getRemoteHost());
pw.println("遠端主機位置"+req.getRemoteAddr());
}
}
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class GetEnvVar extends GenericServlet
{
public void service(ServletRequest req,ServletResponse res)
throws ServletException,IOException
{
ServletContext sc=getServletContext();
res.setContentType("text/plain");
ServletOutputStream sos=res.getOutputStream();
PrintWriter pw=new PrintWriter(sos,true);
pw.println("名稱:"+req.getServerName());
pw.println("端口:"+req.getServerPort());
pw.println("網頁路徑"+sc.getRealPath("/"));
pw.println("遠端主機名稱"+req.getRemoteHost());
pw.println("遠端主機位置"+req.getRemoteAddr());
}
}
|
粗心的错误
public void service(ServletRequest req,ServletRequest res)
两个参数的类型都是ServletRequest了!!!!
service(javax.servlet.ServletRequest,javax.servlet.ServletResponse)
public void service(ServletRequest req,ServletRequest res)
两个参数的类型都是ServletRequest了!!!!
service(javax.servlet.ServletRequest,javax.servlet.ServletResponse)
|
这个类应该继承HttpServlet这个类才可以在下面用
ServletRequest req,ServletRequest res,
ServletRequest req,ServletRequest res,
|
不是继承GenericServlet
而是继承HttpServlet
而是继承HttpServlet
|
少了import javax.servlet.http.*;
完整下面:
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Morning extends HttpServlet
{
public void service(HttpServletRequest req,HttpServletResponse res)
throws ServletException,IOException
{
res.setContentType("text/html;charset=Big5");
PrintWriter pw=res.getWriter();
pw.println("");
pw.println("");
pw.println("");
pw.println("");
pw.println("GOOD MORNING!!
");
pw.println("");
pw.println("");
}
}
完整下面:
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Morning extends HttpServlet
{
public void service(HttpServletRequest req,HttpServletResponse res)
throws ServletException,IOException
{
res.setContentType("text/html;charset=Big5");
PrintWriter pw=res.getWriter();
pw.println("");
pw.println("");
pw.println("");
pw.println("");
pw.println("GOOD MORNING!!
");
pw.println("");
pw.println("");
}
}
|
1.未定义GetEnvVar()方法,将service()换成GetEnvVar()
2.3.可能是jdk版本的问题,我遇到过类似的,jdk1.3的session.getAttribute()在jdk1.2中是getValue()
2.3.可能是jdk版本的问题,我遇到过类似的,jdk1.3的session.getAttribute()在jdk1.2中是getValue()
|
去下载一个servlet.jar的包
去java.sun.com找找
然后把它设置到classpath中去
去java.sun.com找找
然后把它设置到classpath中去
|
怎么设的?