自主设计通用JSP网站源代码提供
好久没来过论坛了,刚完成一个网站的后台制作,准备把代码开放,西北农林科技大学迎评专题网([url=http://pj.nwsuaf.edu.cn]http://pj.nwsuaf.edu.cn[/url]),该站点完全基于MVC封装,所有资料动态页统一程序生成静态页,单点登陆控制,统一的日志记录以及友好的错误处理,实现HTML代码过滤,登陆验证码设计及密码MD5加密并作非法字符效验,灵活的资料更新以及文件上传功能,能对上传图片自动生成一定规格缩略图和并提供集成的图象管理系统,除了利用了struts和log4j两个开源产品以外,所有代码自主设计,所有的类除servlet外都已经生成jdk文档,需要源程序和文档请在下面附自己的邮箱地址,我逐个邮寄,程序中有不足或则能够增加功能的地方,希望大家不吝赐教![ Last edited by aleel on 2004-12-1 at 03:45 ] 下面附上一部分类的源码,对各个方法均有符合jdk要求的注释,可以生成jdk文档便于阅读,由于该网站部署在Linux环境下,所以实际部署的类和现在公布的略有差别,但现在这些类在中文Windows系统上是完全可以执行的,这是由于在部署之前不知道系统将部署在Linux平台,所以采用的是GB2312编码才会出现这种问题,希望各位部署应用的时候最好能够能够UTF-8编码,这样将完全保证Java系统的跨平台性,而我的系统只能说是一个失败了,虽然通过打补丁的方法解决了这个问题
[code]
/**
* @author 一骑绝尘(王真-aleel) @time 2004-11-23
*/
package org.yuchen.util.format;
import java.util.Stack;
/**
* 一个html代码过滤器,在textfield域中对文字间换行和空格保持支持的同时, 对html代码不作变换
*/
public class HtmlFilter {
/**
* 对提供的字符串提供转换功能的静态方法.
*
* @param text
* 欲转换的字符串
* @return String 转换后返回的字符串
*/
public static String change(String text) {
//Logger.info(text);
StringBuffer buffer = new StringBuffer(\"\");
Stack s = new Stack();
char c;
int i = 0, j = 0, k = 0;
for (i = 0; i < text.length(); i++) {
if (text.charAt(i) == \'<\') {
s.push(new Character(text.charAt(i)));
j++;
} else if (text.charAt(i) == \'>\') {
s.push(new Character(text.charAt(i)));
j++;
for (k = 0; k < j; k++) {
buffer.append(((Character) s.elementAt(k)).toString());
}
s.clear();
j = 0;
} else if (!s.empty()) {
s.push(new Character(text.charAt(i)));
j++;
} else if (s.empty()) {
if (text.charAt(i) == \' \') {
buffer.append(\"\");
} else {
buffer.append(new Character(text.charAt(i)).toString());
}
}
}
s.clear();
//Logger.info(buffer.toString());
return buffer.toString();
}
}
[/code]
[ Last edited by aleel on 2004-12-1 at 03:33 ] [code]
/**
* @author 一骑绝尘(王真-aleel) @time 2004-11-23
*/
package org.yuchen.util.listor;
/**
* 列表器接口,抽象出列表器的公有方法.
*/
public interface Listor {
/** 设定标志段在数据库中的字段名 */
public void setId(String id);
/** 设定标题在数据库中的字段名 */
public void setTitle(String title);
/** 设定日期在数据库中的字段名 */
public void setDate(String date);
/** 设定数据库中的表的前缀 */
public void setTablePrefix(String tablePrefix);
/** 设定数据库中的表名(除去前缀部分) */
public void setTableName(String tableName);
/** 设定列表项的单行字符长度 */
public void setLen(String len);
/** 设定当前列表页的页号 */
public void setPara(String para);
/** 设定每行所显示的列表条目 */
public void setSep(String sep);
/** 取得每行所显示的列表条目 */
public int getSep();
/** 取得数据库中列表项的总数目 */
public int getNum();
/** 返回当前页显示的列表内容字符串表达 */
public String list();
}
[/code] [code]
/**
* @author 一骑绝尘(王真-aleel) @time 2004-11-23
*/
package org.yuchen.util.listor;
import java.io.Serializable;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import org.yuchen.conf.Logger;
import org.yuchen.mysql.CharConvert;
import org.yuchen.mysql.DBConn;
/**
* 用ul标签来实现数据项列表的类
*/
public class ULListor implements Listor, Serializable {
private boolean hasPrefix = false;
private boolean hasSuffix = false;
private int depth;
private String id;
private String title;
private String date;
private String tablePrefix;
private String tableName;
private int num;
private int len = 35;
private int sep = 25;
private int para;
public void setHasPrefix(String hasPrefix) {
this.hasPrefix = Boolean.valueOf(hasPrefix).booleanValue();
}
public void setHasSuffix(String hasSuffix) {
this.hasSuffix = Boolean.valueOf(hasSuffix).booleanValue();
}
public void setDepth(String depth) {
this.depth = Integer.parseInt(depth);
}
public void setId(String id) {
this.id = id;
//Logger.info(id);
}
public void setTitle(String title) {
this.title = title;
//Logger.info(title);
}
public void setDate(String date) {
this.date = date;
//Logger.info(date);
}
public void setTablePrefix(String tablePrefix) {
this.tablePrefix = tablePrefix;
//Logger.info(tablePrefix);
}
public void setTableName(String tableName) {
this.tableName = tableName;
//Logger.info(tableName);
}
public void setLen(String len) {
this.len = Integer.parseInt(len);
//Logger.info(\"len:\"+len);
}
public void setPara(String para) {
if (para != null && !para.equals(\"\")) {
this.para = Integer.parseInt(para);
} else {
this.para = 0;
}
//Logger.info(\"para:\"+para);
}
public void setSep(String sep) {
this.sep = Integer.parseInt(sep);
//Logger.info(\"sep:\"+sep);
}
public int getSep() {
//Logger.info(\"sep\"+sep);
return sep;
}
public int getNum() {
//Logger.info(\"运算进行到63行getNum():\"+num);
calculateNum();
return num;
}
private void calculateNum() {
DBConn dbConnMain = new DBConn();
String sql = \"select Count(*) from \" + tablePrefix + \"_\" + tableName;
//Logger.info(sql);
ResultSet rs = dbConnMain.executeQuery(sql);
try {
while (rs.next()) {
num = rs.getInt(1);
}
} catch (SQLException ex) {
Logger.println(this.getClass().getName());
Logger.println(ex);
} finally {
try {
rs.close();
} catch (SQLException ex) {
Logger.println(this.getClass().getName());
Logger.println(ex);
}
}
dbConnMain.close();
}
public String list() {
StringBuffer buffer = new StringBuffer(\"\");
String superPath = \"\";
for (int i = 0; i < depth; i++) {
superPath += \"../\";
}
Vector idlist = new Vector();
Vector titlelist = new Vector();
Vector datelist = new Vector();
String sql = null;
ResultSet rs = null;
buffer.append(\"<ul>\");
DBConn dbConnMain = new DBConn();
sql = \"select \" + id + \",\" + title + \",\" + date + \" from \"
+ tablePrefix + \"_\" + tableName + \" order by \" + id
+ \" desc limit \" + para * sep + \",\" + (para + 1) * sep;
//Logger.println(sql);
rs = dbConnMain.executeQuery(sql);
int i = 0;
try {
while (rs.next()) {
idlist.add(rs.getString(id));
titlelist.add(rs.getString(\"title\"));
datelist.add(rs.getString(\"date\"));
i++;
}
} catch (SQLException ex) {
Logger.println(this.getClass().getName());
Logger.println(ex);
} finally {
try {
rs.close();
dbConnMain.close();
} catch (SQLException ex) {
Logger.println(this.getClass().getName());
Logger.println(ex);
}
}
int length = i;
String prefix = \"\";
if (hasPrefix && !hasSuffix) {
for (i = 0; i < length; i++) {
title = CharConvert.ac((String) titlelist.elementAt(i));
buffer.append(\"<li><b>第\");
buffer.append(idlist.elementAt(i));
buffer.append(\"期:</b><a href=\'\");
buffer.append(superPath);
buffer.append(tableName);
buffer.append(\"/\");
buffer.append(idlist.elementAt(i));
buffer.append(\".html\'>\");
buffer.append(title.length() > len ? (title.substring(0,
len - 1) + \"...\") : title);
buffer.append(\"</a><font color=\'#666666\'>\");
buffer.append(((String) datelist.elementAt(i)).substring(5));
buffer.append(\"</font>\");
}
} else if (hasPrefix && hasSuffix) {
for (i = 0; i < length; i++) {
title = CharConvert.ac((String) titlelist.elementAt(i));
buffer.append(\"<li><b>第\");
buffer.append(idlist.elementAt(i));
buffer.append(\"期:</b><a href=\'\");
buffer.append(superPath);
buffer.append(tableName);
buffer.append(\"/\");
buffer.append(idlist.elementAt(i));
buffer.append(\".html\'>\");
buffer.append(title.length() > len ? (title.substring(0,
len - 1) + \"...\") : title);
buffer.append(\"</a><font color=\'#666666\'>\");
buffer.append(((String) datelist.elementAt(i)).substring(5));
buffer.append(\"</font>\");
buffer.append(\"<a href=\'../edit\");
buffer
.append(String.valueOf(tableName.charAt(0))
.toUpperCase());
buffer.append(tableName.substring(1));
buffer.append(\"?\");
buffer.append(id);
buffer.append(\"=\");
buffer.append(idlist.elementAt(i));
buffer.append(\"\'>修改</a>**<a href=\'../remove\");
buffer
.append(String.valueOf(tableName.charAt(0))
.toUpperCase());
buffer.append(tableName.substring(1));
buffer.append(\"?\");
buffer.append(id);
buffer.append(\"=\");
buffer.append(idlist.elementAt(i));
buffer.append(\"\'>删除</a>\");
}
} else if (!hasPrefix && hasSuffix) {
for (i = 0; i < length; i++) {
title = CharConvert.ac((String) titlelist.elementAt(i));
buffer.append(\"<li><a href=\'\");
buffer.append(superPath);
buffer.append(tableName);
buffer.append(\"/\");
buffer.append(idlist.elementAt(i));
buffer.append(\".html\'>\");
buffer.append(title.length() > len ? (title.substring(0,
len - 1) + \"...\") : title);
buffer.append(\"</a><font color=\'#666666\'>\");
buffer.append(((String) datelist.elementAt(i)).substring(5));
buffer.append(\"</font>\");
buffer.append(\"<a href=\'../edit\");
buffer
.append(String.valueOf(tableName.charAt(0))
.toUpperCase());
buffer.append(tableName.substring(1));
buffer.append(\"?\");
buffer.append(id);
buffer.append(\"=\");
buffer.append(idlist.elementAt(i));
buffer.append(\"\'>修改</a>**<a href=\'../remove\");
buffer
.append(String.valueOf(tableName.charAt(0))
.toUpperCase());
buffer.append(tableName.substring(1));
buffer.append(\"?\");
buffer.append(id);
buffer.append(\"=\");
buffer.append(idlist.elementAt(i));
buffer.append(\"\'>删除</a>\");
}
} else {
for (i = 0; i < length; i++) {
title = CharConvert.ac((String) titlelist.elementAt(i));
buffer.append(\"<li><a href=\'\");
buffer.append(superPath);
buffer.append(tableName);
buffer.append(\"/\");
buffer.append(idlist.elementAt(i));
buffer.append(\".html\'>\");
buffer.append(title.length() > len ? (title.substring(0,
len - 1) + \"...\") : title);
buffer.append(\"</a><font color=\'#666666\'>\");
buffer.append(((String) datelist.elementAt(i)).substring(5));
buffer.append(\"</font>\");
}
}
idlist.clear();
titlelist.clear();
datelist.clear();
buffer.append(\"</ul>\");
//Logger.info(buffer.toString());
return buffer.toString();
}
}
[/code] [code]
/**
* @author 一骑绝尘(王真-aleel) @time 2004-11-23
*/
package org.yuchen.util.listor;
import java.io.Serializable;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;
import org.yuchen.conf.Logger;
import org.yuchen.mysql.CharConvert;
import org.yuchen.mysql.DBConn;
/**
* 用table标签来实现数据项列表的类
*/
public class TableListor implements Listor, Serializable {
private String id;
private String title;
private String date;
private String tablePrefix;
private String tableName;
private int len = 35;
private int num = 0;
private int para = 0;
private int sep = 25;
public void setId(String id) {
this.id = id;
}
public void setTitle(String title) {
this.title = title;
}
public void setDate(String date) {
this.date = date;
}
public void setTablePrefix(String tablePrefix) {
this.tablePrefix = tablePrefix;
}
public void setTableName(String tableName) {
this.tableName = tableName;
}
public void setLen(String len) {
this.len = Integer.parseInt(len);
}
public void setPara(String para) {
if (para != null && !para.equals(\"\")) {
this.para = Integer.parseInt(para);
} else {
this.para = 0;
}
}
public void setSep(String sep) {
this.sep = Integer.parseInt(sep);
}
public int getSep() {
return sep;
}
public int getNum() {
DBConn dbConnMain = new DBConn();
String sql = \"select Count(*) from \" + tablePrefix + \"_\" + tableName;
ResultSet rs = dbConnMain.executeQuery(sql);
try {
while (rs.next()) {
num = rs.getInt(1);
}
} catch (SQLException ex) {
Logger.println(this.getClass().getName());
Logger.println(ex);
} finally {
try {
rs.close();
} catch (SQLException ex) {
Logger.println(this.getClass().getName());
Logger.println(ex);
}
}
dbConnMain.close();
return num;
}
public String list() {
StringBuffer buffer = new StringBuffer(\"\");
Vector idlist = new Vector();
Vector titlelist = new Vector();
Vector datelist = new Vector();
String sql;
ResultSet rs;
buffer.append(\"<table>\");
DBConn dbConnMain = new DBConn();
sql = \"select \" + id + \",\" + title + \",\" + date + \" from \"
+ tablePrefix + \"_\" + tableName + \" order by \" + id
+ \" desc limit \" + para * sep + \",\" + (para + 1) * sep;
//Logger.info(\"list():\"+sql);
rs = dbConnMain.executeQuery(sql);
int i = 0;
try {
while (rs.next()) {
idlist.add(rs.getString(id));
titlelist.add(rs.getString(\"title\"));
datelist.add(rs.getString(\"date\"));
i++;
}
} catch (SQLException ex) {
Logger.println(this.getClass().getName());
Logger.println(ex);
} finally {
try {
rs.close();
dbConnMain.close();
} catch (SQLException ex) {
Logger.println(this.getClass().getName());
Logger.println(ex);
}
}
int length = i;
for (i = 0; i < length; i++) {
title = CharConvert.ac((String) titlelist.elementAt(i));
buffer.append(\"<tr><th nowrap>第\");
buffer.append(idlist.elementAt(i));
buffer.append(\"期:</th><td><a href=\'\");
buffer.append(tableName);
buffer.append(\"/\");
buffer.append(idlist.elementAt(i));
buffer.append(\".html\'>\");
buffer
.append(title.length() > len ? (title.substring(0, len - 1) + \"...\")
: title);
buffer.append(\"</a>\");
buffer.append(\"<font color=\'#666666\'>\");
buffer.append(((String) datelist.elementAt(i)).substring(5));
buffer.append(\"</font></td></tr>\");
}
idlist.clear();
titlelist.clear();
datelist.clear();
buffer.append(\"</table>\");
//Logger.info(buffer.toString());
return buffer.toString();
}
}
[/code] [code]
package org.yuchen.file;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import org.yuchen.conf.Logger;
/**
* 按行阅读日志,并返回得到的结果
*/
public class LogReader {
private String pageNO;
private int sep = 30;
/**
* 设定当前阅读的日志页
*
* @param pageNO
* 页号
*/
public void setPageNO(String pageNO) {
this.pageNO = pageNO;
}
/**
* 设定每页显示的日志数
*
* @param sep
* 日志条数
*/
public void setSep(String sep) {
this.sep = Integer.parseInt(sep);
}
/**
* 阅读特定路径下的日志. 按照页号和该页的显示条目得到从日志文件取到的日志并返回,并提供前后页链接
*
* @param path
* 日志路径
* @return String 日志内容
*/
public String readLog(String path) {
int NO = 0;
int currLineNumber = 0;
String record = \"\";
StringBuffer buffer = new StringBuffer(\"\");
if (pageNO != null && !pageNO.equals(\"\")) {
NO = Integer.parseInt(pageNO);
currLineNumber = NO * sep;
}
FileInputStream fin = null;
LineNumberReader lr = null;
try {
fin = new FileInputStream(path);
lr = new LineNumberReader(new InputStreamReader(fin));
for (int i = 0; i < currLineNumber; i++) {
if (lr.readLine() == null) {
break;
}
}
//Logger.println(lr.getLineNumber()+\"\");
while (currLineNumber < (NO + 1) * sep
&& (record = lr.readLine()) != null) {
buffer.append(record + \"\\n\");
currLineNumber++;
}
if (NO > 0) {
buffer
.append(\"<a href=\'log.jsp?pageNO=\"
+ (NO - 1) + \"\'>上一页</a>\");
}
if (lr.readLine() != null) {
buffer
.append(\"<a href=\'log.jsp?pageNO=\"
+ (NO + 1) + \"\'>下一页</a>\\n\");
}
return buffer.toString();
} catch (IOException ex) {
Logger.println(ex);
} finally {
try {
if (lr != null) {
lr.close();
}
} catch (IOException ex) {
Logger.println(ex);
}
}
return null;
}
}
[/code] [code]
/**
* @author 一骑绝尘(王真-aleel)
* @time 2004-11-23
*/
package org.yuchen.struts;
import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.SQLException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.upload.FormFile;
import org.yuchen.conf.GetRealPath;
import org.yuchen.conf.Logger;
import org.yuchen.file.HtmlWriter;
import org.yuchen.image.ImageTransform;
import org.yuchen.mysql.DBConn;
import org.yuchen.util.time.Date;
public class UploadAction extends Action {
private DBConn dbConn;
public ActionForward perform(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
if (form instanceof UploadForm) {
UploadForm theForm = (UploadForm) form;
//retrieve the text data
String text = theForm.getTheText();
//retrieve the file representation
FormFile file = theForm.getTheFile();
//retrieve the file name
String fileName = file.getFileName();
//retrieve the content type
String contentType = file.getContentType();
boolean writeFile = theForm.getWriteFile();
//retrieve the file size
String size = (file.getFileSize() + \" bytes\");
Date date = new Date();
date.setPara(\"yyyy-MM-dd HH:mm:ss\");
String dateStr = date.getDate().replace(\':\', \'.\');
String data = null;
try {
if (text == null || fileName == null || text.equals(\"\")
|| fileName.equals(\"\")) {
throw new IOException(\"the text or file name is null\");
}
if (text.length() > 100) {
throw new IllegalArgumentException(
\"the text length is too long!\");
}
//insert data into database
dbConn = new DBConn();
String sql = \"insert into yp_picture(photo_title,photo_file_name,essayId) values(\'\"
+ text
+ \"\',\'\"
+ dateStr
+ \"\',\"
+ Integer.parseInt((String) request.getSession()
.getAttribute(\"essayId\")) + \")\";//保存图片的信息到数据库
int length = dbConn.executeUpdate(sql);
if (length == 0) {
throw new SQLException(\"inserting data failed!\");
}
//retrieve the file data
ByteArrayOutputStream baos = new ByteArrayOutputStream();
InputStream stream = file.getInputStream();
if (!writeFile) {
//only write files out that are less than 1MB
if (file.getFileSize() < (4 * 1024000)) {
byte[] buffer = new byte[8192];
int bytesRead = 0;
while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
baos.write(buffer, 0, bytesRead);
}
data = new String(baos.toByteArray());
} else {
data = new String(
\"The file is greater than 4MB, \"
+ \" and has not been written to stream.\"
+ \" File Size: \"
+ file.getFileSize()
+ \" bytes. This is a\"
+ \" limitation of this particular web application, hard-coded\"
+ \" in org.apache.struts.webapp.upload.UploadAction\");
}
} else {
//write the file to the file specified
String filePath = GetRealPath.getPath() + \"photo/\"
+ dateStr + \".jpg\";
OutputStream bos = new FileOutputStream(filePath);
//request.getSession().removeAttribute(\"essayId\");
//Logger.println(sql);
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
bos.write(buffer, 0, bytesRead);
}
bos.flush();
bos.close();
ImageTransform trans = new ImageTransform();
try {
trans.init(GetRealPath.getPath() + \"photo\", GetRealPath
.getPath()
+ \"photo\", dateStr, dateStr, \"jpg\", \"jpeg\");
trans.CreateThumbnail();//转换图像,生成微缩图
data = dateStr + \".jpg\";
new HtmlWriter().write(\"dynnews\", (String) request
.getSession().getAttribute(\"essayId\"));//生成相应静态页
response.sendRedirect(\"admin/display.jsp?text=\" + text
+ \"&fileName=\" + fileName + \"&size=\" + size
+ \"&data=\" + data + \"&contentType=\"
+ contentType);
} catch (Exception ex) {
Logger.println(ex);
}
}
//close the stream
stream.close();
//destroy the temporary file created
file.destroy();
} catch (FileNotFoundException fnfe) {
Logger.println(fnfe);
return null;
} catch (IOException ioe) {
Logger.println(ioe);
return null;
} catch (SQLException ex) {
Logger.println(ex);
} catch (IllegalArgumentException ex) {
Logger.println(ex);
} finally {
if (dbConn != null) {
dbConn.close();
}
}
}
return null;
}
}
[/code] [email]killer21cn@163.com[/email] 我觉得数据封装和数据方法的实现分开比较好
写在同一个类里不利于以后扩展 pass:美工有待改进 [color=red]随着一声『[color=#009999]观音娘娘到[/color]』,从天上掉下一滴甘露正好落在你的嘴唇上!
你在恍惚中看见了2两黄金。[/color]
再加一句:
干嘛把校徽搞得跟卫星一样?怎么想的?
用的什么数据库,这么慢
不过这在西农算高手了,这项目你净赚至少上千吧 页面设计不是楼主做的,他写的后台和数据库
给我发一份学习一下
[email]amoon0724@163.com[/email] 已发,呵呵 已发,呵呵 我也想学习一下!
发给我,谢谢
[email]mxd6799@163.com[/email]
再谢谢了!!! 给我也发一份吧
谢谢!
[email]zla_0723@613.com[/email] 楼主也给小弟发一份吧.小弟最近刚开始学...
[email]cccpmig_29@163.com[/email]
留下偶的QQ,楼主交个朋友啊. budong 谢了,我要!顺便想认识你!都是同路人!我的邮箱:
[email]msqingsi@163.com[/email] 我是主攻asp.net但对java对感兴趣!
页:
[1]
2