jsp标签和增删改查案列

目录

1.标签语言的特点

2.自定义标签的使用步骤

3.JSP标签生命周期图

4.JSP标签实践

5.增删改查案列

1.主界面邦值servlet和index.jsp

2.增加servlet和主界面

3.修改servlet和主界面

4.删除servlet 


案列思路

jsp标签和增删改查案列_第1张图片

---------------------------------------------------------实际操作------------------------------------------------------------

1.标签语言的特点

了解标签结构
     开始标签
    true               标签提
    
           结束标签

    标签分类:控制标签,数据标签,UI标签
    没有标签体也能在网页中输出内容的标签,为UI标签
  

true
	false
	
	

-------------------------------------------------------------------------------------------------------------------------------

2.自定义标签的使用步骤

1.自定义标签库是与tld文件相关的

 2.标签库中的标签与tld中的元素有关,也就是跟tag元素的助手类有关

true
	false
	
	

-------------------------------------------------------------------------------------------------------------------------------

3.JSP标签生命周期图

1.助手类

package com.hz.tag;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;

/**
 * 助手类 必须继承bodytagSupport
 * @author Administrator
 *
 */
public class DemoTag1 extends BodyTagSupport{
	@Override
	public int doStartTag() throws JspException {
		System.out.println("========doStartTag========");
		return EVAL_BODY_INCLUDE;
	}
	
	@Override
	public int doAfterBody() throws JspException {
		System.out.println("=======doAfterBody=======");
		return super.doAfterBody();
//		return EVAL_BODY_AGAIN;

	}
	
	@Override
	public int doEndTag() throws JspException {
		System.out.println("==========doEndTag=========");
		return super.doEndTag();
//		return SKIP_PAGE;

	}
}

-------------------------------------------------------------------------------------------------------------------------------

4.JSP标签实践

package com.hz.tag;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;
/**
 * servlet中后台代码前台输出内容    out.print
 * 将数据输出到前台,首先拿到输出流
 * @author Administrator
 *
 */
public class OutTag extends BodyTagSupport{
	private Object value;

	public Object getValue() {
		return value;
	}

	public void setValue(Object value) {
		this.value = value;
	}

	@Override
	public int doStartTag() throws JspException {
		JspWriter out=pageContext.getOut();
		try {
			out.print(value);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return super.doStartTag();
	}
}
package com.hz.tag;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;

/**
 * 数据标签,存储数据的
 * 使用,pagecontext,request,session,applocation(servletContext)
 * 
 * 要存储数据,以健值对的方式进行存储,分析得来该标签有2个属性
 * @author Administrator
 *
 */
public class SetTag extends BodyTagSupport{
	private String var;
	private Object value;
	
	public String getVar() {
		return var;
	}

	public void setVar(String var) {
		this.var = var;
	}

	public Object getValue() {
		return value;
	}

	public void setValue(Object value) {
		this.value = value;
	}

	@Override
public int doStartTag() throws JspException {
//		 * 要存储数据,以健值对的方式进行存储,分析得来该标签有2个属性
		pageContext.setAttribute(var, value);
		return super.doStartTag();
}
}

-------------------------------------------------------------------------------------------------------------------------------

5.增删改查案列

1.主界面邦值servlet和index.jsp

servlet

package com.Hz.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

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 com.Hz.biz.ClassBiz;
import com.Hz.biz.HobbyBiz;
import com.Hz.biz.IClassBiz;
import com.Hz.biz.IHobbyBiz;
import com.Hz.biz.IStudentBiz;
import com.Hz.biz.ITeacherBiz;
import com.Hz.biz.StudentBiz;
import com.Hz.biz.TeacherBiz;
import com.Hz.entity.Class;
import com.Hz.entity.Hobby;
import com.Hz.entity.Student;
import com.Hz.entity.Teacher;

@WebServlet("/IndexServlet")
public class IndexServlet 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");
		response.setContentType("text/html; charset=UTF-8");
		
		//定义页码页数
		int pageInde=1;
		int pageSize=4;
		//接收表单传值
		String ctr = request.getParameter("ctr");//班级
		String htr = request.getParameter("htr");//爱好
		System.out.println(htr);
		String str = request.getParameter("str");//教员
		if(str==null) {
			str="";
		}
		if(htr==null) {
			htr="";
		}
		if(ctr==null) {
			ctr="";
		}
		String pid=request.getParameter("pid");
		if(pid!=null) {
			pageInde=Integer.parseInt(pid);
		}
		IStudentBiz isb=new StudentBiz();
		int rows=isb.getRows("tb_student where cid like '%"+ctr+"%' and hid like '%"+htr+"%' and tid like '%"+str+"%'");
		int max=rows/pageSize;
		if(rows%pageSize!=0) {
			max++;
		}
		if(max==0) {
			max=1;
		}
		//查询所有的班级
		IClassBiz icb=new ClassBiz();
		List lc = icb.getAll();
		System.out.println(lc);
		//查询所有的教员
		ITeacherBiz itb=new TeacherBiz();
		List lt = itb.getAll();
		//查询所有的学生
//		List ls = isb.getAll();
		//带模糊查询的分页
		List ls = isb.getMH(ctr, htr, str, pageInde, pageSize);
		//查询所有的爱好
		IHobbyBiz ihb=new HobbyBiz();
		List lh = ihb.getAll();

		if(ls.size()!=0&<.size()!=0&&lc.size()!=0&&lh.size()!=0) {
			request.setAttribute("pageIndex", pageInde);
			request.setAttribute("ls", ls);
			request.setAttribute("la", lt);
			request.setAttribute("lc", lc);
			request.setAttribute("lh", lh);
			request.setAttribute("max", max);
			request.getRequestDispatcher("index.jsp").forward(request, response);
		}
		else {
			System.out.println("集合为空");
		}
	}

}

主界面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>




Insert title here



	
${h.hname}
学生编号 学生姓名 学生的教员 学生所在班级 学生爱好 操作 增加
${s.sid} ${s.sname} ${s.t.tname} ${s.c.cname} ${sd.hname} 删除 修改
首页 上一页 [${pageIndex}/${max}] 下一页 尾页

主界面页面展示

jsp标签和增删改查案列_第2张图片

---------------------------------------------------------------------------------------------------------------------------------

2.增加servlet和主界面

增加servlet

package com.Hz.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

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 com.Hz.biz.ClassBiz;
import com.Hz.biz.HobbyBiz;
import com.Hz.biz.IClassBiz;
import com.Hz.biz.IHobbyBiz;
import com.Hz.biz.IStudentBiz;
import com.Hz.biz.ITeacherBiz;
import com.Hz.biz.StudentBiz;
import com.Hz.biz.TeacherBiz;
import com.Hz.entity.Class;
import com.Hz.entity.Hobby;
import com.Hz.entity.Student;
import com.Hz.entity.Teacher;

@WebServlet("/AddServlet")
public class AddServlet 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");
		response.setContentType("text/html; charset=UTF-8");
		
		//拿out
		PrintWriter out = response.getWriter();
		
		//接收表单传值
		String sname = request.getParameter("sname");
		String tid = request.getParameter("teacher");
		String cid = request.getParameter("class");
		String[] hobby = request.getParameterValues("aa");
		String ss="";
		for (String string : hobby) {
			ss+=string;
		}
		
		//调用业务逻辑层
		IStudentBiz isb=new StudentBiz();
		ITeacherBiz itb=new TeacherBiz();
		Teacher t = itb.getTeacher(Integer.parseInt(tid));
		IClassBiz icb=new ClassBiz();
		Class c = icb.getOne(Integer.parseInt(cid));
		Student stu=new Student(sname, t, c, ss);
		int n = isb.addStu(stu);
		if(n>0) {//加入成功
			out.print("");
		}
		else {//加入失败
			out.print("");
		}
	}

}

增加主界面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>




Insert title here



	
学生姓名
教员
班级
爱好 ${h.hname}

界面展示

jsp标签和增删改查案列_第3张图片

---------------------------------------------------------------------------------------------------------------------------------

3.修改servlet和主界面

package com.Hz.servlet;

import java.io.IOException;
import java.util.List;

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 com.Hz.biz.ClassBiz;
import com.Hz.biz.HobbyBiz;
import com.Hz.biz.IClassBiz;
import com.Hz.biz.IHobbyBiz;
import com.Hz.biz.IStudentBiz;
import com.Hz.biz.ITeacherBiz;
import com.Hz.biz.StudentBiz;
import com.Hz.biz.TeacherBiz;
import com.Hz.entity.Class;
import com.Hz.entity.Hobby;
import com.Hz.entity.Student;
import com.Hz.entity.Teacher;

@WebServlet("/PreUpdateServlet")
public class PreUpdateServlet 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");
		response.setContentType("text/html; charset=UTF-8");
		
		//接收
		String sid = request.getParameter("sid");
		//调用业务逻辑层
		IStudentBiz isb=new StudentBiz();
		Student stu = isb.getStu(Integer.parseInt(sid));
		IHobbyBiz ihb=new HobbyBiz();
		IClassBiz icb=new ClassBiz();
		ITeacherBiz itb=new TeacherBiz();
		List lh = ihb.getAll();
		List lc = icb.getAll();
		List lt = itb.getAll();
		request.setAttribute("lh", lh);
		request.setAttribute("lc", lc);
		request.setAttribute("la", lt);
		request.setAttribute("stu", stu);
		request.getRequestDispatcher("update.jsp").forward(request, response);
	}

}

package com.Hz.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

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 com.Hz.biz.ClassBiz;
import com.Hz.biz.HobbyBiz;
import com.Hz.biz.IClassBiz;
import com.Hz.biz.IHobbyBiz;
import com.Hz.biz.IStudentBiz;
import com.Hz.biz.ITeacherBiz;
import com.Hz.biz.StudentBiz;
import com.Hz.biz.TeacherBiz;
import com.Hz.entity.Class;
import com.Hz.entity.Hobby;
import com.Hz.entity.Student;
import com.Hz.entity.Teacher;

@WebServlet("/PreUpdateServlet1")
public class PreUpdateServlet1 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");
		response.setContentType("text/html; charset=UTF-8");
		PrintWriter out = response.getWriter();
		
		
		//接收表单传值
		String sid = request.getParameter("sid");
		String sname = request.getParameter("sname");
		String tid = request.getParameter("teacher");
		String cid = request.getParameter("class");
		String[] hobby = request.getParameterValues("aa");
		String ss="";
		for (String string : hobby) {
			ss+=string;
		}
		
		//调用业务逻辑层
		IStudentBiz isb=new StudentBiz();
		ITeacherBiz itb=new TeacherBiz();
		Teacher t = itb.getTeacher(Integer.parseInt(tid));
		IClassBiz icb=new ClassBiz();
		Class c = icb.getOne(Integer.parseInt(cid));
		Student stu=new Student(sname, t, c, ss);
		int n = isb.updStu(Integer.parseInt(sid),stu);
		if(n>0) {//加入成功
			out.print("");
		}
		else {//加入失败
			out.print("");
		}
	}

}

主界面

jsp标签和增删改查案列_第4张图片

--------------------------------------------------------------------------------------------------------------------------------

4.删除servlet 

package com.Hz.servlet;

import java.io.IOException;
import java.io.PrintWriter;

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 com.Hz.biz.IStudentBiz;
import com.Hz.biz.StudentBiz;

@WebServlet("/DeleteServlet")
public class DeleteServlet 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");
		response.setContentType("text/html; charset=UTF-8");
		
		PrintWriter out = response.getWriter();
		//接收表单传值
		String sid = request.getParameter("sid");
		//调用业务逻辑层
		IStudentBiz isb=new StudentBiz();
		int n = isb.delStu(Integer.parseInt(sid));
		if(n>0) {
			out.print("");
		}
		else {
			out.print("");
		}
	}

}

你可能感兴趣的:(java,servlet,服务器)