一、JSP运行原理
每个JSP 页面在第一次被访问时,WEB容器都会把请求交给JSP引擎(即一个Java程序)去处理。JSP引擎先将JSP翻译成一个_jspServlet(实质上也是一个servlet) ,然后按照servlet的调用方式进行调用。
由于JSP第一次访问时会翻译成servlet,所以第一次访问通常会比较慢,但第二次访问,JSP引擎如果发现JSP没有变化,就不再翻译,而是直接调用,所以程序的执行效率不会受到影响
1、1 ServletContext 域--- application
ServletContext:代表整个web应用的对象。
生命周期:web应用被tomcat服务器加载时,ServletContext对象产生,生命周期开始。
web应用被移除容器或者tomcat服务器关闭的时候,ServletContext对象销毁,生命周期结束。
作用范围:整个web应用。
1、2:session 域---session
Session:代表整个会话的对象
生命周期:当调用request.getSession()时,Session对象被创建。生命周期开始
调用session.invalidate()方法销毁Session对象
在设定的时间内,Session对象没有被使用,则Session对象被销毁。默认为30分钟
当服务器意外关闭的时候,Session对象被销毁。当服务器正常关闭的时候,Session对象中仍有数据,会序列化到磁盘上形成一个文件,
这个过程称之为钝化。在服务器再次启动的时候,这个文件会被重新读取到服务器中使用,这个过程称之为活化。
作用范围:整个会话范围
主要功能:在会话范围内共享数据
1、3request 域---request
Request:代表请求的对象
生命周期:请求链开始,request对象被创建,请求链结束,request对象销毁。
作用范围:整个请求链
主要功能:在请求链内共享数据
1、4pageContext域---pageContext
PageContext代表当前页面的对象
生命周期:在访问jsp页面时,pageContext对象产生,生命周期开始。在结束访问jsp页面时,pageContext对象销毁,生命周期结束。
作用范围:整个jsp页面
主要功能:在整个jsp页面内共享数据
二、作用域测试
三、商品的增、删
3.1.1商品新增类
public int ad(Goods goods) {
try {
Class.forName(drive);
con=DriverManager.getConnection(url,use,pas);
String sql="insert into t_goods(gname,price,mark) values(?,?,?)";
pst=con.prepareStatement(sql);
pst.setObject(1,goods.getGname());
pst.setObject(2,goods.getPrice());
pst.setObject(3,goods.getMark());
row=pst.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}finally {
try{
if (rs!=null){
rs.close();
}
if (pst!=null){
pst.close();
}
if (con!=null){
con.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return row;
}
3.1.2新增语句执行
@WebServlet( "/addGoods")
public class Addgoods extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
Goods goods=new Goods();
goods.setGname(request.getParameter("gname"));
goods.setPrice(Double.parseDouble(request.getParameter("price")));
goods.setMark(request.getParameter("mark"));
Goodsdao goodsd=new Goodsdao();
int row = goodsd.ad(goods);
if (row>0){
request.getRequestDispatcher("selectAllGoods").forward(request,response);
}else{
request.getRequestDispatcher("error.jsp").forward(request,response);
}
}
}
3.1.3新增网页布局
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>添加商品</title>
</head>
<body>
<h2>添加所需要上架的商品</h2>
<form action="addGoods" method="poat">
商品名称:<input type="text" name="gname" value="" placeholder="商品名称"><br>
商品价格:<input type="number" step="0.01" name="price" value="" placeholder="商品价格"><br>
商品说明:<input type="text" name="mark" value="" placeholder="商品介绍"><br>
<input type="submit" value="提交">
</form>
</body>
</html>
3.2.1商品的删除
public int delect(int gid){
try {
Class.forName(drive);
con=DriverManager.getConnection(url,use,pas);
String sql="delete from t_goods where gid=?";
pst=con.prepareStatement(sql);
pst.setObject(1,gid);
row=pst.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}finally {
try{
if (rs!=null){
rs.close();
}
if (pst!=null){
pst.close();
}
if (con!=null){
con.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return row;
}
3.2.2商品全删的语句执行
@WebServlet( "/del")
public class Delet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// request.setCharacterEncoding("utf-8");
// response.setCharacterEncoding("utf-8");
int gid=Integer.parseInt(request.getParameter("gid"));
Goodsdao goodsdao=new Goodsdao();
int row=goodsdao.delect(gid);
if (row>0){
request.getRequestDispatcher("selectAllGoods").forward(request,response);
}else{
request.getRequestDispatcher("error.jsp").forward(request,response);
}
}
}
3.2.3商品删除的网页代码
在商品展示页面添加
<a href="del?gid=${goods.gid}">删除</a>
通过id获取信息
<%@ page import="java.util.List" %>
<%@ page import="com.bean.Goods" %><%--
Created by IntelliJ IDEA.
User: T
Date: 2023/2/21
Time: 8:33
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%-- 在页面导入jstl的核心类库--%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>主页</title>
</head>
<body>
<%--El表达式是通过${ } 从作用域对象中自动获取数据,如果对象可以通过,访问其属性--%>
<h2>欢迎${as.address}的${as.username}来到主页</h2>
<a href="addGoods.jsp">添加</a>
<table>
<tr>
<th>商品编号</th>
<th>商品名称</th>
<th>商品价格</th>
<th>商品说明</th>
</tr>
<%--items:要遍历的集合元素 var:临时变量--%>
<c:forEach items="${goodsList}" var="goods" >
<tr>
<td>${goods.gid}</td>
<td>${goods.gname}</td>
<td>${goods.price}</td>
<td>${goods.mark}</td>
<td>
<a href="del?gid=${goods.gid}">删除</a>
<a href="#">修改</a>
</td>
</tr>
</c:forEach>
</table>
</body>
</html>
四、模糊查询和修改
4.1修改
思维
1.点击修改跳转到servlet,后端根据id查询到该商品的信息;2.把商品信息添加到request作用域中请求转发到信息展示页面;3.用户根据需求修改表单中的字段,重新提交表单;4.编写后端servlet获取用户最新修订的商品信息,执行数据库的修改操作;5.重新请求查询所有商品,跳转到商品信息主页;
修改jsp内容
Dao内容
public Goods selectByid(int gid){
Goods goods=null;
try {
Class.forName(drive);
con=DriverManager.getConnection(url,use,pas);
String sql="select * from t_goods where gid=?";
pst=con.prepareStatement(sql);
pst.setInt(1,gid);
rs = pst.executeQuery();
if (rs.next()){
goods=new Goods();
goods.setGid(rs.getInt("gid"));
goods.setGname(rs.getString("gname"));
goods.setPrice(rs.getDouble("price"));
goods.setMark(rs.getString("mark"));
}
} catch (Exception e) {
e.printStackTrace();
}finally {
try{
if (rs!=null){
rs.close();
}
if (pst!=null){
pst.close();
}
if (con!=null){
con.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return goods;
}
public int update(Goods goods){
try {
Class.forName(drive);
con=DriverManager.getConnection(url,use,pas);
String sql="update t_goods set gname=?,price=?,mark=? where gid=?";
pst=con.prepareStatement(sql);
pst.setObject(1,goods.getGname());
pst.setObject(2,goods.getPrice());
pst.setObject(3,goods.getMark());
pst.setObject(4,goods.getGid());
row = pst.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}finally {
try{
if (rs!=null){
rs.close();
}
if (pst!=null){
pst.close();
}
if (con!=null){
con.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return row;
}
修改Select内容
public class FindByid extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int gid=Integer.parseInt(request.getParameter("gid"));
//根据gid查询商品信息
Goodsdao goodsdao=new Goodsdao();
Goods goods=goodsdao.selectByid(gid);
if (goods!=null){
request.setAttribute("goods",goods);
request.getRequestDispatcher("showgoods.jsp").forward(request,response);
}else{
request.getRequestDispatcher("error.jsp").forward(request,response);
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
@WebServlet("/update")
public class update extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
// 获取表单提交的数据,封装到goods对象中
Goods goods=new Goods();
goods.setGid(Integer.parseInt(request.getParameter("gid")));
goods.setGname(request.getParameter("gname"));
goods.setPrice(Double.parseDouble(request.getParameter("price")));
goods.setMark(request.getParameter("mark"));
System.out.println(goods);
Goodsdao goodsdao=new Goodsdao();
int row=goodsdao.update(goods);
if (row>0){
System.out.println("修改成功!");
request.getRequestDispatcher("selectAllGoods").forward(request,response);
}else{
request.setAttribute("error_msg","修改时出问题了!!");
request.getRequestDispatcher("error.jsp").forward(request,response);
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
4.2模糊查询
模糊查询的jsp内容
Dao内私有类
public List<Goods> seach(String keyword){
List<Goods> goodsList=new ArrayList<>();
try {
Class.forName(drive);
con=DriverManager.getConnection(url,use,pas);
String sql="select * from t_goods where gname like concat('%',?,'%')";
pst=con.prepareStatement(sql);
pst.setObject(1,keyword);
rs= pst.executeQuery();
while (rs.next()){
Goods goods=new Goods();
goods.setGid(rs.getInt("gid"));
goods.setGname(rs.getString("gname"));
goods.setPrice(rs.getDouble("price"));
goods.setMark(rs.getString("mark"));
// 把Goods对象储存到集合中
goodsList.add(goods);
}
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}finally {
try{
if (rs!=null){
rs.close();
}
if (pst!=null){
pst.close();
}
if (con!=null){
con.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
return goodsList;
}
Select内容
@WebServlet("/seach")
public class seach extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//设置请求和响应的编码
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
String keyword=request.getParameter("keyword");
// 执行JDBC模糊查询
Goodsdao goodsdao=new Goodsdao();
List<Goods> goodsList=goodsdao.seach(keyword);
System.out.println(goodsList);
HttpSession session = request.getSession();
// 名字与shangp.jsp中遍历结果集名字一致
session.setAttribute("goodsList",goodsList);
// 跳转到主页进行信息展示
response.sendRedirect("shangp.jsp");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
}