環境:Tomcat 5.5.7 + J2SE 5.0 + PostgreSQL 8.0
我將分幾個步驟完成對一個新聞發布系統的構建,來理解JSP的一些基本使用方法!
首先我將先介紹這個新聞發布系統的基本結構:
index.jsp:管理員登陸界面
check.jsp:驗證管理員身份
main.jsp:管理員添加新聞的頁面
pub.jsp:發布信息的頁面
display:顯示所有的新聞
而後台的程序主要有:
DB.java:數據庫連接
MD5.java:MD5算法
PubBean.java:發布
CheckBean.java:核實登陸身份
即當你從index -> main -> display 走一趟你基本就可以完成一個新聞發布系統的基本功能了!
我並非把新聞的標題和內容都寫入數據庫,因為那樣太耗費數據庫系統的資源,而且在訪問的時候總要讀取數
據庫,很費勁,我把新聞寫入了一個單獨的HTM文件,之後把標題及HTM文件的名字寫入的數據庫!
而這個HTM文件的名字怎麼隨機生成呢?我選擇了MD5算法,因為每個新聞的標題都不會相同,所以保證了唯一
性!
下面我先把這個系統的基本框架勾勒出來,說的大一點,這似乎就是這個“系統”的“內核”啦!:)
================數據庫部分==================
CREATE TABLE administrator
(
admin char(16),
"password" char(32)
)
WITHOUT OIDS;
ALTER TABLE administrator OWNER TO admin;
CREATE TABLE news
(
title char(255),
page char(255)
)
WITHOUT OIDS;
ALTER TABLE news OWNER TO admin;
================程序部分==================
package login;
import java.sql.*;
public class DB {
private Connection conn;
private Statement stmt;
private ResultSet rs;
public DB() {
try {
Class.forName("org.postgresql.Driver");
conn = DriverManager.getConnection
("jdbc:postgresql://localhost:5432/news?user=admin&&password=");
stmt = conn.createStatement();
}
catch(Exception e) {
System.out.println(e);
}
}
public void update(String sql) {
try {
stmt.executeUpdate(sql);
}
catch(Exception e) {
System.out.println(e);
}
}
public ResultSet quarry(String sql) {
try {
rs = stmt.executeQuery(sql);
}
catch(Exception e) {
System.out.println(e);
}
return rs;
}
}
package login;
import java.sql.*;
import java.io.*;
public class PubBean {
private String title,context;
private DB db;
private MD5 md5;
public PubBean() {
db = new DB();
md5 = new MD5();
}
public void setTitle(String title){
this.title = title;
}
public void setContext(String context) {
this.context = context;
}
public void pubIt() {
try {
title = new String(title.getBytes("8859_1"),"gb2312");
context = new String(context.getBytes("8859_1"),"gb2312");
String titleMD5 = md5.getkeyBeanofStr(title);
db.update("insert into news values('"+title+"','"+titleMD5+"')");
String file = "newsice"+titleMD5+".htm";
PrintWriter pw = new PrintWriter(new FileOutputStream(file));
pw.println("<title>"+title+"</title>");
pw.println(context);
pw.close();
}
catch(Exception e){
System.out.println(e);
}
}
}
package login;
import java.sql.*;
public class CheckBean {
private String message="",admin,password;
private DB db;
public CheckBean() {
db = new DB();
}
public void setAdmin(String admin){
this.admin = admin;
}
public void setPassword(String password) {
this.password = password;
}
public String checkIt() {
try {
ResultSet rs = db.quarry("select * from administrator where
admin='"+this.admin+"'");
while(rs.next()){
String pws = rs.getString("password").trim();
if(pws.equals(this.password)){
message = "密碼正確!";
}
else message = "密碼錯誤!";
return message;
}
message = "用戶不存在!";
}
catch(Exception e) {
System.out.println(e);
}
return message;
}
}
================頁面部分==================
index.jsp:
<%@ page contentType="text/html;charset=gb2312"%>
<html><head><title>登陸系統</title></head>
<body>
<form name="login" action="check.jsp" method="post">
用戶:<input type="text" name="admin"><br>
密碼:<input type="password" name="password"><br>
<input type="submit" value="登陸"><br>
</form>
</body>
</html>
<%
String error=request.getParameter("error");
error=new String(error.getBytes("8859_1"),"gb2312");
if(error==null) {}
else{
%>
<%=error%>
<%
}
%>
check.jsp
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="login.CheckBean"%>
<%
String admin = request.getParameter("admin");
String password = request.getParameter("password");
%>
<jsp:useBean id="checkBean" class="login.CheckBean"/>
<jsp:setProperty name="checkBean" property="admin" value="<%= admin.trim() %>"/>
<jsp:setProperty name="checkBean" property="password" value="<%= password.trim() %>"/>
<%
String result = checkBean.checkIt();
if(result.equals("密碼正確!")){
session.setAttribute("admin",admin);