一、入门
在webapp里面必须先创建一个文件夹,文件夹里面放的内容,才会被访问到。

创建一个javaweb项目后


二、servlet
1.概述


2.servlet生命周期

3.servlet实例的创建时机

4.Servlet实例的初始化参数

5.HTTP状态码


6.servelet返回JSON数据

7.服务端设置跨域访问



8.HttpServletRequest常用api

9、HttpServletResponce常用api

还有一个
req.getParameterMap() 可以获得一个map,

Servelet问题:
1.其中@Webservelet的urlPatterns 和 value有什么区别

2.关于servlet生命周期的困惑
当我第一次访问一个网址时,servelet会实例化并调用init()和service()
当我重复访问该网址,servlet会反复调用service()
当关闭服务器后 或Tomcat容器 决定销毁一个servlet时,会调用destroy()方法

3.实际开发中,跨域问题的解决


三、

过滤器 设置 utf_8
监听器

session监听
mvc

三、Servlet会话跟踪基础
1.会话跟踪技术
1.1 什么是会话
客户端访问服务端发生的一系列请求和响应过程称之为会话。 通俗的说:从打开浏览器向服务器端发送请求开始,到客户端关闭浏览器结束访问为止,就是一个会话。
1.2 会话跟踪技术
HTTP 是一种"无状态"协议。也就是说:当一个请求响应结束后,不会留下任何痕迹。或者说:下一次请求响应不会获得上一次请求响应的任何信息。
但是,从开发角度考虑,我们希望上一次请求所传递的数据能够维持状态到下一次请求,并且辨认出是否相同的客户端所发送出来的。也就是说:服务器端业务是需要有状态的。比如:登陆一次后,在一个会话范围内,就不用再重复登陆了。
那么会话跟踪技术,就是一种在客户端与服务器间保持HTTP状态的解决方案,管理浏览器客户端和服务器端之间会话过程中产生的会话数据。
会话跟踪技术解决方案有:
- Cookie
- Session
- URL重写
- 隐藏表单域
- ... ...
1.3 Cookie
Cookie由服务器生成,然后发送给客户端浏览器,浏览器会将 Cookie保存到客户端内存中或者保存到客户端某个目录下的文本文件内。下次请求同一网站时就发送该 Cookie给 服务器(前提是浏览器设置为启用 Cookie)。
Cookie较为典型的应用就是:保存已成功登录过的用户的用户名信息,以便在 下一次登录此网站时,不需输入用户名,简化登录手续,完成自动登录等功能。
注意: 如果是通过ajax 访问,那么sevlet会使用out.print()向客户端响应数据,并且在Cookie中写入sessionid,返回给客户端。但是这样一来,这样就会将cookie中原有的值覆盖掉。也就是说:通过ajax的请求方式,服务器端将无法为客户端设置cookie。
所以,下面使用表单提交来演示Cookie。
前端index.html文件
html
<h3>登陆</h3>
<form action="http://localhost:8080/demo/login" method="post">
用户名:<input type="text" name="userName"><br>
密码:<input type="password" name="password"><br>
<input type="submit">
</form>
<a href="http://localhost:8080/demo/login">自动登陆</a>
服务器端LoginServlet.java文件
java
package servlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.ServletException;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
@WebServlet("/login")
public class LoginServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setHeader("Access-Control-Allow-Origin", "*");
boolean isLogin = false;
//获取从客户端发送过来的Cookie(获取的是一个Cookie数组)
Cookie[] cookieArr = request.getCookies();
if(cookieArr!=null) {
//遍历每一个cookie的名称(使用getName()方法)
for(Cookie cookie : cookieArr) {
if(cookie.getName().equals("user")) {
System.out.println("自动登陆成功");
isLogin = true;
}
}
}
if(!isLogin) {
String userName = request.getParameter("userName");
String password = request.getParameter("password");
if(userName.equals("test")&&password.equals("111")) {
System.out.println("登陆成功");
//创建用户信息Cookie
Cookie userCookie = new Cookie("user","ok");
//设置Cookie过期时间为一周
userCookie.setMaxAge(24*60*60);
//向客户端发生Cookie
response.addCookie(userCookie);
}else {
System.out.println("登陆失败");
}
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}



cookie的缺点


1.4 session
Cookie虽然在持久保存(可以设置过期时间)客户端数据提供了方便,但在一个会话范围内维持状态却并无优势。所以,在基于Cookie的基础上,出现了Session技术。
Session较为典型的应用就是:当第一次登陆成功后,一个会话开始。在这个会话范围内,可以依靠Session维护登陆状态,直到此会话结束。
1.4.1 session原理
使用Session维护一个会话的登陆状态:
- 当第一次请求时,在服务器端创建一个Session对象,并且为此对象生成一个sessionId。
- 同时,使用Cookie将此sessionId返回给客户端,并存储在客户端的Cookie中。
- 当客户端发起下一次请求时,必须携带此sessionId发送给服务器端。
- 服务器端根据接收的sessionId,就能找回Session对象,从而获取了上一次请求的信息。
1.4.2 Session实例

填个坑:等看完黑马javaweb再来补充。
四、过滤器和监听器。
在学习过滤器时,比较困惑它是怎样按过滤链进行传递,机器人给了我答案。


执行的顺序的怎样的?是执行完本函数?
