天天看點

JSP自定義标簽el任務圖檔目錄圖檔

浠誨姟鍥劇墖

JSP自定義标簽el任務圖檔目錄圖檔

鐩綍鍥劇墖

JSP自定義标簽el任務圖檔目錄圖檔

杩愯link.jsp鍥劇墖

JSP自定義标簽el任務圖檔目錄圖檔

鐐圭暀瑷€鍒楄〃鍚庢晥鏋滃浘

JSP自定義标簽el任務圖檔目錄圖檔

鏁版嵁搴撹〃

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鍥劇墖

JSP自定義标簽el任務圖檔目錄圖檔

tab_gestbook鏁版嵁搴撹〃璁捐琛ㄥ浘

JSP自定義标簽el任務圖檔目錄圖檔

绋嬪簭鎵ц杩囩▼濡傚浘

JSP自定義标簽el任務圖檔目錄圖檔

== 閫傚悎瀵矽薄javaWeb鍒濆鑰?==

鐣欒█鏉挎渚? 寮€鍙戠幆澧? java鐨刯dk:jdk1.8.0_66

java鐨勫紑鍙戝伐鍏鳳細eclipse锛坋clipse-photon锛? 鏁版嵁搴擄細MySQL Server 5.1

Web瀹瑰櫒锛歛pache-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>

	<!--閰嶇疆杩炴帴姹爉ysql -->
	<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" target="_blank" rel="external nofollow" >鐣欒█鍒楄〃</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>