Cookie
Cookie 客户端保存数据技术可以设置保存时长,保存的数据较少 效率较低 一般不建议使用
package com.huang.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
public class CookieDemo01 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//设置乱码
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
//设置输出
PrintWriter out = resp.getWriter();
//获取Cookie
Cookie[] cookies = req.getCookies();
out.write(" 您上一次的时间是:");
if (cookies != null){
for (int i = 0; i < cookies.length; i++) {
Cookie cookie = cookies[i];
if (cookie.getName().equals("lastLoginTime")){
long l = Long.parseLong(cookie.getValue());
Date date = new Date(l);
out.write(date.toLocaleString());
}
}
}else{
out.write("第一次访问本站");
}
//存入Cookie
Cookie cookie = new Cookie("lastLoginTime", System.currentTimeMillis() + "");
resp.addCookie(cookie);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
Session
package com.huang.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
public class SessionDemo01 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//解决乱码问题
req.setCharacterEncoding("utf-8");
resp.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf-8");
//获取session
HttpSession session = req.getSession();
session.setAttribute("name", "小明");
String sessionId = session.getId();
if (session.isNew()){
resp.getWriter().write("创建ID:" + sessionId);
}else{
resp.getWriter().write("存在的ID" + sessionId);
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
<!--设置Session默认的失效时间-->
<session-config>
<!--15分钟后Session自动失效,以分钟为单位-->
<session-timeout>15</session-timeout>
</session-config>
JSP
一个网页形式的servlet,可以直接编写java程序或编写前段页面程序,后台会生产index.java的文件
常用4个作用域
pageContext : 页面端存储数据,开用于保存当前页面使用 其他地方不使用的数据(小技巧:可通过第三个参数来扩展作用于)
requset:当前请求中使用的数据作为保存的参数
Session:当前打开浏览器不会丢失的数据 购物车经典中的经典
Application:保存在服务器中的数据,几乎等于永久保存
作用域不同使用的资源不同,作用域越大 资源占据越多
MVC三层架构
MVC: Model view Controller 模型、视图、控制器
Model
业务处理 :业务逻辑(Service)
数据持久层:CRUD (Dao)
View
展示数据
提供链接发起Servlet请求 (a,form,img…)
Controller (Servlet)
接收用户的请求 :(req:请求参数、Session信息….)
交给业务层处理对应的代码
控制视图的跳转
Filter(过滤器)
过滤器:将常用的加载或设置代码统一实现在一个函数中
package com.huang.Servlet;
import javax.servlet.*;
import java.io.IOException;
public class servletFilter implements Filter {
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("过滤器初始化");
}
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
servletRequest.setCharacterEncoding("utf-8");
servletResponse.setCharacterEncoding("utf-8");
servletResponse.setContentType("text/html;charset=utf-8");
System.out.println("过滤器执行前");
filterChain.doFilter(servletRequest,servletResponse);
System.out.println("过滤器执行后");
}
@Override
public void destroy() {
System.out.println("过滤器销毁中");
}
}
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>com.huang.Servlet.helloServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/servlet/hello</url-pattern>
</servlet-mapping>
<filter>
<filter-name>servlet</filter-name>
<filter-class>com.huang.Servlet.servletFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>servlet</filter-name>
<url-pattern>/servlet/*</url-pattern>
</filter-mapping>
Listener(监听器)
package com.huang.Servlet;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
public class OnlineCountListener implements HttpSessionListener {
@Override
public void sessionCreated(HttpSessionEvent httpSessionEvent) {
ServletContext ctx = httpSessionEvent.getSession().getServletContext();
System.out.println(httpSessionEvent.getSession().getId());
Integer onlineCount = (Integer) ctx.getAttribute("OnlineCount");
if (onlineCount==null){
onlineCount = new Integer(1);
}else {
int count = onlineCount.intValue();
onlineCount = new Integer(count+1);
}
ctx.setAttribute("OnlineCount",onlineCount);
}
@Override
public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
ServletContext ctx = httpSessionEvent.getSession().getServletContext();
Integer onlineCount = (Integer) ctx.getAttribute("OnlineCount");
if (onlineCount==null){
onlineCount = new Integer(0);
}else {
int count = onlineCount.intValue();
onlineCount = new Integer(count-1);
}
ctx.setAttribute("OnlineCount",onlineCount);
}
}
<listener>
<listener-class>com.huang.Servlet.OnlineCountListener</listener-class>
</listener>
onlineCount = new Integer(count-1);
}
ctx.setAttribute("OnlineCount",onlineCount);
}
}
```xml
<listener>
<listener-class>com.huang.Servlet.OnlineCountListener</listener-class>
</listener>