
1)init(ServletConfig config)
何时执行:servlet对象创建的时候执行
ServletConfig : 代表的是该servlet对象的配置信息
2)service(ServletRequest request,ServletResponse response)
何时执行:每次请求都会执行
ServletRequest :代表请求 认为ServletRequest 内部封装的是http请求的信息
ServletResponse :代表响应 认为要封装的是响应的信息
3)destroy()
何时执行:servlet销毁的时候执行
1)Servlet何时创建
默认(服务器启动时)第一次访问servlet时创建该对象
2)Servlet何时销毁
服务器关闭servlet就销毁了
3)每次访问必然执行的方法
service(ServletRequest req, ServletResponse res)方法
问题:对XXXServlet进行了10次访问,init(),destory(),service(),doGet(),doPost() 一共执行力多少次?request对象创建几个?response创建几个?
init():一次 (创建时) destory():一次(销毁时) service():10次(运行一次创建一次) doGet(),doPost()属于 service()
request对象创建10个(运行一次创建一次) response创建10个(运行一次创建一次)
1)init()
2)doGet(HttpServletRequest request,HttpServletResponse response)
3)doPost(HttpServletRequest request,HttpServletResponse response)
4)destroy()

其中url-pattern的配置方式:
1)完全匹配 访问的资源与配置的资源完全相同才能访问到、

2)目录匹配 格式:/虚拟的目录../* *代表任意

3)扩展名匹配 格式:*.扩展名

注意:第二种与第三种不要混用
/aaa/bbb/*.abcd(错误的)
创建Dynamic Web项目

在Webcontebt里新建jsp文件


<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="/WW/LoginServlet" method="post">
用户名:<input type="text" name="uname"><br>
密码:<input type="password" name="pwd"><br>
<input type="submit" value="注册"><br>
</form>
</body>
</html>
action="/WW/LoginServlet"需要运行的地址
LoginServlet页面
public class LoginServlet extends HttpServlet {
private UserService userService=new UserService();
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String uname=request.getParameter("uname");
String pwd=request.getParameter("pwd");
int row=userService.login(uname, pwd);
if(row>0){
response.getWriter().write("chenggong");;
}else{
response.getWriter().write("shibai");
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
UserService页面
public class UserService {
private UserDao userDao=new UserDao();
public int login(String uname,String pwd){
int row=0;
try {
row=userDao.login(uname, pwd);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return row;
}
}
UserDao页面
public class UserDao {
public int login(String uname,String pwd) throws SQLException{
Connection conn=JDBCUtils.getConn();
String sql="INSERT INTO USER (uname,pwd) VALUES(?,?) ";
PreparedStatement pst=conn.prepareStatement(sql);
pst.setString(1, uname);
pst.setString(2, pwd);
int row=pst.executeUpdate();
JDBCUtils.close(conn, pst);
return row;
}
}
JDBCUtils类
public class JDBCUtils {
//获取连接对象
public static Connection getConn(){
Connection conn=null;
try {
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/shop?characterEncoding=utf8";
String uname="root";
String ped="123456";
conn=DriverManager.getConnection(url,uname,ped);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
//释放资源方法
public static void close(Connection conn,PreparedStatement pst){
if(pst!=null){
try {
pst.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//查询
public static void close(Connection conn,PreparedStatement pst,ResultSet rs){
if(pst!=null){
try {
pst.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(conn!=null){
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.getWriter().write("hello dandan...");//往客户端打印
}
public void init(ServletConfig arg0) throws ServletException {
//获取ServletName
System.out.println(arg0.getServletName());
//获取初始化参数
System.out.println(arg0.getInitParameter("driver"));
//ServerContext con=arg0.getServletContext();
System.out.println("Servlet创建了");
}
原文:https://www.cnblogs.com/haoduoyu0512/p/13494786.html