package app01a;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.GenericServlet;
import javax.servlet.Servlet;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
/**
* 如何访问:
* http://localhost:端口号(使用tomcat没有修改过的是8080)/项目名/下面WebServlet中的urlPatterns
* 如果使用的是web.xml配置文件,那么访问路径就变为:
* http://localhost:端口号(使用tomcat没有修改过的是8080)/项目名/web.xml中该servlet对应的url-pattern
* @author Administrator
*
*/
@WebServlet( name = "GenericServletDemoServlet",
urlPatterns = { "/generic" },
initParams = {
@WebInitParam( name = "admin", value = " Harry Taciak"),
@WebInitParam( name = "email", value = " admin@example.com")
})
public class GenericServletDemoServlet extends GenericServlet{
private static final long serialVersionUID = 1L;
public GenericServletDemoServlet() {
}
public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
ServletConfig servletConfig = getServletConfig();
String admin = servletConfig.getInitParameter("admin");
String email = servletConfig.getInitParameter("email");
response.setContentType("text/html");
PrintWriter writer = response.getWriter();
writer.print("<html><head></head><body>" + "Admin:" + admin + "<br/>Email:" + email +
"</body></html>");
}
}