环境: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 = "news\\ice"+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);
response.sendRedirect("main.jsp");
}
else
{
%>
<jsp:forward page="index.jsp">
<jsp:param name="error" value="<%=result%>"/>
</jsp:forward>
<%
}
%>
main.jsp
<%@ page contentType="text/html;charset=gb2312"%>
<%
String admin =(String)(session.getAttribute("admin"));
if(admin==null){
response.sendRedirect("index.jsp");
}
else{
%>
<html><head><title>新闻发布</title></head>
<body>
<form name="pub" action="pub.jsp" method="post">
题目:<input type="text" name="title"><br>
内容:<textarea cols="100" rows="10" name="context"></textarea><br>
<input type="submit" value="提交"><br>
</form>
</body>
</html>
<%}%>
pub.jsp
<%@ page contentType="text/html;charset=gb2312"%>
<%
String admin = (String)(session.getAttribute("admin"));
String title = request.getParameter("title");
String context = request.getParameter("context");
if(admin == null){
response.sendRedirect("index.jsp");
}
else{
%>
<jsp:useBean id="pubBean" class="login.PubBean"/>
<jsp:setProperty name="pubBean" property="title" value="<%= title.trim() %>"/>
<jsp:setProperty name="pubBean" property="context" value="<%= context %>"/>
<%
pubBean.pubIt();
response.sendRedirect("display.jsp");
}
%>
display.jsp
<%@ page contentType