1,定义servlet;
2,servlet生命周期;
3,HttpServlet常用方法;
4,servlet常用接口;
一,servlet定义:
servlet是一个继承HttpServlet类的java类,直接利用开发工具创建,
servlet可以接收页面的跳转,进行逻辑处理,<form>表单的action对
应servlet的url,同一工程下为“../servletName”。
二,servlet生命周期:
1,生成servlet,加载到Servlet Engine中,通过调用 init() 方法初始化;
2,通过提供的响应方法(doGet, doPost, service)处理客户端请求;
3,调用 distroy() 方法进行销毁。
三,servelet处理客户端的常用方法:
1,doGet() 处理 get 方式提交的表单,也可处理浏览器的直接访问。
//设置输出内容格式与编码
response.setContentType("text/html; charset=utf-8");
//设置接收参数的编码格式
request.setCharacterEncoding("utf-8");
//接收参数
String uname = request.getParameter("uname");
//获取 PrintWriter 向网页输出内容
PrintWriter out = response.getWriter();
用户登录示例:
(1.前端页面 userForm.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="../DoGetDemo" method="post"> <table align="center"> <tr> <td>账号:</td> <td><input type="text" name="uname" /></td> </tr> <tr> <td>密码:</td> <td><input type="password" name="password" /></td> </tr> <tr> <td><input type="submit" value="提交" /></td> <td><input type="reset" value="重置" /></td> </tr> </table> </form> </body> </html>
(2.servlet接收提交的数据)
package com.javaweb.servelet; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class DoGetDemo */ @WebServlet("/DoGetDemo") public class DoGetDemo extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public DoGetDemo() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub //doPost(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub //设置输出内容格式与编码 response.setContentType("text/html; charset=utf-8"); PrintWriter out = response.getWriter(); //设置接收参数的编码格式 request.setCharacterEncoding("utf-8"); //接收参数 String uname = request.getParameter("uname"); String password = request.getParameter("password"); //输出结果 out.println("<html>"); out.println("<body>"); out.println("用户:"+uname); out.println("<br>密码:"+password); out.println("</body>"); out.println("</html>"); } }
2,doPost() 处理 post 方式提交的表单,不可处理浏览器的直接访问。
代码与 doGet() 相同。
3,service() 处理 get , post方式提交的表单,以及客户浏览器的直接访问。
四,servlet常用接口:
1,HttpServletRequest 对应于jsp的内置对象request;
2,HttpServletResponse 对应于jsp的内置对象response;
3,HttpSession 类似于jsp的内置对象session,必须通过 request 的getSession() 获取;
4,ServletContext 类似于jsp的内置对象 application,必须通过 HttpServlet 的 getServletContext()获取
ServletContext application = getServletContext();