在Servlet程序编译期间遇到错误

时间:2018-05-01 05:02:06

标签: java servlets

import javax.Servlet.GenericServlet;
import javax.Servlet.ServletRequest;
import javax.Servlet.ServletResponse;
import javax.Servlet.ServletException;
import java.io.*;


public class MessageServlet extends GenericServlet
{
    public void service(ServletRequest request,ServletResponse response)throws ServletException,IOException
    {           
        PrintWriter out=res.getWriter();
        out.println("<h1`enter code here`>HIIIIIIIIIIIII</h1>");
        out.close();
    }
}

在编译上述程序时,我收到以下错误。

public class MessageServlet extends GenericServlet
                                ^
symbol: class GenericServlet
MessageServlet.java:10: error: cannot find symbol
    public void service(ServletRequest request,ServletResponse response)throws ServletException,IOException
                        ^
symbol:   class ServletRequest
location: class MessageServlet
MessageServlet.java:10: error: cannot find symbol
    public void service(ServletRequest request,ServletResponse response)throws ServletException,IOException
                                               ^
symbol:   class ServletResponse
location: class MessageServlet
MessageServlet.java:10: error: cannot find symbol
    public void service(ServletRequest request,ServletResponse response)throws ServletException,IOException

^
symbol:   class ServletException
location: class MessageServlet
MessageServlet.java:12: error: cannot find symbol
            PrintWriter out=res.getWriter();
                            ^
symbol:   variable res
location: class MessageServlet
9 errors

1 个答案:

答案 0 :(得分:0)

首先,import语句是错误的。 servlet中应该有一个小的's'。 其次,对象或ServletResponse是Response,你使用了'res'

正确的代码

import java.io.*;
import javax.servlet.GenericServlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;


public class NewServlet extends GenericServlet
{
    @Override
    public void service(ServletRequest request,ServletResponse response)throws ServletException,IOException
    {           
        PrintWriter out=response.getWriter();
        out.println("<h1`enter code here`>HIIIIIIIIIIIII</h1>");
        out.close();
    }
}