一、Servlet介绍:
Servlet是基于Java语言的Web服务器端编程技术,按照Java EE规范定义,Servlet是运行在Servlet容器中的Java类,它能处理Web客户的HTTP请求,并产生HTTP响应。
根据请求的方式调用doGet()或doPost()
public class ServletDemo1 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("hello!!");
/*out.flush();
out.close();*/
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
可以用service方法代替doGet()和doPost()
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.service(req, resp);
}
二、ServletConfig对象
1.ServletConfig对象的作用
主要是用于加载servlet的初始化参数。在一个web应用可以存在多个ServletConfig对象(一个Servlet对应一个ServletConfig对象)
三、读取配置信息(ServletConfig):
1.servlet的初始化参数配置
实例代码:servlet