注意点:
如果内置对象的作用域下的键名都相同的情况下,el表达式优先获取的是作用域范围小的值
测试:
Created by IntelliJ IDEA.
To change this template use File | Settings | File Templates.
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
pageContext.setAttribute("pname","班长");
request.setAttribute("rname","梁男");
session.setAttribute("sname","黑福");
application.setAttribute("aname","肥男");
request.setAttribute("upwd",11);
session.setAttribute("upwd",22);
application.setAttribute("upwd",33);
request.getRequestDispatcher("info.jsp").forward(request,response);
Created by IntelliJ IDEA.
To change this template use File | Settings | File Templates.
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
${applicationScope.aname}
三、JSP动态标签库
简介:
JSP动态标签库,主要用于简化页面java代码的编写,所有的jsp页面都可以使用
转发:
"info.jsp">
动态引入公共页面:
"kk.jsp">
动态引入与静态引入的区别:
动态引入:
先翻译并编译两个jsp文件,再进行合并 效率低
静态引入:
先合并两个jsp文件,在翻译并编译合并后的jsp文件 效率高
访问路径的设置:
在实际项目一般使用的绝对路径 在jsp中设置绝对路径跳转到Servlet
解决访问路径错误的问题 :在访问路径前加入${pageContext.request.contextPath}
四、分页
需求:

基础搭建:
创建项目:

数据库:

功能实现:
Student实体类:
import java.io.Serializable;
public class Student implements Serializable {
public Student(int sid, String sname, String sex, String hobby) {
public void setSid(int sid) {
public String getSname() {
public void setSname(String sname) {
public void setSex(String sex) {
public String getHobby() {
public void setHobby(String hobby) {
public String toString() {
", sname='" + sname + '\'' +
", hobby='" + hobby + '\'' +
pageUtils类:
private Integer currentPageNo;
private Integer pageSize;
private Integer totalPageCount;
private Integer totalPageSize;
public Integer getCurrentPageNo() {
public void setCurrentPageNo(Integer currentPageNo) {
this.currentPageNo = currentPageNo;
public Integer getPageSize() {
public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
public Integer getTotalPageCount() {
public void setTotalPageCount(Integer totalPageCount) {
this.totalPageCount = totalPageCount;
public Integer getTotalPageSize() {
public void setTotalPageSize(Integer totalPageSize) {
this.totalPageSize = totalPageSize;
public List getCommenList() {
public void setCommenList(List commenList) {
this.commenList = commenList;
public String toString() {
"currentPageNo=" + currentPageNo +
", pageSize=" + pageSize +
", totalPageCount=" + totalPageCount +
", totalPageSize=" + totalPageSize +
", commenList=" + commenList +

DBUtils类:
import com.alibaba.druid.pool.DruidDataSourceFactory;
import org.apache.commons.dbutils.QueryRunner;
import javax.sql.DataSource;
import java.io.InputStream;
import java.util.Properties;
InputStream is = DBUtils.class.getResourceAsStream("db.properties");
Properties pro = new Properties();
DataSource ds = DruidDataSourceFactory.createDataSource(pro);
qr = new QueryRunner(ds);
StudentDao接口:
import com.qf.entity.Student;
public interface StudentDao {
Integer selectCount(String sname);
List selectByPage(Integer currentPageNo,Integer pageSize,String sname);
StudentDaoImpl类:
import com.qf.dao.StudentDao;
import com.qf.entity.Student;
import com.qf.utils.DBUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import java.sql.SQLException;
import java.util.ArrayList;
public class StudentDaoImpl implements StudentDao {
private QueryRunner qr = DBUtils.DB.qr;
public Integer selectCount(String sname) {
StringBuffer sb = new StringBuffer("select count(1) from student where 1=1");
List
if (sname!=null&&!"".equals(sname)){
sb.append(" and sname like ?");
parmList.add("%"+sname+"%");
if (parmList!=null&&parmList.size()>0){
count = (Long) qr.query(sb.toString(), new ScalarHandler(),parmList.toArray());
count = (Long) qr.query(sb.toString(), new ScalarHandler());
} catch (SQLException e) {
return count==null?0:Integer.parseInt(count+"");
public List selectByPage(Integer currentPageNo, Integer pageSize,String sname) {
StringBuffer sb = new StringBuffer("select * from student where 1=1");
List
if (sname!=null&&!"".equals(sname)){
sb.append(" and sname like ?");
parmList.add("%"+sname+"%");
parmList.add((currentPageNo-1)*pageSize);
studentList = qr.query(sb.toString(), new BeanListHandler(Student.class), parmList.toArray());
} catch (SQLException e) {

StudentService接口:
import com.qf.entity.Student;
import com.qf.utils.PageUtils;
public interface StudentService {
PageUtils selectPage(String currentPageNoStr,String pageSizeStr,String sname);
StudentServiceImpl实现类:
package com.qf.service.impl;
import com.qf.dao.StudentDao;
import com.qf.dao.impl.StudentDaoImpl;
import com.qf.entity.Student;
import com.qf.service.StudentService;
import com.qf.utils.PageUtils;
public class StudentServiceImpl implements StudentService {
private StudentDao studentDao = new StudentDaoImpl();
public PageUtils selectPage(String currentPageNoStr, String pageSizeStr,String sname) {
if (currentPageNoStr==null||"".equals(currentPageNoStr)){
currentPageNo = Integer.parseInt(currentPageNoStr);
if (pageSizeStr==null||"".equals(pageSizeStr)){
pageSize = Integer.parseInt(pageSizeStr);
Integer totalPageCount = studentDao.selectCount(sname);
Integer totalPageSize = totalPageCount%pageSize==0?totalPageCount/pageSize:totalPageCount/pageSize+1;
List studentList = studentDao.selectByPage(currentPageNo, pageSize,sname);
PageUtils pageUtils = new PageUtils<>();
pageUtils.setCurrentPageNo(currentPageNo);
pageUtils.setPageSize(pageSize);
pageUtils.setTotalPageCount(totalPageCount);
pageUtils.setTotalPageSize(totalPageSize);
pageUtils.setCommenList(studentList);

BaseServlet类:
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.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class BaseServlet extends HttpServlet {
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String methodName = req.getParameter("mark");
if (methodName==null||"".equals(methodName)){
throw new RuntimeException("方法不存在");
Class cla = this.getClass();
Method method = cla.getMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
method.invoke(this,req,resp);
} catch (NoSuchMethodException e) {
} catch (IllegalAccessException e) {
} catch (InvocationTargetException e) {
StudentServlet类:
import com.qf.entity.Student;
import com.qf.service.StudentService;
import com.qf.service.impl.StudentServiceImpl;
import com.qf.utils.PageUtils;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet(name = "studentServlet",urlPatterns = "/studentServlet")
public class StudentServlet extends BaseServlet {
private StudentService studentService = new StudentServiceImpl();
public void showInfo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String currentPageNoStr = request.getParameter("currentPageNo");
String pageSizeStr = request.getParameter("pageSize");
String sname = request.getParameter("sname");
PageUtils pageUtils = studentService.selectPage(currentPageNoStr, pageSizeStr,sname);
request.setAttribute("pageUtils",pageUtils);
request.setAttribute("sname",sname);
request.getRequestDispatcher("index.jsp").forward(request,response);

index.jsp:
Created by IntelliJ IDEA.
To change this template use File | Settings | File Templates.
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<script src="${pageContext.request.contextPath}/js/jquery-1.8.3.js">script>
if(request.getAttribute("pageUtils")==null){
response.sendRedirect("studentServlet?mark=showInfo");
<div style="margin-left: 300px">
<input type="text" id="tv_sname" value="${sname}">
<input type="button" id="tv_select" value="查询">
<table width="80%" cellspacing="0px" cellpadding="0px" border="1px" align="center">
<c:forEach items="${pageUtils.commenList}" var="student">
<div style="margin-left: 300px">
<a href="${pageContext.request.contextPath}/studentServlet?mark=showInfo¤tPageNo=1">首页a>
<c:if test="${pageUtils.currentPageNo > 1}">
<a href="${pageContext.request.contextPath}/studentServlet?mark=showInfo¤tPageNo=${pageUtils.currentPageNo-1}">上一页a>
<c:if test="${pageUtils.currentPageNo < pageUtils.totalPageSize}">
<a href="${pageContext.request.contextPath}/studentServlet?mark=showInfo¤tPageNo=${pageUtils.currentPageNo+1}">下一页a>
<a href="${pageContext.request.contextPath}/studentServlet?mark=showInfo¤tPageNo=${pageUtils.totalPageSize}">末页a>
<span>当前页:${pageUtils.currentPageNo}span>
<span>总页数:${pageUtils.totalPageSize}span>
<span>总记录数:${pageUtils.totalPageCount}span>
<input type="text" size="4" id="tv_num"> <input type="button" value="go" id="tv_go">
$("#tv_go").click(function () {
var tv_num = $("#tv_num").val();
if (tv_num==null||tv_num==""){
var maxPage = '${pageUtils.totalPageSize}';
if (tv_num<=0||parseInt(tv_num)>parseInt(maxPage)){
window.location.href="${pageContext.request.contextPath}/studentServlet?mark=showInfo¤tPageNo="+tv_num;
$("#tv_select").click(function () {
var sname = $("#tv_sname").val();
window.location.href="${pageContext.request.contextPath}/studentServlet?mark=showInfo&sname="+sname;

效果图:
