学习内容:
1.Servlet
2.过滤器
一.Servlet
1. Servlet的生命周期:
实例化-----Servlet容器创建Servlet的实例
初始化-----容器调用init()方法
服务-----客户端请求Servlet,则调用其下的方法
摧毁-----摧毁前调用destroy()方法
2. 创建Servlet
右键new有个Servlet选项新建,一般自动继承HttpServlet类
注:doget方法很少使用,注册都是dopost形式处理,所以可以在doget直接调用dopost
例:doPost(request,response);
创建好后参考代码:
public class Servlet extends HttpServlet {
public Servlet() {
super();
}
public void destroy() {
System.out.println("生命周期结束时");
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
public void init() throws ServletException {
System.out.println("生命周期初始化");
System.out.println();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
doPost(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the POST method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
}
3.通过xml文件查看跳转页面和Servlet路径属性
注:提交该页面的路径要写上项目名
注:测试Servlet只要在客户端请求的时候才会触发,如需要测试可以直接通过URL地址访问获取测试结果
注意:获取值
参考案例:
MyHtml.html中通过form表单添加信息后提交给Servlet,调用Post方法中获得name及pwd的值,调用dao类进行判断是否正确的用户名登录,如true进行跳转Index.jsp页面,否则不跳
MyHtml.html参考代码:
<html>
<head>
<title>MyHtml.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
</head>
<body>
<form action="/TZ/serlvet/Servlet" method="post">
用户名:<input text="text" name="name"></input>
密码:<input text="password" name="pwd"></input>
<input type="submit" value="提交"></input>
</form>
</body>
</html>
Servlet.java参考代码:
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String name =request.getParameter("name");
String pwd =request.getParameter("pwd");
UserDao userdao = new UserDao();
boolean bb = userdao.islogin(name, pwd);
if(bb){
response.sendRedirect("../index.jsp");
}
}
二.过滤器
可以使用过滤器修改编码格式
1.创建过滤器需要实现Filter接口,设置编号格式后并重新调用方法
1.Servlet
2.过滤器
一.Servlet
1. Servlet的生命周期:
实例化-----Servlet容器创建Servlet的实例
初始化-----容器调用init()方法
服务-----客户端请求Servlet,则调用其下的方法
摧毁-----摧毁前调用destroy()方法
2. 创建Servlet
右键new有个Servlet选项新建,一般自动继承HttpServlet类
注:doget方法很少使用,注册都是dopost形式处理,所以可以在doget直接调用dopost
例:doPost(request,response);
创建好后参考代码:
public class Servlet extends HttpServlet {
public Servlet() {
super();
}
public void destroy() {
System.out.println("生命周期结束时");
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
public void init() throws ServletException {
System.out.println("生命周期初始化");
System.out.println();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
doPost(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the POST method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
}
3.通过xml文件查看跳转页面和Servlet路径属性
注:提交该页面的路径要写上项目名
可以在web.xml中添加变量和属性
注:测试Servlet只要在客户端请求的时候才会触发,如需要测试可以直接通过URL地址访问获取测试结果
注意:获取值
参考案例:
MyHtml.html中通过form表单添加信息后提交给Servlet,调用Post方法中获得name及pwd的值,调用dao类进行判断是否正确的用户名登录,如true进行跳转Index.jsp页面,否则不跳
MyHtml.html参考代码:
<html>
<head>
<title>MyHtml.html</title>
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="this is my page">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
</head>
<body>
<form action="/TZ/serlvet/Servlet" method="post">
用户名:<input text="text" name="name"></input>
密码:<input text="password" name="pwd"></input>
<input type="submit" value="提交"></input>
</form>
</body>
</html>
Servlet.java参考代码:
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String name =request.getParameter("name");
String pwd =request.getParameter("pwd");
UserDao userdao = new UserDao();
boolean bb = userdao.islogin(name, pwd);
if(bb){
response.sendRedirect("../index.jsp");
}
}
二.过滤器
可以使用过滤器修改编码格式
1.创建过滤器需要实现Filter接口,设置编号格式后并重新调用方法