JavaWeb详解
JavaWeb从入门到实战
Maven
maven阿里云镜像
<mirror>
<id>nexus-aliyun</id>
<mirrorOf>*,!jeecg,!jeecg-snapshots</mirrorOf>
<name>Nexus aliyun</name>
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>
Servlet
Servlet简介
- Servlet就是sun公司开发动态web的一门技术
- sun在这些API中提供一个接口叫做:Servlet,如果你想开发一个Servlet程序,只需要完成两个小步骤:
- 编写一个类,实现Servlet接口
- 把开发好的Java类部署到web服务器中
把实现了Servlet接口的Java程序叫做Servlet
HelloServlet
package org.example.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//设置响应类型
resp.setContentType("text/html");
//设置响应编码格式
resp.setCharacterEncoding("utf-8");
PrintWriter out=resp.getWriter();
out.print("<html>");
out.print("<head>");
out.print("<title>Hello World</title>");
out.print("</head>");
out.print("<body>");
out.print("<h1>Hello World!</h1>");
out.print("<h1>你好,世界!</h1>");
out.print("</body>");
out.print("</html>");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//无论使用get还是post方式都会执行doget中的方法
doGet(req,resp);
}
}
编写Servlet的映射:
为什么需要映射:我们写的是Java程序,但是要通过浏览器访问,而浏览器需要连接web服务器,所以我们需要在web服务中注册我们写的Servlet,还需给他一个浏览器能够访问的路径;
<!--web.xml中是配置我么web的核心应用-->
<!--注册Servlet-->
<servlet>
<servlet-name>helloServlet</servlet-name>
<servlet-class>org.example.servlet.HelloServlet</servlet-class>
</servlet>
<!--一个Servlet对应一个Mapping:映射-->
<!--Servlet的请求路径-->
<servlet-mapping>
<servlet-name>helloServlet</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
Servlet原理
Mapping问题
1、一个Servlet可以指定一个映射路径(最常用)
<servlet-mapping>
<servlet-name>helloServlet</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
2、一个Servlet可以指定多个映射路径
<servlet-mapping>
<servlet-name>helloServlet</servlet-name>
<url-pattern>/hello1</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>helloServlet</servlet-name>
<url-pattern>/hello2</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>helloServlet</servlet-name>
<url-pattern>/hello3</url-pattern>
</servlet-mapping>
3、一个Servlet可以指定通用映射路径
<servlet-mapping>
<servlet-name>helloServlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
4、可以指定前缀或者后缀的映射路径
<!--可以自定义后缀实现请求映射
注意点:*前面不能加项目映射的路径(/hello/*.test)-->
<servlet-mapping>
<servlet-name>helloServlet</servlet-name>
<url-pattern>*.test</url-pattern>
</servlet-mapping>
ServletContext
web容器在启动的时候,它会为每个web程序都创建一个对应的ServletContext对象,它代表了当前的web应用;
共享数据
在一个servlet中保存,可以在另一个servlet中获取
package org.example.servlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
//在servlet中保存
public class ServletContextDemo1 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext servletContext = this.getServletContext();
String username="张三";
//将一个数据保存在ServletContext中,名字为username,值为username对象
servletContext.setAttribute("username",username);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req,resp);
}
}
package org.example.servlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
//获取ServletContextDemo存入ServletContext中的值
public class ServletContextDemo2 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext servletContext = this.getServletContext();
String username = (String) servletContext.getAttribute("username");
resp.setContentType("text/html");//必须设置响应类型,否则响应结果还是中文乱码
resp.setCharacterEncoding("utf-8");
resp.getWriter().print(username);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req,resp);
}
}
获取初始化参数
在web.xml中配置初始化参数
<!--初始化参数-->
<context-param>
<param-name>url</param-name>
<param-value>jdbc:mysql://localhost:3306/test</param-value>
</context-param>
获取初始化参数
package org.example.servlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class InitParameterDemo extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext servletContext = this.getServletContext();
String url = servletContext.getInitParameter("url");
resp.getWriter().print(url);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req,resp);
}
}
请求转发
package org.example.servlet;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class RequestDispatcherDemo extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletContext servletContext = this.getServletContext();
RequestDispatcher requestDispatcher = servletContext.getRequestDispatcher("/initParameter");
requestDispatcher.forward(req,resp);//调用forward实现请求转发
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req,resp);
}
}
读取资源文件
db.properties
driverClass=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/mytest
username=root
password=123456
package org.example.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Properties;
public class ResourceAsStreamDemo extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
InputStream resourceAsStream = this.getServletContext().getResourceAsStream("/WEB-INF/classes/db.properties");
Properties properties = new Properties();
properties.load(resourceAsStream);
String driverClass = (String) properties.get("driverClass");
String url = (String) properties.get("url");
String username = (String) properties.get("username");
String password = (String) properties.get("password");
PrintWriter out = resp.getWriter();
out.println(driverClass);
out.println(url);
out.println(username);
out.println(password);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req,resp);
}
}
HttpServletResponse
web服务器接收到客户端的http请求,针对这个请求,分别创建一个代表请求的HttpServletRequest对象,代表响应的一个HttpServletResponse;
- 如果要获取客户端请求过来的参数:找HttpServletRequest
- 如果要给客户端响应一些信息:找HttpServletResponse
分类:
负责向浏览器发送数据的方法
ServletOutputStream getOutputStream() throws IOException;
PrintWriter getWriter() throws IOException;
下载文件:
package org.example.servlet;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URLEncoder;
public class FileServletDemo extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//要获取下载文件的路径
String realPath = this.getServletContext().getRealPath("/WEB-INF/classes/风景.webp");
System.out.println("下载文件的路径: "+realPath);
//获取下载的文件名
String fileName = realPath.substring(realPath.lastIndexOf('\\') + 1);
//设置想办法让浏览器能够支持(Content-Disposition)下载我们需要的东西
resp.setHeader("Content-Disposition","attachment;filename="+URLEncoder.encode(fileName,"utf-8"));
//获取下载文件的输入流
FileInputStream fileInputStream = new FileInputStream(realPath);
//创建缓冲区
int len=0;
byte[] buffer=new byte[1024];
//获取OutputStream对象
ServletOutputStream outputStream = resp.getOutputStream();
//将OutputStream写入到buffer缓冲区
while((len=fileInputStream.read(buffer))>0){
outputStream.write(buffer,0,len);
}
outputStream.close();
fileInputStream.close();
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req,resp);
}
}
验证码
package org.example.servlet;
import com.sun.javafx.iio.ImageStorage;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
public class ImageServletDemo extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//如何让浏览器5秒自动刷新一次
resp.setHeader("refresh","3");
//在内存中创建一个图片
BufferedImage bufferedImage = new BufferedImage(120, 80, BufferedImage.TYPE_INT_RGB);
//获得画笔对象
Graphics2D graphics = (Graphics2D) bufferedImage.getGraphics();
//设置图片的背景颜色
graphics.setColor(Color.black);
graphics.fillRect(0,0,120,80);
//给图片写数据
graphics.setColor(Color.white);
graphics.setFont(new Font("",Font.BOLD,20));
for (int i = 0; i < 4; i++) {
graphics.drawString(getRandomData(),20*i+20,50);
}
//告诉浏览器,这个请求以图片的形式打开
resp.setContentType("image/jpeg");
//网站存在缓存,不让浏览器缓存
resp.setDateHeader("expires",-1);
resp.setHeader("Cache-Control","no-cache");
resp.setHeader("Pragma","no-cache");
//把图片写给浏览器
ImageIO.write(bufferedImage,"jpg",resp.getOutputStream());
}
private String getRandomData(){
String data="1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
int rand=new Random().nextInt(data.length());
return String.valueOf(data.charAt(rand));
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req,resp);
}
}
重定向
package org.example.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class RedirectServletDemo extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
/*
* resp.setHeader("Location","/start/authCode");
* resp.setStatus(302);*/
resp.sendRedirect("/start/authCode");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req,resp);
}
}
面试题:请你聊聊重定向和转发的区别?
相同点
- 页面都会实现跳转
不同点
- 请求转发的时候,url不会发生变化
- 重定向的时候,url地址栏会发生变化
HttpServletRequest
获取参数以及请求转发
package org.example.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
public class RegisterServletDemo extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String username = req.getParameter("username");
String password = req.getParameter("password");
String gender = req.getParameter("gender");
String[] hobbies = req.getParameterValues("hobby");
System.out.println(username);
System.out.println(password);
System.out.println(gender);
System.out.println(Arrays.toString(hobbies));
//这里的 / 代表当前的wen应用
req.getRequestDispatcher("success.jsp").forward(req,resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req,resp);
}
}
Cookie和Session
保存会话的两种技术
Cookie
- 客户端技术(响应,请求)
Session
- 服务器技术,利用这个技术,可以保存用户的会话信息,我们可以把信息或者数据放在Session中!
Cookie
- 从请求中拿到cookie的信息
- 服务器响应给客户端cookie
Cookie[] cookies=req.getCookies();//获得Cookie
String name=cookie.getName();//获得Cookie中的key
String value=cookie.getValue();//获得Cookie中的value
Cookie cookie=new Cookie(String key, String value);//新建一个Cookie
cookie.setMaxAge(24*60*60);//设置Cookie的有效期为24小时
resp.addCookie(cookie);//将cookie响应给客户端
Cookie:一般会保存在本地的用户目录 appdata下
Cookie细节
- 一个Cookie只能保存一个信息
- 一个web站点可以给浏览器发送多个cookie,最多存放20个cookie
- Cookie大小有限制4kb
- 300个cookie浏览器上限
删除Cookie
- 不设置有效期,关闭浏览器,自动失效
- 设置有效期为0
字符串在网络中的编码和解码
URLEncoder.encode("张三","utf-8");//使用utf-8编码
URLDecoder.decode(cookie.getValue(),"utf-8");//使用utf-8解码
Session(重点)
什么是Session
- 服务器会给每一个用户(浏览器)创建一个Session对象
- 一个Session独占一个浏览器,只要浏览器没有关闭,这个Session就存在
- 用户登录之后,整个都可以访问!--> 保存用户信息;保存购物车信息等等。
Session和Cookie的区别
- Cookie是把用户的数据写给用户的浏览器,浏览器保存(可以保存多个)
- Session把用户的数据写到用户独占的Session中,服务器端保存(保存重要的信息,减少服务器资源的浪费)
- Session对象由服务器创建
Session存数据
package org.example.servlet;
import org.example.domain.Person;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import java.io.IOException;
public class SessionDemo1 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//解决乱码问题
req.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf-8");
resp.setCharacterEncoding("utf-8");
//得到session
HttpSession session = req.getSession();
//给session中存东西
//session.setAttribute("name","张三");
Person person = new Person("张三", 21);
session.setAttribute("person",person);
//获取session的id
String id = session.getId();
//判断session是不是新创建的
if(session.isNew()){
resp.getWriter().write("session创建成功,ID: "+id);
}else{
resp.getWriter().write("session已经存在,ID: "+id);
}
//Session创建的时候做了什么事情
/*Cookie cookie = new Cookie("JSESSIONID", id);
resp.addCookie(cookie);*/
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req,resp);
}
}
Session获取数据
package org.example.servlet;
import org.example.domain.Person;
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 SessionDemo2 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();
//String name = (String) session.getAttribute("name");
Person person = (Person) session.getAttribute("person");
resp.getWriter().print(person);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req,resp);
}
}
注销Session
package org.example.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 SessionDemo3 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
HttpSession session = req.getSession();
session.removeAttribute("person");
//手动注销Session
session.invalidate();
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req,resp);
}
}
Session自动过期:web.xml配置
<!--设置Session的默认失效时间-->
<session-config>
<!--1分钟后session自动失效,以分钟为单位-->
<session-timeout>1</session-timeout>
</session-config>
JSP
什么是JSP
Java Server Pages:Java服务器端页面,也和Servlet一样,用户动态Web技术
最大的特点:
- 写JSP就像在写HTML
- 区别
- HTML只给用户提供静态的数据
- JSP页面中可以嵌入Java代码,为用户提供动态数据
JSP基础语法
JSP表达式
<%--
JSP表达式
作用:用来将程序的输出,输出到客户端
<%= 变量或表达式 %>
--%>
<%=new java.util.Date()%>
JSP脚本片段
<%--jsp脚本片段--%>
<%--脚本片段可以嵌套--%>
<%
int sum=0;
for (int i = 1; i <= 100; i++) {
sum+=i;
}
out.print("<h1>sum="+sum+"</h1>");
%>
JSP声明
<%!
static{
System.out.println("Loading Servlet!");
}
private int globalVar=0;
public void test(){
System.out.println("进入了test方法");
}
%>
JSP声明:会被编译到JSP生成的Java类中!其他的,就会被生成到_jspService方法中
//将<%%>的内容转换为Servlet的服务方法。
<%%>
//< %!%>的内容被直接翻译到Servlet类中。
<%!%>
//jsp注释
<%--注释--%>
//html注释
<!--注释-->
自定义错误页面
方式一
<%@ page errorPage="error/500.jsp" %>//指定发生错误后跳转的页面
<%@ page isErrorPage="true" %>//指定当前页面是错误页面
方式二
//web.xml配置
<error-page>
<error-code>500</error-code>
<location>/error/500.jsp</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/error/404.jsp</location>
</error-page>
include
include指令常用来拼接页面
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>body</title>
</head>
<body>
<%--jsp标签--%>
<jsp:include page="common/header.jsp"></jsp:include>
<%--<%@ include file="common/header.jsp"%>--%>
<h1>我是body</h1>
<%--<%@ include file="common/footer.jsp"%>--%>
<jsp:include page="common/footer.jsp"></jsp:include>
</body>
</html>
注意
如果include进来的页面和本页面中存在相同的变量,会报500错误,所以include的本质就是拼接页面
9大内置对象
- PageContext
- Request
- Response
- Session
- Application
- config
- out
- page(不用了解)
- exception
举例
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
<title>pageContextDemo1</title>
</head>
<body>
<%--内置对象--%>
<%
pageContext.setAttribute("name1","张三");//保存的数据只在一个页面中有效
request.setAttribute("name2","李四");//保存的数据只在一次请求中有效,请求转发会携带这个数据
session.setAttribute("name3","王五");//保存的数据只在一次会话中有效,从打开浏览器到关闭浏览器
application.setAttribute("name4","赵六");//保存的数据只在服务器中有效,从打开服务器到关闭服务器
%>
<%
/*从pageContext获取*/
String name1 = (String) pageContext.findAttribute("name1");
String name2 = (String) pageContext.findAttribute("name2");
String name3 = (String) pageContext.findAttribute("name3");
String name4 = (String) pageContext.findAttribute("name4");
String name5 = (String) pageContext.findAttribute("name5");
%>
<%--使用el表达式输出--%>
<h3>${name1}</h3><%--可以找到--%>
<h3>${name2}</h3><%--可以找到--%>
<h3>${name3}</h3><%--可以找到--%>
<h3>${name4}</h3><%--可以找到--%>
<h3>${name5}</h3><%--不可以找到--%>
</body>
</html>
新建一个页面获取上一个页面存放的值
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<html>
<head>
<title>pageContextDemo2</title>
</head>
<body>
<%--内置对象--%>
<%
/*从pageContext获取*/
String name1 = (String) pageContext.findAttribute("name1");//获取不到
String name2 = (String) pageContext.findAttribute("name2");//获取不到
String name3 = (String) pageContext.findAttribute("name3");
String name4 = (String) pageContext.findAttribute("name4");
String name5 = (String) pageContext.findAttribute("name5");//获取不到
%>
<%--使用el表达式输出--%>
<h3>${name1}</h3>
<h3>${name2}</h3>
<h3>${name3}</h3>
<h3>${name4}</h3>
<h3>${name5}</h3>
</body>
</html>
request:客户端向服务器发送请求,产生的数据用户看完就没用了,比如:新闻,用户看完就没用了!
session:客户端向服务器发送请求,产生数据,用户用完一会还有用,比如:购物车
application:客户端向服务器发送请求,产生的数据,一个用户用完了,其他用户还可能使用,比如:聊天数据
JSP标签、JSTL标签、EL表达式
EL表达式
- 获取数据
- 执行运算
- 获取web开发的常用对象
JSP标签
请求转发
<%@ page contentType="text/html;charset=UTF-8" pageEncoding="utf-8" language="java" %>
<html>
<head>
<title>jspTag1</title>
</head>
<body>
<h1>jspTag1</h1>
<%--请求转发并携带转发参数--%>
<jsp:forward page="jspTag2.jsp">
<jsp:param name="name" value="张三"/>
</jsp:forward>
</body>
</html>
接收请求转发数据
<%@ page contentType="text/html;charset=UTF-8" pageEncoding="utf-8" language="java" %>
<html>
<head>
<title>jspTag2</title>
</head>
<body>
<h1>jspTag2</h1>
<%--取出参数--%>
<%=request.getParameter("name")%>
</body>
</html>
JSTL标签
JSTL标签库使用步骤
- 引入对应的taglib
- 使用其中的方法
- 在Tomcat也需要引入jstl的包,否则会报错:JSTL解析错误
<c:if>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%--引入jstl核心标签库,我们才能使用jstl标签--%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>if</title>
</head>
<body>
<h3>if测试</h3>
<form action="jstlCore1.jsp" method="get">
<%--el表达式获取表单中的数据:${param.参数名}--%>
<input type="text" name="username" value="${param.username}"><br/>
<input type="submit" value="登录">
</form>
<%--判断如果提交的用户名是admin,则登录成功--%>
<c:if test="${param.username=='admin'}" var="isAdmin">
<c:out value="管理员欢迎您"/>
</c:if>
<c:out value="${isAdmin}"/>
</body>
</html>
<c:when>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%--引入jstl核心标签库,我们才能使用jstl标签--%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>when</title>
</head>
<body>
<%--定义一个变量score,值为85--%>
<c:set var="score" value="85"/>
<c:choose>
<c:when test="${score>=90}">
<c:out value="你的成绩为优秀"/>
</c:when>
<c:when test="${score>=80}">
<c:out value="你的成绩为良好"/>
</c:when>
<c:when test="${score>=70}">
<c:out value="你的成绩为中等"/>
</c:when>
<c:when test="${score>=60}">
<c:out value="你的成绩为及格"/>
</c:when>
<c:otherwise>
<c:out value="你的成绩不及格"/>
</c:otherwise>
</c:choose>
</body>
</html>
<c:foreach>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>
<%--引入jstl核心标签库,我们才能使用jstl标签--%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>foreach</title>
</head>
<body>
<%
ArrayList<String> list=new ArrayList<>();
list.add("张三");
list.add("李四");
list.add("王五");
list.add("赵六");
list.add("孙七");
request.setAttribute("list",list);
%>
<%--
var 每一次遍历出来的变量
item 要遍历的对象
begin 开始遍历的下标
end 遍历结束的下标
step 步长
--%>
<c:forEach items="${list}" var="person">
<c:out value="${person}"/><br/>
</c:forEach>
</body>
</html>
JavaBean
实体类
JavaBean有特定的写法:
- 必须要有一个无参构造
- 属性必须私有化
- 必须有对应的get/set方法
一般用来和数据库的字段做映射ORM
ORM:对象关系映射
- 表--->类
- 字段--->属性
- 行记录--->对象
Person表
id | name | age | address |
---|---|---|---|
1 | 张三 | 21 | 北京 |
2 | 李四 | 22 | 上海 |
3 | 王五 | 23 | 广州 |
Person类
class Person{
private int id;
private String name;
private int age;
private String address;
public void setId(int id){
this.id=id;
}
public int getId(){
return this.id;
}
public void setName(String name){
this.name=name;
}
public String getName(){
return this.name;
}
public void setAge(int age){
this.age=age;
}
public int getAge(){
return this.age;
}
public void setAddress(String address){
this.address=address;
}
public String getAddress(){
return this.address;
}
}
MVC三层架构
MVC
- model
- 业务处理:业务逻辑(Service)
- 数据持久层:CRUD(Dao)
- view
- 展示数据
- 提供链接发起Srevlet请求(a,form,img...)
- controller
- 接收用户的请求:(req:请求参数、Session信息......)
- 交给业务层处理对应的代码
- 控制视图的跳转
过滤器Filter(重点)
Filter:过滤器,用来过滤网站的数据
- 处理中文乱码
- 登录验证......
解决中文乱码过滤器
package org.example.filter;
import javax.servlet.*;
import java.io.IOException;
public class CharacterEncodingFilter implements Filter {
/**
* 初始化:web服务器启动时就初始化了,等待过滤对象的出现
* @param filterConfig
* @throws ServletException
*/
@Override
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("CharacterEncodingFilter初始化了");
}
@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("CharacterEncodingFilter执行前");
//让我们的请求继续走,如果不写,程序到这里就被拦截停止了
filterChain.doFilter(servletRequest,servletResponse);
System.out.println("CharacterEncodingFilter执行后");
}
/**
* 销毁
*/
@Override
public void destroy() {
System.out.println("CharacterEncodingFilter销毁了");
}
}
web.xml配置
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>org.example.filter.CharacterEncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<!--只要是 /servlet的任何请求,都会经过这个过滤器-->
<url-pattern>/servlet/*</url-pattern>
</filter-mapping>
监听器Listener
监听在线人数
package org.example.listener;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
public class OnlineCountListener implements HttpSessionListener {
/**
* 创建Session监听:监听你的一举一动
* 一旦创建Session就会触发一次这个事件
* @param se
*/
@Override
public void sessionCreated(HttpSessionEvent se) {
ServletContext servletContext = se.getSession().getServletContext();
Integer onlineCount = (Integer) servletContext.getAttribute("OnlineCount");
if(onlineCount==null){
onlineCount=new Integer(1);
}
else{
int count=onlineCount.intValue();
onlineCount=new Integer(count++);
}
servletContext.setAttribute("OnlineCount",onlineCount);
}
/**
* 销毁session监听
* 一旦销毁session就会触发一次这个事件
* @param se
*/
@Override
public void sessionDestroyed(HttpSessionEvent se) {
ServletContext servletContext = se.getSession().getServletContext();
Integer onlineCount = (Integer) servletContext.getAttribute("OnlineCount");
if(onlineCount==null){
onlineCount=new Integer(0);
}
else{
int count=onlineCount.intValue();
onlineCount=new Integer(count--);
}
servletContext.setAttribute("OnlineCount",onlineCount);
}
}
web.xml配置
<!--注册监听器-->
<listener>
<listener-class>org.example.listener.OnlineCountListener</listener-class>
</listener>
案例
用户登录之后才能进入主页!用户注销后就不能进入主页了!
JDBC
连接mysql数据库
package org.example.test;
import java.sql.*;
public class TestJdbc {
public static void main(String[] args) {
String driver="com.mysql.cj.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8";
String username="root";
String password="123456";
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try {
Class.forName(driver);
connection = DriverManager.getConnection(url, username, password);
statement = connection.createStatement();
String sql="select * from users;";
resultSet = statement.executeQuery(sql);
while(resultSet.next()){
Object id = resultSet.getObject(1);
Object name = resultSet.getObject(2);
Object pwd = resultSet.getObject(3);
Object email = resultSet.getObject(4);
Object birthday= resultSet.getObject(5);
System.out.println(id);
System.out.println(name);
System.out.println(pwd);
System.out.println(email);
System.out.println(birthday);
System.out.println("=====================");
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}finally {
try {
resultSet.close();
statement.close();
connection.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
JDBC固定步骤
-
加载驱动
-
Class.forName("com.mysql.cj.jdbc.Driver");//mysql8 //mysql5是com.mysql.jdbc.Driver
-
-
连接数据库,获得Connection对象
-
connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8", "root", "123456");//mysql8需要加上时区
-
-
获取执行sql的Statement对象
-
statement = connection.createStatement();//不能避免sql注入 PreparedStatement preparedStatement = connection.prepareStatement(String sql);//可以避免sql注入
-
-
编写sql语句
-
执行sql
-
resultSet = statement.executeQuery(sql);
-
-
关闭连接
-
try { resultSet.close(); statement.close(); connection.close(); } catch (SQLException e) { e.printStackTrace(); }
-
-------------------------------------------
个性签名:独学而无友,则孤陋而寡闻。做一个灵魂有趣的人!
如果觉得这篇文章对你有小小的帮助的话,记得在右下角点个“推荐”哦,博主在此感谢!
万水千山总是情,打赏一分行不行,所以如果你心情还比较高兴,也是可以扫码打赏博主,哈哈哈(っ•̀ω•́)っ✎⁾⁾!