学习笔记——JSP入门知识点
一、学习重点
二、学习内容
案例一
查询学生(以表格形式)
遍历集合并显示在页面上,table表格展示
1.在JSP脚本片段中只能写java
2.在body中只能写html
<%@ page import="com.jsoft.entity.Student" %>
<%@ page import="java.util.List" %>
<%@ page pageEncoding="utf-8" contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
welcome:<%= request.getAttribute("username")%>
<%
List<Student> students = (List<Student>) request.getAttribute("students");
%>
<table border="1" cellpadding="10" cellspacing="0">
<tr>
<th>ID</th>
<th>NAME</th>
<th>AGE</th>
<th>GENDER</th>
</tr>
<%
for (Student student : students) {
%>
<tr>
<td><%= student.getId()%></td>
<td><%= student.getName()%></td>
<td><%= student.getAge()%></td>
<td><%= student.getGender()%></td>
</tr>
<%
}
%>
</table>
</body>
</html>
案例二
登录,如果登录成功,跳转到某个jsp页面,并且显示登录的用户名
欢迎你:xxx
admin
登录成功,欢迎你:admin
思路:输入账号和密码在servlet中比对,比对成功,把用户名放进作用域。
作用域对象的使用规则:
从小到大使用!!!------- request!!!
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
</body>
</html>
package com.jsoft2;
import com.jsoft.entity.Student;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
@WebServlet(name = "LoginJspServlet", value = "/loginjsp.do")
public class LoginJspServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String username = request.getParameter("username");
String password = request.getParameter("password");
PrintWriter out = response.getWriter();
List<Student> students = new ArrayList<>(5);
students.add(new Student(1001,"aaa",25,"Man"));
students.add(new Student(1002,"bbb",26,"Man"));
students.add(new Student(1003,"ccc",28,"Man"));
students.add(new Student(1004,"ddd",27,"Man"));
students.add(new Student(1005,"eee",24,"Man"));
if(Objects.equals(username,"admin") && Objects.equals(password,"123456")){
request.setAttribute("username",username);
request.setAttribute("students",students);
request.getRequestDispatcher("jsp/welcome.jsp").forward(request,response);
}else {
out.write("username or password wrong");
}
}
}
案例三
分页查询:
1、总记录数
2、总页数
3、每页显示的记录数
4、每页展示的数据集合
5、导航栏的页码 【1,2,3,4,5】
6、是否是第一页
7、是否是最后一页
8、当前页
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1><a href="${pageContext.request.contextPath}/page.do?pagenum=1">加载分页导航</a></h1>
当前页:【${requestScope.pagenum}】,总页数【${requestScope.pageCount}】
<hr>
<c:if test="${requestScope.pagenum ne 1}">
<a href="${pageContext.request.contextPath}/page.do?pagenum=1">首页</a>
<a href="${pageContext.request.contextPath}/page.do?pagenum=${requestScope.pagenum - 1}">上一页</a>
</c:if>
<c:if test="${requestScope.pagenum eq 1}">
<span>首页</span>
<span>上一页</span>
</c:if>
<c:forEach begin="1" end="${requestScope.pageCount}" var="i">
<c:if test="${requestScope.pagenum eq i}">
<span>${i}</span>
</c:if>
<c:if test="${requestScope.pagenum ne i}">
<a href="${pageContext.request.contextPath}/page.do?pagenum=${i}">${i}</a>
</c:if>
</c:forEach>
<c:if test="${requestScope.pagenum ne requestScope.pageCount}">
<a href="${pageContext.request.contextPath}/page.do?pagenum=${requestScope.pagenum + 1}">下一页</a>
<a href="${pageContext.request.contextPath}/page.do?pagenum=${requestScope.pageCount}">尾页</a>
</c:if>
</body>
</html>
package com.jsoft2;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.Objects;
@WebServlet(name = "PageServlet", value = "/page.do")
public class PageServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String pagenumStr = request.getParameter("pagenum");
Integer pagenum = 1;
Integer pageCount = 20;
try{
if(Objects.nonNull(pagenumStr)){
pagenum = Integer.parseInt(pagenumStr);
}
if(pagenum <= 0){
pagenum = 1;
}
if(pagenum > 20){
pagenum = 20;
}
} catch (Exception e){
pagenum = 1;
}
request.setAttribute("pagenum",pagenum);
request.setAttribute("pageCount",pageCount);
request.getRequestDispatcher("jsp/page.jsp").forward(request,response);
}
}
三、笔记内容
JSP
JSP脚本片段:用于在JSP页面写java代码
面试题:JSP和servlet的区别?
1、JSP本质上就是一个servlet
2、JSP更侧重于视图展示,servlet更侧重于逻辑处理
3、先有的servlet,后有的JSP
注意:
1、JSP脚本片段中只能出现java代码,不能出现HTML元素。在访问JSP时,JSP引擎翻译JSP页面中的脚本片段。
2、JSP脚本片段中的java代码必须严格遵守java的规则
3、一个JSP页面是可以有多个脚本片段
4、多个脚本片段中的代码可以相互访问
<%
int num = 0;
num++;
// System.out.println(num);
%>
<%
System.out.println(num);
// 向页面打印输出
out.print(num);
%>
JSP表达式
<%= num %>
JSP声明片段
<%!
int x = 10;
static{
}
public void fun(){
}
%>
JSP的指令标识
- <%@ 指令名 属性1="值1" 属性2="值2" .....%>
- page指令:定义整个JSP页面的相关属性
- include指令:引入其他的JSP页面。先把两个页面结合,在去编译成servlet。
- taglib指令:引入页面上需要用到的标签库
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page errorPage="error.jsp" contentType="text/html;charset=UTF-8" language="java" %>
<%@include file="hello.jsp"%>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
List list = new ArrayList();
// int i = 10 / 0;
%>
<h1>JSP02 Page!!!</h1>
</body>
</html>
<%@ page isErrorPage="true" contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>Error Page!!!</h1>
<%=
exception.getMessage()
%>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>hello Page!!!</h1>
</body>
</html>
JSP标签
1、内置标签
(1)jsp:include:引入指定的页面
(2)jsp:forward:转发页面
(3)jsp:param:传参数
2、JSTL标签,需要导入JSTL标签库
3、自定义标签
面试题:jsp:include标签和include指令的区别?
include标签:先把要引入的页面编译,再合并
include指令:先把要引入的页面合并,再编译
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>JSP03 Page!!!</h1>
<jsp:include page="hello.jsp"></jsp:include>
<jsp:forward page="hello.jsp">
<jsp:param name="num1" value="10"/>
<jsp:param name="num2" value="20"/>
</jsp:forward>
</body>
</html>
JSP四大作用域:
1、当前页(pageContext):一个属性只能在一个页面中获取。
2、一次请求(request):一个页面中设置的属性,范围是一次请求。
3、一次会话(session):一个会话中的属性,只要页面不关闭,都能获取到
4、整个web应用(application):在这个服务器上,当前项目下的任何一个位置都能获取。
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
pageContext.setAttribute("pageContext","pageContext");
request.setAttribute("request","request");
session.setAttribute("session","session");
application.setAttribute("application","application");
%>
<h1>pageContext:<%= pageContext.getAttribute("pageContext")%></h1>
<h1>request:<%= request.getAttribute("request")%></h1>
<h1>session:<%= session.getAttribute("session")%></h1>
<h1>application:<%= application.getAttribute("application")%></h1>
<jsp:forward page="jsp04_copy.jsp"></jsp:forward>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>jsp04_copy page</h1>
<h1>pageContext:<%= pageContext.getAttribute("pageContext")%></h1>
<h1>request:<%= request.getAttribute("request")%></h1>
<h1>session:<%= session.getAttribute("session")%></h1>
<h1>application:<%= application.getAttribute("application")%></h1>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>jsp05</h1>
<h1>pageContext:<%= pageContext.getAttribute("pageContext")%></h1>
<h1>request:<%= request.getAttribute("request")%></h1>
<h1>session:<%= session.getAttribute("session")%></h1>
<h1>application:<%= application.getAttribute("application")%></h1>
</body>
</html>
package com.jsoft.servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.PageContext;
import java.io.IOException;
@WebServlet("/jsp.do")
public class JSPServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
/*
pageContext--PageContext
request------HttpServletRequest
session------HttpSession
application--ServletContext
*/
System.out.println(req.getAttribute("request"));
System.out.println(req.getSession().getAttribute("session"));
System.out.println(req.getServletContext().getAttribute("application"));
}
}
面试题:JSP的九大内置对象。内置:不需要创建,直接就能用。
对象名 | 作用 |
---|---|
request | 请求 |
response | 响应 |
session* | 会话 |
out | 输出 |
page | 当前JSP页面对象 |
application | 应用 |
exception* | 异常,只能在指定了isErrorPage="true" |
pageContext | 当前页,作用域 |
config | 配置 |
<%@ page session="false" isErrorPage="true" contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
out.print("<h1>out对象</h1>");
%>
</body>
</html>
EL表达式
EL表达式的内置作用域对象
pageContext
requestScope
sessionScope
applicationScope
EL表达式的缺陷:1、只能读,不能写 2、不支持流程控制语句
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%
request.setAttribute("name","zhangsan");
session.setAttribute("name","lisi");
pageContext.setAttribute("age",30);
%>
<%-- <input type="text" value="<%= request.getAttribute("name1") == null ? "" : request.getAttribute("name1")%>">--%>
<input type="text" value="${sessionScope.name}">
<hr>
${age eq 1}
<hr>
取参数:${param.username},${paramValues.hobby},${initParam.password}
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%-- 向指定的作用域中设置值 --%>
<c:set scope="session" var="name" value="zhangsan"></c:set>
<c:set scope="session" var="age" value="20"></c:set>
${sessionScope.name}
<hr>
<c:if test="${sessionScope.age >= 18}">成年!</c:if>
<c:if test="${sessionScope.age < 18}">未成年!</c:if>
<hr>
<c:choose>
<c:when test="${sessionScope.age eq 18}">
你已经年满18岁,可以签署劳动合同了!
</c:when>
<c:when test="${sessionScope.age lt 18}">
你好没有满18岁!
</c:when>
<c:otherwise>
你已经是大人了!!!
</c:otherwise>
</c:choose>
<hr>
<c:forEach begin="1" end="10" step="2" var="i" varStatus="stat">
${i} ----- ${stat.first} <br>
</c:forEach>
</body>
</html>
WebInfServlet
package com.jsoft2;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
@WebServlet(name = "WebInfServlet", value = "/WebInfServlet")
public class WebInfServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// request.getRequestDispatcher("/WEB-INF/aaa.jsp").forward(request,response);
response.sendRedirect("WEB-INF/aaa.jsp");
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY