-
servlet
- servlet是什么 servlet属于javaee规范里面的一个技术. servlet是运行在服务器上的小型java程序,servlet可以用来接收客户端请求并且给客户端一个响应.
- servlet有什么用 servlet是用来开发动态网页的技术.
- servlet怎么用
- 实现步骤
- 创建servlet实例
public class ServletDemo extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { req.setCharacterEncoding("utf-8"); String method = req.getMethod(); System.out.println(method); String name = req.getParameter("name"); String encode = URLEncoder.encode(name, "iso-8859-1"); System.out.println("+++" + encode); String decode = URLDecoder.decode(encode, "utf-8"); System.out.println("====" + decode); String pwd = req.getParameter("pwd"); String sex = req.getParameter("sex"); System.out.println(name + "---" + pwd + "---" + sex); String[] fruits = req.getParameterValues("fruit"); for (String fruit : fruits) { System.out.println(fruit); } resp.setHeader("content-type", "text/html;charset=utf-8"); resp.getOutputStream().write("登录成功".getBytes("utf-8")); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); }}
-
-
- 在web.xml中配置servlet 原理类似android中清单文件
-
a ngyb.ServletDemo a /a
-
-
-
- 配置文件的作用 :通过配置文件来找到我们要访问的servlet
- servlet运行原理
- servlet生命周期
- 生命周期:比如 人 花草树木
- 声明周期过程:
- 当tomcat启动的时候servlet并没有创建
- 当访问servlet的时候 tomcat服务器把servlet创建 构造—>init()—->service方法
- 当tomcat关闭的时候servlet销毁.
- service方法和doget & doPost关系
- 根据请求的方式不同,如果请求方式是get请求底层还是调用的doGet方法 如果是post请求底层调用post方法
- servletConfig对象
-
-
req.setCharacterEncoding("utf-8"); String method = req.getMethod(); System.out.println(method); String name = req.getParameter("name"); String encode = URLEncoder.encode(name, "iso-8859-1"); System.out.println("+++" + encode); String decode = URLDecoder.decode(encode, "utf-8"); System.out.println("====" + decode); String pwd = req.getParameter("pwd");
-
-
- urlpattern的三种写法
- 完全路径匹配 /demo
- 任意路径匹配 /*
- 扩展名匹配 .do .action
- 在实际开发中使用完全路径匹配.
- servletContext对象
- 这个对象在javaee开发中也理解成是一个工具箱
- urlpattern的三种写法
-
req.setCharacterEncoding("utf-8"); String method = req.getMethod(); System.out.println(method); String name = req.getParameter("name"); String encode = URLEncoder.encode(name, "iso-8859-1"); System.out.println("+++" + encode); String decode = URLDecoder.decode(encode, "utf-8"); System.out.println("====" + decode); String pwd = req.getParameter("pwd"); String sex = req.getParameter("sex"); System.out.println(name + "---" + pwd + "---" + sex); String[] fruits = req.getParameterValues("fruit"); for (String fruit : fruits) { System.out.println(fruit); }
-
-
- request对象
- 请求 :请求行 请求头 请求体 所有请求的信息被封装到了request对象
- 响应 :响应行 响应头 响应体 所有响应的信息被封装到了response对象
- 实现代码
- html代码
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%>
My JSP 'index.jsp' starting page - servlet代码
- html代码
- request对象
-
req.setCharacterEncoding("utf-8"); String method = req.getMethod(); System.out.println(method); String name = req.getParameter("name"); String encode = URLEncoder.encode(name, "iso-8859-1"); System.out.println("+++" + encode); String decode = URLDecoder.decode(encode, "utf-8"); System.out.println("====" + decode); String pwd = req.getParameter("pwd"); String sex = req.getParameter("sex"); System.out.println(name + "---" + pwd + "---" + sex); String[] fruits = req.getParameterValues("fruit"); for (String fruit : fruits) { System.out.println(fruit); }
-
-
-
-
- 乱码问题
- post方式解决乱码
- 乱码问题
-
-
-
req.setCharacterEncoding("utf-8");
-
-
-
-
-
- get方式提交数据乱码
- 由于get提交数据是以组拼url地址的形式提交 “张三”& 要对特殊符号& 和中文进行url编码
- get方式提交数据乱码
-
-
-
-
String name = req.getParameter("name"); String encode = URLEncoder.encode(name, "iso-8859-1"); System.out.println("+++" + encode); String decode = URLDecoder.decode(encode, "utf-8"); System.out.println("====" + decode);
-
-
- response
-
resp.setHeader("content-type", "text/html;charset=utf-8"); resp.getOutputStream().write("登录成功".getBytes("utf-8"));