重写httpservlet中的doget()方法和dopost()方法打印出九九乘法表。
时间: 2023-04-24 11:06:48 浏览: 318
重写HttpServlet中的doGet()方法和doPost()方法,打印出九九乘法表。
```java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MultiplicationTableServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head>");
out.println("<title>九九乘法表</title>");
out.println("</head>");
out.println("<body>");
out.println("<table border='1'>");
for (int i = 1; i <= 9; i++) {
out.println("<tr>");
for (int j = 1; j <= i; j++) {
out.println("<td>" + j + "*" + i + "=" + (i * j) + "</td>");
}
out.println("</tr>");
}
out.println("</table>");
out.println("</body>");
out.println("</html>");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
```
以上代码中,我们重写了HttpServlet中的doGet()方法和doPost()方法,使用PrintWriter对象输出HTML代码,生成九九乘法表。在doGet()方法中
阅读全文