我对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
文件 - &gt;
<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>
当我运行这个项目时,就像这样 - &gt; http://localhost:8080/ColorProject/ColorServlet
我只能在浏览器上看到一个完整的黑屏..可能是什么问题?
答案 0 :(得分: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();
}
}