目录
案列思路
---------------------------------------------------------实际操作------------------------------------------------------------
1.标签语言的特点
了解标签结构
<c:if test="true"> 开始标签
true 标签提
</c:if> 结束标签
标签分类:控制标签,数据标签,UI标签
没有标签体也能在网页中输出内容的标签,为UI标签
<c:if test="true">true</c:if>
<c:if test="false">false</c:if>
<c:set var="name" value="HZ"></c:set>
<c:out value="${name }"></c:out>
-------------------------------------------------------------------------------------------------------------------------------
2.自定义标签的使用步骤
1.自定义标签库是与tld文件相关的
2.标签库中的标签与tld中的元素有关,也就是跟tag元素的助手类有关
<h:if test="true">true</h:if>
<h:if test="false">false</h:if>
<h:set var="name" value="HZ"></h:set>
<h:out value="${name }"></h:out>
-------------------------------------------------------------------------------------------------------------------------------
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<Class> lc = icb.getAll();
System.out.println(lc);
//查询所有的教员
ITeacherBiz itb=new TeacherBiz();
List<Teacher> lt = itb.getAll();
//查询所有的学生
// List<Student> ls = isb.getAll();
//带模糊查询的分页
List<Student> ls = isb.getMH(ctr, htr, str, pageInde, pageSize);
//查询所有的爱好
IHobbyBiz ihb=new HobbyBiz();
List<Hobby> 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" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<!-- http://t.csdn.cn/tJGfj -->
</head>
<body>
<center>
<form method="post" action="IndexServlet">
<c:if test="${empty la}">
<jsp:forward page="IndexServlet"></jsp:forward>
</c:if>
<c:if test="${not empty la}">
<select name="str">
<c:forEach var="t" items="${la}">
<option value="${t.tid}">${t.tname}</option>
</c:forEach>
</select>
</c:if>
<c:if test="${empty lc}">
<jsp:forward page="IndexServlet"></jsp:forward>
</c:if>
<c:if test="${not empty lc}">
<select name="ctr">
<c:forEach var="c" items="${lc}">
<option value="${c.cid}">${c.cname}</option>
</c:forEach>
</select>
</c:if>
<c:if test="${empty lh}">
<jsp:forward page="IndexServlet"></jsp:forward>
</c:if>
<c:if test="${not empty lh}">
<c:forEach items="${lh}" var="h">
<input type="checkbox" name="htr" value="${h.hid},"/>${h.hname}
</c:forEach>
</c:if>
<input type="submit" value="查询"/>
</form>
<c:if test="${empty ls}">
<jsp:forward page="IndexServlet"></jsp:forward>
</c:if>
<c:if test="${not empty ls}">
<table border="1px">
<tr>
<td>学生编号</td>
<td>学生姓名</td>
<td>学生的教员</td>
<td>学生所在班级</td>
<td>学生爱好</td>
<td>操作 <a href="add.jsp">增加</a></td>
</tr>
<c:forEach items="${ls}" var="s">
<tr>
<td>${s.sid}</td>
<td>${s.sname}</td>
<td>${s.t.tname}</td>
<td>${s.c.cname}</td>
<td>
<c:forEach items="${s.ls}" var="sd">
${sd.hname}
</c:forEach>
</td>
<td>
<a onclick="return confirm('你确定要删除吗?');" href="DeleteServlet?sid=${s.sid}">删除</a>
<a href="PreUpdateServlet?sid=${s.sid}">修改</a>
</td>
</tr>
</c:forEach>
<tr>
<td colspan="6">
<a href="IndexServlet?pid=1">首页</a>
<a href="IndexServlet?pid=${pageIndex>1?pageIndex-1:1}">上一页</a>
[${pageIndex}/${max}]
<a href="IndexServlet?pid=${pageIndex<max?pageIndex+1:max}">下一页</a>
<a href="IndexServlet?pid=${max}">尾页</a>
</td>
</tr>
</table>
</c:if>
</center>
</body>
</html>
主界面页面展示
---------------------------------------------------------------------------------------------------------------------------------
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("<script>alert('增加成功');location.href='index.jsp';</script>");
}
else {//加入失败
out.print("<script>alert('增加失败');location.href='add.jsp';</script>");
}
}
}
增加主界面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
function myf(){
location.href="index.jsp";
}
</script>
</head>
<body>
<center>
<form action="AddServlet" method="post">
<table border="1px">
<tr>
<td>学生姓名</td>
<td><input type="text" name="sname"/></td>
</tr>
<tr>
<td>教员</td>
<td>
<c:if test="${empty la}">
<jsp:forward page="PreAddServlet"></jsp:forward>
</c:if>
<c:if test="${not empty la}">
<select name="teacher">
<c:forEach var="t" items="${la}">
<option value="${t.tid}">${t.tname}</option>
</c:forEach>
</select>
</c:if>
</td>
</tr>
<tr>
<td>班级</td>
<td>
<c:if test="${empty lc}">
<jsp:forward page="PreAddServlet"></jsp:forward>
</c:if>
<c:if test="${not empty lc}">
<select name="class">
<c:forEach var="c" items="${lc}">
<option value="${c.cid}">${c.cname}</option>
</c:forEach>
</select>
</c:if>
</td>
</tr>
<tr>
<td>爱好</td>
<td>
<c:if test="${empty lh}">
<jsp:forward page="PreAddServlet"></jsp:forward>
</c:if>
<c:if test="${not empty lh}">
<c:forEach items="${lh}" var="h">
<input type="checkbox" name="aa" value="${h.hid},"/>${h.hname}
</c:forEach>
</c:if>
</td>
</tr>
</table>
<input type="submit" value="增加"/>
<input type="button" value="清空" onclick="myf()"/>
</form>
</center>
</body>
</html>
界面展示
---------------------------------------------------------------------------------------------------------------------------------
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<Hobby> lh = ihb.getAll();
List<Class> lc = icb.getAll();
List<Teacher> 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("<script>alert('修改成功');location.href='index.jsp';</script>");
}
else {//加入失败
out.print("<script>alert('修改成功');location.href='PreUpdateServlet?sid="+sid+"';</script>");
}
}
}
主界面
--------------------------------------------------------------------------------------------------------------------------------
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("<script>alert('删除成功');location.href='index.jsp';</script>");
}
else {
out.print("<script>alert('删除失败');location.href='index.jsp';</script>");
}
}
}