|
@@ -293,6 +293,7 @@ resp.sendRedirect("/r/image");
|
|
|
```
|
|
|
重定向和转发区别?
|
|
|
相同点:
|
|
|
+
|
|
|
* 界面都会实现改变
|
|
|
不同点:
|
|
|
* 一个是web服务器
|
|
@@ -372,6 +373,17 @@ session使用场景:
|
|
|
* 购物车信息:
|
|
|
* 整个网站中经常会使用的数据
|
|
|
|
|
|
+什么时候会被销毁?
|
|
|
+话题:当浏览器关闭后,Session就销毁了吗?
|
|
|
+答案:存在于浏览器上的唯一标识符JSESSIONID(sessionid)消失了,但是服务器中存放的sessionid并没有立马销毁。
|
|
|
+分析:我们知道Session是JSP的九大内置对象(也叫隐含对象)中的一个,它的作用是可以保存当前用户的状态信息,初学它的时候,认为Session的生命周期是从打开一个浏览器窗口发送请求到关闭浏览器窗口,但其实这种说法是不正确的!当一个Session开始时,Servlet容器会创建一个HttpSession对象,那么在HttpSession对象中,可以存放用户状态的信息,Servlet容器为HttpSession对象分配一个唯一标识符即Sessionid,Servlet容器把Sessionid作为一种Cookie保存在客户端的浏览器 中用户每次发出Http请求时,Servlet容器会从HttpServletRequest对象中取出Sessionid,然后根据这个Sessionid找到相应的HttpSession对象,从而获取用户的状态信息。
|
|
|
+
|
|
|
+其实让Session结束生命周期,有以下两种办法:
|
|
|
+1. 一个是Session.invalidate()方法,不过这个方法在实际的开发中,并不推荐,可能在强制注销用户的时候会使用;
|
|
|
+2. 一个是当前用户和服务器的交互时间超过默认时间后,Session会失效。
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
## 6、JSP
|
|
|
|
|
|
***
|
|
@@ -659,7 +671,7 @@ Controller(Servlet)
|
|
|
登录----》接受用户的请求---》处理用户的请求(获取参数:username,password)----》交给业务层处理登录业务(判断用户名密码是否正确:事务)-----》Dao层查询用户名和密码是否正确----》数据库
|
|
|
```
|
|
|
|
|
|
-## 9、Filter
|
|
|
+## 9、Filter(重点)
|
|
|
|
|
|
***
|
|
|
|
|
@@ -669,7 +681,177 @@ Filter:过滤器,用来过滤网站的数据;
|
|
|

|
|
|
|
|
|
Filter开发步骤:
|
|
|
-1.
|
|
|
+1. 导包
|
|
|
+
|
|
|
+2. 编写过滤器
|
|
|
+```java
|
|
|
+public class CharacterEncodingFilter implements Filter {
|
|
|
+ // 初始化:web服务器启动就已经初始化了,随时等待监听
|
|
|
+ @Override
|
|
|
+ public void init(FilterConfig filterConfig) throws ServletException {
|
|
|
+ System.out.println("init CharacterEncodingFilter");
|
|
|
+ }
|
|
|
|
|
|
+ // chain:链
|
|
|
|
|
|
+ /**
|
|
|
+ * 1.过滤器中的所有代码,在过滤特定请求的时候都会执行
|
|
|
+ * 2.必须要让过滤器继续通行
|
|
|
+ */
|
|
|
+ @Override
|
|
|
+ public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
|
|
|
+ request.setCharacterEncoding("utf-8");
|
|
|
+ response.setCharacterEncoding("utf-8");
|
|
|
+ response.setContentType("text/html; charset=UTF-8");
|
|
|
+
|
|
|
+ System.out.println("CharacterEncodingFilter执行前");
|
|
|
+ // 让我们的请求继续走,如果不写,程序在这里就被拦截
|
|
|
+ chain.doFilter(request,response);
|
|
|
+ System.out.println("CharacterEncodingFilter执行后");
|
|
|
+ }
|
|
|
|
|
|
+ // 销毁:web服务器关闭的时候,过滤器会销毁
|
|
|
+ @Override
|
|
|
+ public void destroy() {
|
|
|
+ System.out.println("destroy CharacterEncodingFilter");
|
|
|
+ }
|
|
|
+}
|
|
|
+```
|
|
|
+3. 在web.xml中配置
|
|
|
+```xml
|
|
|
+ <filter>
|
|
|
+ <filter-name>CharacterEncodingFilter</filter-name>
|
|
|
+ <filter-class>com.filter.CharacterEncodingFilter</filter-class>
|
|
|
+ </filter>
|
|
|
+ <filter-mapping>
|
|
|
+ <filter-name>CharacterEncodingFilter</filter-name>
|
|
|
+ <!--只要是/servlet的任何请求,都会经过这个过滤器-->
|
|
|
+ <url-pattern>/servlet/*</url-pattern>
|
|
|
+ </filter-mapping>
|
|
|
+```
|
|
|
+
|
|
|
+## 10、监听器
|
|
|
+
|
|
|
+***
|
|
|
+
|
|
|
+实现一个监听器的接口;(有n种)
|
|
|
+1. 重写方法
|
|
|
+```java
|
|
|
+public class OnlineCountListener implements HttpSessionListener {
|
|
|
+ // 创建session监听,一旦创建一个session就会触发这个事件
|
|
|
+ @Override
|
|
|
+ public void sessionCreated(HttpSessionEvent se) {
|
|
|
+ ServletContext context = se.getSession().getServletContext();
|
|
|
+ Integer onlineCount = (Integer) context.getAttribute("OnlineCount");
|
|
|
+ if (onlineCount == null) {
|
|
|
+ onlineCount = new Integer(1);
|
|
|
+ } else {
|
|
|
+ int value = onlineCount.intValue();
|
|
|
+ onlineCount = new Integer(value + 1);
|
|
|
+ }
|
|
|
+ context.setAttribute("OnlineCount", onlineCount);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 销毁session监听,一旦销毁一个session就会触发这个事件
|
|
|
+ @Override
|
|
|
+ public void sessionDestroyed(HttpSessionEvent se) {
|
|
|
+ ServletContext context = se.getSession().getServletContext();
|
|
|
+ Integer onlineCount = (Integer) context.getAttribute("OnlineCount");
|
|
|
+ if (onlineCount == null) {
|
|
|
+ onlineCount = new Integer(0);
|
|
|
+ } else {
|
|
|
+ int value = onlineCount.intValue();
|
|
|
+ onlineCount = new Integer(value - 1);
|
|
|
+ }
|
|
|
+ context.setAttribute("OnlineCount", onlineCount);
|
|
|
+ }
|
|
|
+}
|
|
|
+```
|
|
|
+2. 配置web.xml
|
|
|
+```xml
|
|
|
+ <!--注册监听器-->
|
|
|
+ <listener>
|
|
|
+ <listener-class>com.listener.OnlineCountListener</listener-class>
|
|
|
+ </listener>
|
|
|
+```
|
|
|
+
|
|
|
+## 11、过滤器监听器常见应用
|
|
|
+
|
|
|
+***
|
|
|
+
|
|
|
+用户登陆之后才能进入主页!用户注销就不能进入主页!
|
|
|
+```java
|
|
|
+ @Override
|
|
|
+ public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
|
|
|
+ // 转换过来才能过去session
|
|
|
+ HttpServletRequest httpServletRequest = (HttpServletRequest) request;
|
|
|
+ HttpServletResponse httpServletResponse = (HttpServletResponse) response;
|
|
|
+
|
|
|
+
|
|
|
+ if (httpServletRequest.getSession().getAttribute("USER_SESSION") == null) {
|
|
|
+ httpServletResponse.sendRedirect("/f/error.jsp");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ chain.doFilter(request,response);
|
|
|
+ }
|
|
|
+```
|
|
|
+
|
|
|
+
|
|
|
+## 12、JDBC
|
|
|
+
|
|
|
+***
|
|
|
+
|
|
|
+什么是JDBC:Java连接数据库
|
|
|
+* 加载驱动
|
|
|
+* 连接数据库,代表数据库
|
|
|
+* 向数据库发送SQL的对象Statement : CRUD
|
|
|
+* 编写SQL (根据业务,不同的SQL)
|
|
|
+* 执行SQL
|
|
|
+* 关闭连接(先开的后关)
|
|
|
+
|
|
|
+
|
|
|
+1. 导入驱动
|
|
|
+```xml
|
|
|
+ <!--连接数据库-->
|
|
|
+ <dependency>
|
|
|
+ <groupId>mysql</groupId>
|
|
|
+ <artifactId>mysql-connector-java</artifactId>
|
|
|
+ <version>8.0.19</version>
|
|
|
+ </dependency>
|
|
|
+```
|
|
|
+2. java语句
|
|
|
+```java
|
|
|
+ String url = "jdbc:mysql://localhost:3306/jdbc?useUnicode=true&characterEncoding=utf-8";
|
|
|
+ String username = "root";
|
|
|
+ String password = "123456";
|
|
|
+
|
|
|
+
|
|
|
+ // 加载驱动
|
|
|
+ Class.forName("com.mysql.jdbc.Driver");
|
|
|
+ // 连接数据库,代表数据库
|
|
|
+ Connection connection = DriverManager.getConnection(url, username, password);
|
|
|
+ // 想数据库发送SQL的对象Statement:CRUD
|
|
|
+ Statement statement = connection.createStatement();
|
|
|
+ // 编写SQL
|
|
|
+ String sql = "select * from users";
|
|
|
+ // 执行SQL,返回结果集
|
|
|
+ ResultSet resultSet = statement.executeQuery(sql);
|
|
|
+
|
|
|
+ while (resultSet.next()) {
|
|
|
+ System.out.println("id" + resultSet.getObject("id"));
|
|
|
+ }
|
|
|
+ resultSet.close();
|
|
|
+ connection.close();
|
|
|
+ statement.close();
|
|
|
+```
|
|
|
+
|
|
|
+**事务**
|
|
|
+要么都成功,要么都失败!
|
|
|
+ACID原则:保证数据的安全
|
|
|
+```
|
|
|
+开启事务
|
|
|
+事务提交 commit
|
|
|
+事务回滚 rollback
|
|
|
+关闭事务
|
|
|
+```
|