我正在通过一组代码示例学习 servlets 3.0,许多方法的目的对我来说没有任何意义。服务方式除外。
输出是“来自 MyServlet 的你好”。但是,所有其他方法是什么?
@WebServlet(name = "MyServlet", urlPatterns = { "/my" })
public class MyServlet implements Servlet {
What is the line below trying to do?
private transient ServletConfig servletConfig;
@Override
public void init(ServletConfig servletConfig) throws ServletException {
this.servletConfig = servletConfig;
}
@Override
public ServletConfig getServletConfig() {
return servletConfig;
}
@Override
public String getServletInfo() {
return "My Servlet";
}
//This is the only method that makes sense to me. All the others, I have no
idea why they are in here.
@Override
public void service(ServletRequest request, ServletResponse response)
throws ServletException, IOException {
String servletName = servletConfig.getServletName();
response.setContentType("text/html");
PrintWriter writer = response.getWriter();
writer.print("<html><head></head>" + "<body>Hello from " + servletName
+ "</body></html>");
}
@Override
public void destroy() {
}
}