JavaWeb学习笔记(5)
JavaWeb自学(5)
1、JSP、JSTL标签
jsptag.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title
<%--
1
<%--http://localhost:8080/jsptag.jsp?name=caiwei&age=18--%>
jsptag2.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title
<%--
1
<%--http://localhost:8080/jsptag.jsp?name=caiwei&age=18--%>
coreif.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%--引入JSTL核心标签库,我们才能使用JSTL标签 core--%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
Title
if测试
<%--判断如果提交的用户名是管理员,则登录成功--%>
<%--自闭合标签--%>
coreone.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%--引入JSTL核心标签库,我们才能使用JSTL标签 core--%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
Title
if测试
<%--判断如果提交的用户名是管理员,则登录成功--%>
<%--自闭合标签--%>
coreforeach.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%--引入JSTL核心标签库,我们才能使用JSTL标签 core--%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
Title
if测试
<%--判断如果提交的用户名是管理员,则登录成功--%>
<%--自闭合标签--%>
2、JavaBean及作业
javabean.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
Title
<%--
People people=new People();
--%>
姓名:
id:
年龄:
地址:
People.java
package top.gujiakai.pojo;
/**
* Author: Gu Jiakai
* Date: 2021/10/6 9:00
* FileName: People
* Description:
*/
//实体类我们一般都是和数据库中的表结构一一对应!
public class People {
private int id;
private String name;
private int age;
private String address;
public People() {
}
public People(int id, String name, int age, String address) {
this.id = id;
this.name = name;
this.age = age;
this.address = address;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "People{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", address='" + address + '\'' +
'}';
}
}
3、MVC三层架构
4、过滤器Filter
CharacterEncodingFilter.java
package top.gujiakai.filter;
import javax.servlet.*;
import java.io.IOException;
/**
* Author: Gu Jiakai
* Date: 2021/10/9 14:50
* FileName: CharacterEncodingFilter
* Description:
*/
public class CharacterEncodingFilter implements Filter {
//初始化
//初始化: web服务器启动,就已经初始化了,随时等待过滤对象出现!|
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("CharacterEncodingFilter初始化");
}
//filterChain:链
/*
1.过滤中的所有代码,在过滤特定请求的时候都会执行
2.必须要让过滤器继续同行
chain.doFilter( request, response);
*/
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("CharacterEncodingFilter执行前...");
filterChain.doFilter(servletRequest, servletResponse);//让我们的请求继续走,如果不写,程序到这里就拦截停止。
System.out.println("CharacterEncodingFilter执行后...");
}
//销毁:web服务器关闭的时候,过滤器会销毁。
public void destroy() {
System.out.println("CharacterEncodingFilter销毁");
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
ShowServlet
top.gujiakai.servlet.ShowServlet
ShowServlet
/servlet/show
ShowServlet
/show
CharacterEncodingFilter
top.gujiakai.filter.CharacterEncodingFilter
CharacterEncodingFilter
/servlet/*
5、监听器
OnlineCountListener.java
package top.gujiakai.listener;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
/**
* Author: Gu Jiakai
* Date: 2021/10/10 14:35
* FileName: OnlineCountListener
* Description:
*/
//统计网站在线人数:统计session
public class OnlineCountListener implements HttpSessionListener {
//创建session监听,看你的一举一动。
// 一旦创建Session就会触发一次这个事件!
@Override
public void sessionCreated(HttpSessionEvent se) {
// HttpSessionListener.super.sessionCreated(se);
ServletContext ctx = se.getSession().getServletContext();
System.out.println(se.getSession().getId());
Integer onlineCount = (Integer) ctx.getAttribute("OnlineCount");
if(onlineCount==null){
onlineCount= Integer.valueOf(1);
}else{
int count= onlineCount;
onlineCount= Integer.valueOf(count + 1);
}
ctx.setAttribute("OnlineCount",onlineCount);
}
//销毁session监听
//一旦销毁Session就会触发一次这个事件!
@Override
public void sessionDestroyed(HttpSessionEvent se) {
// HttpSessionListener.super.sessionDestroyed(se);
ServletContext ctx = se.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);
}
/*
Session销毁:
1. 手动销毁 getSession().invalidate();
2. 自动销毁
*/
}
index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
$Title$
当前有<%=this.getServletConfig().getServletContext().getAttribute("OnlineCount")%>人在线
web.xml
<?xml version="1.0" encoding="UTF-8"?>
ShowServlet
top.gujiakai.servlet.ShowServlet
ShowServlet
/servlet/show
ShowServlet
/show
CharacterEncodingFilter
top.gujiakai.filter.CharacterEncodingFilter
CharacterEncodingFilter
/servlet/*
top.gujiakai.listener.OnlineCountListener
1