環境
我將分幾個步驟完成對一個新聞發布系統的構建
首先我將先介紹這個新聞發布系統的基本結構
index
check
main
pub
display
而後台的程序主要有
DB
MD
PubBean
CheckBean
即當你從index
我並非把新聞的標題和內容都寫入數據庫
據庫
而這個HTM文件的名字怎麼隨機生成呢?我選擇了MD
性!
下面我先把這個系統的基本框架勾勒出來
================數據庫部分==================
CREATE TABLE administrator
(
admin char(
"password" char(
)
WITHOUT OIDS;
ALTER TABLE administrator OWNER TO admin;
CREATE TABLE news
(
title char(
page char(
)
WITHOUT OIDS;
ALTER TABLE news OWNER TO admin;
================程序部分==================
package login;
import java
public class DB {
private Connection conn;
private Statement stmt;
private ResultSet rs;
public DB() {
try {
Class
conn = DriverManager
("jdbc:postgresql://localhost:
stmt = conn
}
catch(Exception e) {
System
}
}
public void update(String sql) {
try {
stmt
}
catch(Exception e) {
System
}
}
public ResultSet quarry(String sql) {
try {
rs = stmt
}
catch(Exception e) {
System
}
return rs;
}
}
package login;
import java
import java
public class PubBean {
private String title
private DB db;
private MD
public PubBean() {
db = new DB();
md
}
public void setTitle(String title){
this
}
public void setContext(String context) {
this
}
public void pubIt() {
try {
title = new String(title
context = new String(context
String titleMD
db
String file = "news\\ice"+titleMD
PrintWriter pw = new PrintWriter(new FileOutputStream(file));
pw
pw
pw
}
catch(Exception e){
System
}
}
}
package login;
import java
public class CheckBean {
private String message=""
private DB db;
public CheckBean() {
db = new DB();
}
public void setAdmin(String admin){
this
}
public void setPassword(String password) {
this
}
public String checkIt() {
try {
ResultSet rs = db
admin=
while(rs
String pws = rs
if(pws
message = "密碼正確!";
}
else message = "密碼錯誤!";
return message;
}
message = "用戶不存在!";
}
catch(Exception e) {
System
}
return message;
}
}
================頁面部分==================
index
<%@ page contentType="text/html;charset=gb
<html><head><title>登陸系統</title></head>
<body>
<form name="login" action="check
用戶
密碼
<input type="submit" value="登陸"><br>
</form>
</body>
</html>
<%
String error=request
error=new String(error
if(error==null) {}
else{
%>
<%=error%>
<%
}
%>
check
<%@ page contentType="text/html;charset=gb
<%@ page import="login
<%
String admin = request
String password = request
%>
<jsp:useBean id="checkBean" class="login
<jsp:setProperty name="checkBean" property="admin" value="<%= admin
<jsp:setProperty name="checkBean" property="password" value="<%= password
<%
String result = checkBean
if(result
session
response
}
else
{
%>
<jsp:forward page="index
<jsp:param name="error" value="<%=result%>"/>
</jsp:forward>
<%
}
%>
main
<%@ page contentType="text/html;charset=gb
<%
String admin =(String)(session
if(admin==null){
response
}
else{
%>
<html><head><title>新聞發布</title></head>
<body>
<form name="pub" action="pub
題目
內容
<input type="submit" value="提交"><br>
</form>
</body>
</html>
<%}%>
pub
<%@ page contentType="text/html;charset=gb
<%
String admin = (String)(session
String title = request
String context = request
if(admin == null){
response
}
else{
%>
<jsp:useBean id="pubBean" class="login
<jsp:setProperty name="pubBean" property="title" value="<%= title
<jsp:setProperty name="pubBean" property="context" value="<%= context %>"/>
<%
pubBean
response
}
%>
display
<%@ page contentType="text/html;charset=gb
<%@ page import="java
<%
Class
Connection conn=DriverManager
Statement stmt=conn
%>
<html><head><title>新聞</title></head>
<body>
<%
ResultSet rs=stmt
//顯示記錄
while(rs
out
(
out
} %>
</body>
</html>
好了
的完善
import java
public class MD
/* 下面這些S
這裡把它們實現成為static final是表示了只讀
Instance間共享*/
static final int S
static final int S
static final int S
static final int S
static final int S
static final int S
static final int S
static final int S
static final int S
static final int S
static final int S
static final int S
static final int S
static final int S
static final int S
static final int S
static final byte[] PADDING = {
/* 下面的三個成員是keyBean計算過程中用到的
被定義到keyBean_CTX結構中
*/
private long[] state = new long[
private long[] count = new long[
private byte[] buffer = new byte[
/* digestHexStr是keyBean的唯一一個公共成員
*/
public String digestHexStr;
/* digest
*/
private byte[] digest = new byte[
/*
getkeyBeanofStr是類keyBean最主要的公共方法
返回的是變換完的結果
*/
public String getkeyBeanofStr(String inbuf) {
keyBeanInit();
keyBeanUpdate(inbuf
keyBeanFinal();
digestHexStr = "";
for (int i =
digestHexStr += byteHEX(digest[i]);
}
return digestHexStr;
}
// 這是keyBean這個類的標准構造函數
public MD
keyBeanInit();
return;
}
/* keyBeanInit是一個初始化函數
private void keyBeanInit() {
count[
count[
///* Load magic initialization constants
state[
state[
state[
state[
return;
}
/* F
簡單的位運算
實現成了private方法
private long F(long x
return (x & y) | ((~x) & z);
}
private long G(long x
return (x & z) | (y & (~z));
}
private long H(long x
return x ^ y ^ z;
}
private long I(long x
return y ^ (x | (~z));
}
/*
FF
FF
Rotation is separate from addition to prevent recomputation
*/
private long FF(long a
long ac) {
a += F (b
a = ((int) a << s) | ((int) a >>> (
a += b;
return a;
}
private long GG(long a
long ac) {
a += G (b
a = ((int) a << s) | ((int) a >>> (
a += b;
return a;
}
private long HH(long a
long ac) {
a += H (b
a = ((int) a << s) | ((int) a >>> (
a += b;
return a;
}
private long II(long a
long ac) {
a += I (b
a = ((int) a << s) | ((int) a >>> (
a += b;
return a;
}
/*
keyBeanUpdate是keyBean的主計算過程
函數由getkeyBeanofStr調用
*/
private void keyBeanUpdate(byte[] inbuf
int i
byte[] block = new byte[
index = (int)(count[
// /* Update number of bits */
if ((count[
count[
count[
partLen =
// Transform as many times as possible
if (inputLen >= partLen) {
keyBeanMemcpy(buffer
keyBeanTransform(buffer);
for (i = partLen; i +
keyBeanMemcpy(block
keyBeanTransform (block);
}
index =
} else
i =
///* Buffer remaining input */
keyBeanMemcpy(buffer
}
/*
keyBeanFinal整理和填寫輸出結果
*/
private void keyBeanFinal () {
byte[] bits = new byte[
int index
///* Save number of bits */
Encode (bits
///* Pad out to
index = (int)(count[
padLen = (index <
keyBeanUpdate (PADDING
///* Append length (before padding) */
keyBeanUpdate(bits
///* Store state in digest */
Encode (digest
}
/* keyBeanMemcpy是一個內部使用的byte數組的塊拷貝函數
字節拷貝到output的outpos位置開始
*/
private void keyBeanMemcpy (byte[] output
int outpos
{
int i;
for (i =
output[outpos + i] = input[inpos + i];
}
/*
keyBeanTransform是keyBean核心變換程序
*/
private void keyBeanTransform (byte block[]) {
long a = state[
long[] x = new long[
Decode (x
/* Round
a = FF (a
d = FF (d
c = FF (c
b = FF (b
a = FF (a
d = FF (d
c = FF (c
b = FF (b
a = FF (a
d = FF (d
c = FF (c
b = FF (b
a = FF (a
d = FF (d
c = FF (c
b = FF (b
/* Round
a = GG (a
d = GG (d
c = GG (c
b = GG (b
a = GG (a
d = GG (d
c = GG (c
b = GG (b
a = GG (a
d = GG (d
c = GG (c
b = GG (b
a = GG (a
d = GG (d
c = GG (c
b = GG (b
/* Round
a = HH (a
d = HH (d
c = HH (c
b = HH (b
a = HH (a
d = HH (d
c = HH (c
b = HH (b
a = HH (a
d = HH (d
c = HH (c
b = HH (b
a = HH (a
d = HH (d
c = HH (c
b = HH (b
/* Round
a = II (a
d = II (d
c = II (c
b = II (b
a = II (a
d = II (d
c = II (c
b = II (b
a = II (a
d = II (d
c = II (c
b = II (b
a = II (a
d = II (d
c = II (c
b = II (b
state[
state[
state[
state[
}
/*Encode把long數組按順序拆成byte數組
只拆低
*/
private void Encode (byte[] output
int i
for (i =
output[j] = (byte)(input[i] &
output[j +
output[j +
output[j +
}
}
/*Decode把byte數組按順序合成成long數組
只合成低
*/
private void Decode (long[] output
int i
for (i =
output[i] = b
(b
(b
(b
return;
}
/*
b
*/
public static long b
return b <
}
/*byteHEX()
因為java中的byte的toString無法實現這一點
sprintf(outbuf
*/
public static String byteHEX(byte ib) {
char[] Digit = {
char [] ob = new char[
ob[
ob[
String s = new String(ob);
return s;
}
/*
public static void main(String args[]) {
MD
System
}
*/
}
From:http://tw.wingwit.com/Article/program/Java/JSP/201311/20523.html