一.HTTP协议
1.什么是HTTP协议?
就是W3C制定的一个规范
规定了浏览器和服务器如何通信、通信的数据格式
2.如何通信
建立连接
发送请求
接收响应
关闭连接
一个请求一次连接,尽量降低服务器的压力
3.请求数据结构
请求行: 请求的基本信息
消息头: 对实体内容的描述
实体内容: 浏览器向服务器发送的业务数据
4.响应数据结构
状态行: 响应的基本信息
消息头: 对实体内容的描述
实体内容: 服务器向浏览器发送的业务数据
5.注意
1)很多事情不需要我们处理
通信的步骤由浏览器和服务器自动实现
请求数据中的请求行,消息头由浏览器自动填写
响应数据中的状态行,消息头通常由服务器自动填写
2.少量事情需要我们处理
请求数据中的实体内容由我们提供
响应数据中的实体内容由我们提供
通过request处理请求数据,通过response处理响应数据
二.注册案例 ![这里写图片描述](https://img-blog.csdn.net/20170624082435581?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXFfMzgxMzE2Njg=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
reg.html
<!DOCTYPE html>
<html >
<head >
<meta charset ="UTF-8" >
<title > 注册</title >
</head >
<body > <form action ="reg" method ="post" > <p > 账号:<input type ="text" name ="username" /> </p > <p > 密码:<input type ="password" name ="pwd" /> </p > <p > 性别:<input type ="radio" name ="sex" value ="M" /> 男<input type ="radio" name ="sex" value ="F" /> 女</p > <p > 兴趣:<input type ="checkbox" name ="interest" value ="food" /> 美食<input type ="checkbox" name ="interest" value ="game" /> 竞技<input type ="checkbox" name ="interest" value ="friend" /> 社交</p > <p > <input type ="submit" value ="注册" /> </p > </form >
</body >
</html >
RegServlet.java
package web;import java.io.IOException;
import java.io.PrintWriter;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;public class RegServlet extends HttpServlet {
@Override protected void service (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {req.setCharacterEncoding("utf-8" );String name = req.getParameter("username" );String pwd = req.getParameter("pwd" );String sex = req.getParameter("sex" );String[] interests = req.getParameterValues("interest" );System.out.println(name);System.out.println(pwd);System.out.println(sex);if (interests != null ) {for (String interest : interests) {System.out.println(interest);}}res.setContentType("text/html;charset=utf-8" );PrintWriter w = res.getWriter();w.println("<p>ok," +name+"</p>" );w.close();}}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xmlns ="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation ="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version ="2.5" > <display-name > servlet2</display-name > <welcome-file-list > <welcome-file > index.html</welcome-file > <welcome-file > index.htm</welcome-file > <welcome-file > index.jsp</welcome-file > <welcome-file > default.html</welcome-file > <welcome-file > default.htm</welcome-file > <welcome-file > default.jsp</welcome-file > </welcome-file-list > <servlet > <servlet-name > reg</servlet-name > <servlet-class > web.RegServlet</servlet-class > </servlet > <servlet-mapping > <servlet-name > reg</servlet-name > <url-pattern > /reg</url-pattern > </servlet-mapping > </web-app >
三.请求方式
1.什么是请求方式?
2.常用请求方式
3.GET和POST的区别(面试题)
1)GET
采用路径传参
参数在传递过程中可见,隐私性差
路径大小有限制,所有传递的参数大小受限
所有的请求默认都是GET请求
2)POST
采用实体内容传参
参数在传递过程中不可见,隐私性好
实体内容专门用来传参,大小不受限制
在表单上加method=”post”
3)建议
四.Servlet运行原理
五.乱码解决方案
六.查询员工案例
entity\Emp.java
package entity;import java.io.Serializable;public class Emp implements Serializable {
private Integer empno;private String ename;private String job;private Double sal;public Integer getEmpno () {return empno;}public void setEmpno (Integer empno) {this .empno = empno;}public String getEname () {return ename;}public void setEname (String ename) {this .ename = ename;}public String getJob () {return job;}public void setJob (String job) {this .job = job;}public Double getSal () {return sal;}public void setSal (Double sal) {this .sal = sal;}}
dao\EmpDao.java
package dao;import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;import entity.Emp;public class EmpDao implements Serializable {
public List<Emp> findAll () {List<Emp> list = new ArrayList<Emp>();Emp e1 = new Emp();e1.setEmpno(1 );e1.setEname("唐僧" );e1.setJob("领导" );e1.setSal(9000.0 );list.add(e1);Emp e2 = new Emp();e2.setEmpno(2 );e2.setEname("悟空" );e2.setJob("职员" );e2.setSal(5000.0 );list.add(e2);Emp e3 = new Emp();e3.setEmpno(3 );e3.setEname("八戒" );e3.setJob("职员" );e3.setSal(6000.0 );list.add(e3);return list;}}
web\FindEmpServlet.java
package webimport java.io .IOException
import java.io .PrintWriter
import java.util .List import javax.servlet .ServletException
import javax.servlet .http .HttpServlet
import javax.servlet .http .HttpServletRequest
import javax.servlet .http .HttpServletResponse import dao.EmpDao
import entity.Emp public class FindEmpServlet extends HttpServlet {@Overrideprotected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {//1. 接收参数//2. 处理业务EmpDao dao = new EmpDao()List<Emp> list = dao.findAll ()//3. 发送响应res.setContentType ("text/html;charset=utf-8" )PrintWriter w = res.getWriter ()w.println ("<table border='1' cellspacing='0' width='30%'>" )w.println (" <tr>" )w.println (" <td>编号</td>" )w.println (" <td>姓名</td>" )w.println (" <td>职位</td>" )w.println (" <td>薪资</td>" )w.println (" </tr>" )if(list != null) {for(Emp e : list) {w.println ("<tr>" )w.println (" <td>" +e.getEmpno ()+"</td>" )w.println (" <td>" +e.getEname ()+"</td>" )w.println (" <td>" +e.getJob ()+"</td>" )w.println (" <td>" +e.getSal ()+"</td>" )w.println ("</tr>" )}}w.println ("</table>" )w.close ()}
}
webapp\WEB-INF\web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xmlns ="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation ="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version ="2.5" > <display-name > EmpManager</display-name > <welcome-file-list > <welcome-file > index.html</welcome-file > <welcome-file > index.htm</welcome-file > <welcome-file > index.jsp</welcome-file > <welcome-file > default.html</welcome-file > <welcome-file > default.htm</welcome-file > <welcome-file > default.jsp</welcome-file > </welcome-file-list > <servlet > <servlet-name > findEmp</servlet-name > <servlet-class > web.FindEmpServlet</servlet-class > </servlet > <servlet-mapping > <servlet-name > findEmp</servlet-name > <url-pattern > /findEmp</url-pattern > </servlet-mapping > </web-app >