JSP自定义标签el

2 篇文章 0 订阅
1 篇文章 0 订阅
这篇博客适合javaWeb初学者,通过一个留言板案例,详细介绍了开发环境的配置,包括JDK 1.8.0_66、Eclipse Photon、MySQL Server 5.1和Tomcat 8.5.32。讲解了数据库表设计、Web容器的使用以及必需的jar包,如c3p0和mysql-connector。此外,还涉及了配置文件c3p0-config.xml、Servlet、DAO、POJO类的实现,以及JSP页面如list.jsp和link.jsp的使用,展示了EL表达式在JSP中的应用。
摘要由CSDN通过智能技术生成

任务图片

在这里插入图片描述

目录图片

在这里插入图片描述
运行link.jsp图片
在这里插入图片描述
点留言列表后效果图
在这里插入图片描述
数据库表
tab_admin

/*
MySQL Data Transfer
Source Host: localhost
Source Database: test
Target Host: localhost
Target Database: test
Date: 2019/9/14 17:59:27
*/

SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for tab_admin
-- ----------------------------
DROP TABLE IF EXISTS `tab_admin`;
CREATE TABLE `tab_admin` (
  `aid` int(11) NOT NULL AUTO_INCREMENT,
  `aname` varchar(255) NOT NULL,
  `password` varchar(255) NOT NULL,
  PRIMARY KEY (`aid`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=gbk;

-- ----------------------------
-- Records 
-- ----------------------------
INSERT INTO `tab_admin` VALUES ('1', '小洋', '123');
INSERT INTO `tab_admin` VALUES ('2', '郑泽', '123');
INSERT INTO `tab_admin` VALUES ('3', '小慧', '123');

数据库表
tab_admin图片
在这里插入图片描述
tab_gestbook数据库表设计表图
在这里插入图片描述
程序执行过程如图
在这里插入图片描述
== 适合对象javaWeb初学者 ==
留言板案例
开发环境
java的jdk:jdk1.8.0_66
java的开发工具:eclipse(eclipse-photon)
数据库:MySQL Server 5.1
Web容器:apache-tomcat-8.5.32
jar包:c3p0-0.9.1.2.jar mysql-connector-java-5.1.6-bin.jar
配置文件
c3p0-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
	<!--默认配置 -->
	<default-config>
		<property name="initialPoolSize">10</property>
		<property name="maxIdleTime">30</property>
		<property name="maxPoolSize">100</property>
		<property name="minPoolSize">10</property>
		<property name="maxStatements">200</property>
	</default-config>

	<!--配置连接池mysql -->
	<named-config name="mysql">
		<property name="driverClass">com.mysql.jdbc.Driver</property>
		<property name="jdbcUrl">jdbc:mysql://localhost:3306/test</property>
		<property name="user">root</property>
		<property name="password">123456</property>
		<property name="initialPoolSize">10</property>
		<property name="maxIdleTime">30</property>
		<property name="maxPoolSize">100</property>
		<property name="minPoolSize">10</property>
		<property name="maxStatements">200</property>
	</named-config>

</c3p0-config>

注意:将c3p0-config.xml放在src下面

  1. src下
    com.zz.controller下
    ListGestBookServlet.java
package com.zz.controller;

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

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.zz.dao.AdminDao;
import com.zz.dao.GestbookDao;
import com.zz.pojo.Gestbook;

public class ListGestBookServlet extends HttpServlet {
   private static final long serialVersionUID = 1L;
   private GestbookDao gestBookDao;
   public ListGestBookServlet() {
   	super();
   	this.gestBookDao = new GestbookDao();
   }

   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");

   	ArrayList<Gestbook> list = gestBookDao.getAllGestbook();
   	request.setAttribute("list", list);
//		HashMap以 aid<==>aname 的形式组织所有的数据保存进来.
   	HashMap<Integer,String> map=new HashMap<Integer,String>();
   	for(Gestbook book:list) {
   		Integer aid=book.getAid();
   		String aname=AdminDao.sum(book.getAid());
   		map.put(aid, aname);
   	}
   	request.setAttribute("map", map);
   	this.getServletContext().getRequestDispatcher("/list.jsp").forward(request, response);
   	
   }

   	

}

com\zz\dao
AdminDao.java

package com.zz.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.zz.pojo.Admin;
import com.zz.util.C3P0Utils;

public class AdminDao {
	public Admin login(Admin admin) {
		Admin result=null;
		String sql="select aid,aname,password from tab_admin where aname=? and password=?";
		Connection con=null;
		PreparedStatement ps=null;
		ResultSet rs=null;
		try {
			con = C3P0Utils.getConn();
			ps = con.prepareStatement(sql);
			ps.setString(1, admin.getAname());
			ps.setString(2, admin.getPassword());
			rs=ps.executeQuery();
			if(rs.next()) {
				result=new Admin();
				result.setAid(rs.getInt("aid"));
				result.setAname(rs.getString("aname"));
				result.setPassword(rs.getString("password"));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			C3P0Utils.closeJDBC(con, ps, rs);
		}
		return result;
	}

	public static String sum(int aid) {
		String sql="select aname from tab_admin where aid=?";
		String result=null;
		Connection con=null;
		PreparedStatement ps=null;
		ResultSet rs=null;
		try {
			con = C3P0Utils.getConn();
			ps = con.prepareStatement(sql);
			ps.setInt(1, aid);
			rs=ps.executeQuery();
			if(rs.next()) {
				result=rs.getString("aname");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			C3P0Utils.closeJDBC(con, ps, rs);
		}
		return result;
	}
}

GestbookDao.java
package com.zz.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.ArrayList;

import com.zz.pojo.Gestbook;
import com.zz.util.C3P0Utils;

public class GestbookDao {
//	获取所有的数据列表
	public ArrayList<Gestbook> getAllGestbook() {
		ArrayList<Gestbook> list = new ArrayList<Gestbook>();
		String sql = "select * from tab_gestbook";
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			con = C3P0Utils.getConn();
			ps = con.prepareStatement(sql);
			rs = ps.executeQuery();
			while (rs.next()) {
				Gestbook book = new Gestbook();
				book.setGst_id(rs.getInt("gst_id"));
				book.setAid(rs.getInt("aid"));
				book.setGst_title(rs.getString("gst_title"));
				book.setGst_content(rs.getString("gst_content"));
				book.setGst_ip(rs.getString("gst_ip"));
				Timestamp temp = rs.getTimestamp("gst_time");
				book.setGst_time(temp);
				list.add(book);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			C3P0Utils.closeJDBC(con, ps, rs);
		}
		return list;
	}

}

com\zz\pojo
Admin.java

package com.zz.pojo;

public class Admin {
	private Integer aid;
	private String aname;
	private String password;

	public Admin() {
		super();
	}

	public Admin(String aname, String password) {
		super();
		this.aname = aname;
		this.password = password;
	}

	public Admin(Integer aid, String aname, String password) {
		super();
		this.aid = aid;
		this.aname = aname;
		this.password = password;
	}

	public Integer getAid() {
		return aid;
	}

	public void setAid(Integer aid) {
		this.aid = aid;
	}

	public String getAname() {
		return aname;
	}

	public void setAname(String aname) {
		this.aname = aname;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

}

Gestbook.java

package com.zz.pojo;

import java.util.Date;

public class Gestbook {
	private Integer gst_id;
	private Integer aid;
	private String gst_title;
	private String gst_content;
	private String gst_ip;
	private Date gst_time;

	public Gestbook() {
		super();
	}

	public Gestbook(Integer gst_id, Integer aid, String gst_title, String gst_content, String gst_ip, Date gst_time) {
		super();
		this.gst_id = gst_id;
		this.aid = aid;
		this.gst_title = gst_title;
		this.gst_content = gst_content;
		this.gst_ip = gst_ip;
		this.gst_time = gst_time;
	}

	public Integer getGst_id() {
		return gst_id;
	}

	public void setGst_id(Integer gst_id) {
		this.gst_id = gst_id;
	}

	public Integer getAid() {
		return aid;
	}

	public void setAid(Integer aid) {
		this.aid = aid;
	}

	public String getGst_title() {
		return gst_title;
	}

	public void setGst_title(String gst_title) {
		this.gst_title = gst_title;
	}

	public String getGst_content() {
		return gst_content;
	}

	public void setGst_content(String gst_content) {
		this.gst_content = gst_content;
	}

	public String getGst_ip() {
		return gst_ip;
	}

	public void setGst_ip(String gst_ip) {
		this.gst_ip = gst_ip;
	}

	public Date getGst_time() {
		return gst_time;
	}

	public void setGst_time(Date gst_time) {
		this.gst_time = gst_time;
	}

}

com\zz\util
C3P0Utils.java

package com.zz.util;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class C3P0Utils {
	static ComboPooledDataSource dataSource = new ComboPooledDataSource("mysql");

	public static Connection getConn() {
		try {
			Connection conn = dataSource.getConnection();
			return conn;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	public static void closeJDBC(Connection con,Statement st,ResultSet rs) {
		if(rs!=null) {
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if(st!=null) {
			try {
				st.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		
		try {
			if(con!=null && con.isClosed()==false) {
				con.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}

DateUtil.java

package com.zz.util;

import java.text.SimpleDateFormat;
import java.util.Date;

public class DateUtil {
	public static String getStringByDate(Date date) {
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		return sdf.format(date);
	}
}

demo下
MyFunctions.java

package demo;

import com.zz.dao.AdminDao;

public class MyFunctions {

	public static String sum(int aid) {
		String aname = AdminDao.sum(aid);
		return aname;
	}
}

WebContent下
list.jsp

<%@ page language="java"
	import="java.util.*,com.zz.pojo.Gestbook,com.zz.util.DateUtil"
	contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglib prefix="myfn" uri="http://www.imti.com/myfunctions"%>
<%@page import="com.zz.pojo.Gestbook"%>
<%@page import="com.zz.controller.ListGestBookServlet"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<hr />

	<c:forEach items="${requestScope.list}" var="t">
		<c:choose>
			<c:when test="${empty list}">
				列表为空!
			</c:when>
			<c:otherwise>
				用户名:${myfn:sum(t.aid) }&nbsp;&nbsp;&nbsp;&nbsp;ip地址:${t.gst_ip}&nbsp;&nbsp;&nbsp;&nbsp;时间:${t.gst_time}<br/>
				标题:${t.gst_title}<br/>
				内容:${t.gst_content}
				<hr />
				<br />
			</c:otherwise>
		</c:choose>
	</c:forEach>

</body>
</html>

jspPage
link.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>留言列表</title>
</head>
<body>
<a href="${pageContext.request.contextPath }/ListGestBookServlet">留言列表</a>
</body>
</html>

WEB-INF\lib
c3p0-0.9.1.2.jar, jstl.jar, mysql-connector-java-5.1.6-bin.jar, standard.jar

WEB-INF
myfunction.tld

<?xml version="1.0" encoding="UTF-8" ?>

<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
	version="2.0">

	<description>JSTL 1.1 myfunctions library</description>
	<display-name>JSTL myfunctions</display-name>
	<tlib-version>1.0</tlib-version>
	<short-name>myfn</short-name>
	<uri>http://www.imti.com/myfunctions</uri>

	<function>
		<name>sum</name>
		<function-class>demo.MyFunctions</function-class>
		<function-signature>java.lang.Object sum(int)</function-signature>
	</function>
</taglib>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>MassageLable</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  <servlet>
    <description></description>
    <display-name>ListGestBookServlet</display-name>
    <servlet-name>ListGestBookServlet</servlet-name>
    <servlet-class>com.zz.controller.ListGestBookServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>ListGestBookServlet</servlet-name>
    <url-pattern>/ListGestBookServlet</url-pattern>
  </servlet-mapping>
</web-app>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值