Servlet笔记
public class UserLogin extends HttpServlet { }
2可以使用eclipse新建Servlet
常重写doGet() 和doPost()方法
protectedvoid doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("doGet!");
}
protectedvoid doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("doPost!");
}
重启重装不二法宝
3Servlet 在web.xml中配置
<servlet>
<description></description>
<display-name>UserLogin</display-name>
<servlet-name>UserLogin</servlet-name>
<servlet-class>erLogin</servlet-class>
</servlet>
<servlet-mapping>映射
<servlet-name>UserLogin</servlet-name>
<url-pattern>/UserLogin</url-pattern>“/”开头
</servlet-mapping>
例子:
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
publicclass TestLifeCycleServlet extends HttpServlet {
//处理请求
protectedvoid doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("doGet");
}
/* 实例化
注意:构造方法不能有返回值类型,连void也不行(否则就变成普通方法了)*/
public TestLifeCycleServlet() {
System.out.println("constructed");
}
// 退出服务
publicvoid destroy() {
System.out.println("destory");
}
// 初始化
publicvoid init() throws ServletException {
System.out.println("init");
}
}
测试结果(在tomcat服务器控制台窗口):
constructed
init
doGet
Servlet编程接口
例子1读取指定的参数:ThreeParams.Java &ThreeParams.htm
例子2读取所有的参数:ShowParameters.java&ShowParametersForm.htm
6 Cookie(客户端)
7. Session(服务器端)
Session过期时间web.xm设置
<session-config>
<session-timeout>30</session-timeout> </session-config>
8 Application
9Servlets使用java bean:
10 Servlet中使用专门的数据库操作类DB
①Servlet中使用专门的数据库操作类DB,这样,本程序就比前一个显得简单清楚。