设计一个Servlet程序,用于生成一个HTML页面,在HTML页在中显示自己的学号、姓名、所在班级等信息。 说明:(1)姓名、所在班级可以使用英文或汉语拼音,不可以使用汉字。 (2)Servlet的配置使用注解方式,Servlet的名称以及访问路径可自义。
时间: 2024-02-27 21:57:34 浏览: 69
这个问题的答案已经在之前回答过了,以下是之前的回答:
``` java
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(name = "MyInfoServlet", urlPatterns = { "/myinfo" })
public class MyInfoServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private String name = "Your Name";
private String studentId = "Your Student ID";
private String className = "Your Class Name";
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>My Info</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>My Info</h1>");
out.println("<p>Name: " + name + "</p>");
out.println("<p>Student ID: " + studentId + "</p>");
out.println("<p>Class Name: " + className + "</p>");
out.println("</body>");
out.println("</html>");
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
```
请注意,为了使Servlet能够在浏览器中正确显示中文,我们在 `response` 对象中设置了字符编码为 UTF-8。
阅读全文