0

我对 java servlet 和使用 netbeans 有点陌生,问题是,我没有得到预期的输出。这是ColorServlet.java我拥有的文件-->

import javax.servlet.*;
import java.io.*;
public class ColorServlet extends GenericServlet
{

    @Override
    public void service(ServletRequest request,ServletResponse response) throws IOException,ServletException
    {
        PrintWriter out=response.getWriter();
        response.setContentType("text/html");
        String s=request.getParameter("s1");
        out.println("<Body bgcolor="+s+"></body>");
        out.close();
    }
}

这是myhtml.html我拥有的文件-->

<html>
    <head><title>This is a Color project</title></head>
    <body>
        <form action="ColorServlet">
            Select your favorite Color
            <select name="s1">
                <option value="Red">Red</option>
                <option>Green</option>
                <option>Yellow</option>
            </select>
            <br>
            <input type="submit" value ="OK"/>
        </form>
    </body>
</html>

当我运行这个项目时,像这样->http://localhost:8080/ColorProject/ColorServlet 我只能在浏览器上看到一个完全黑屏..可能是什么问题?

4

1 回答 1

0

您需要 HttpServlet 来获取请求并构造响应。像这样更改您的servlet

import javax.servlet.*;
import java.io.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;

@WebServlet(name = "ColorServlet", urlPatterns = {"/ColorServlet"})
public class ColorServlet extends HttpServlet
{

    @Override
    public void service(ServletRequest request,ServletResponse response) throws IOException,ServletException
    {
        PrintWriter out=response.getWriter();
        response.setContentType("text/html");
        String s=request.getParameter("s1");
        out.println("<Body bgcolor="+s+"></body>");
        out.close();
    }
}
于 2013-06-29T15:07:46.063 回答