熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> JSP教程 >> 正文

一個完整的新聞發布系統代碼

2022-06-13   來源: JSP教程 

  環境Tomcat + JSE + PostgreSQL

  我將分幾個步驟完成對一個新聞發布系統的構建來理解JSP的一些基本使用方法!

  首先我將先介紹這個新聞發布系統的基本結構

  indexjsp管理員登陸界面
checkjsp驗證管理員身份
mainjsp管理員添加新聞的頁面
pubjsp發布信息的頁面
display顯示所有的新聞

  而後台的程序主要有

  DBjava數據庫連接
MDjavaMD算法
PubBeanjava發布
CheckBeanjava核實登陸身份

  即當你從index > main > display 走一趟你基本就可以完成一個新聞發布系統的基本功能了!

  我並非把新聞的標題和內容都寫入數據庫因為那樣太耗費數據庫系統的資源而且在訪問的時候總要讀取數

  據庫很費勁我把新聞寫入了一個單獨的HTM文件之後把標題及HTM文件的名字寫入的數據庫!

  而這個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 javasql*;

  public class DB {

  private Connection conn;
 private Statement stmt;
 private ResultSet rs;
 
 public DB() {
  try {
   ClassforName("orgpostgresqlDriver");
   conn = DriverManagergetConnection

  ("jdbc:postgresql://localhost:/news?user=admin&&password=");
   stmt = conncreateStatement();   
  }
  catch(Exception e) {
   Systemoutprintln(e);
  }
 }
 
 public void update(String sql) {
  try {
   stmtexecuteUpdate(sql);
  }
  catch(Exception e) {
   Systemoutprintln(e);
  }
 }
 
 public ResultSet quarry(String sql) {
  try {
   rs = stmtexecuteQuery(sql);
  }
  catch(Exception e) {
   Systemoutprintln(e);
  }
  return rs;
 }
 
}

  
package login;

  import javasql*;
import javaio*;

  public class PubBean {
 
 private String titlecontext;
 private DB db;
 private MD md;
 
 public PubBean() {
  db = new DB();
  md = new MD();  
 }
 
 public void setTitle(String title){
  thistitle = title;
 }
 
 public void setContext(String context) {
  thiscontext = context;
 }

  public void pubIt() {
  try {
   title = new String(titlegetBytes("_")"gb");
   context = new String(contextgetBytes("_")"gb");
   String titleMD = mdgetkeyBeanofStr(title);
   dbupdate("insert into news values("+title+""+titleMD+")");
   String file = "news\\ice"+titleMD+"htm";
   PrintWriter pw = new PrintWriter(new FileOutputStream(file));
   pwprintln("<title>"+title+"</title>");
   pwprintln(context);
   pwclose(); 
  }
  catch(Exception e){
   Systemoutprintln(e);
  }
 }
 
}

  
package login;

  import javasql*;

  public class CheckBean {
 
 private String message=""adminpassword;
 private DB db;
 
 public CheckBean() {
  db = new DB(); 
 }
 
 public void setAdmin(String admin){
  thisadmin = admin;
 }
 
 public void setPassword(String password) {
  thispassword = password;
 }
 
 public String checkIt() {
  try {
   ResultSet rs = dbquarry("select * from administrator where

  admin="+thisadmin+"");
   while(rsnext()){
    String pws = rsgetString("password")trim();
    if(pwsequals(thispassword)){
     message = "密碼正確!"; 
    }
    else message = "密碼錯誤!";
    return message;
   }
   message = "用戶不存在!";
  }
  catch(Exception e) {
   Systemoutprintln(e);
  }
  return message;
 }
 
}

  
================頁面部分==================

  indexjsp:

  <%@ page contentType="text/html;charset=gb"%>
<html><head><title>登陸系統</title></head>
<body>
<form name="login" action="checkjsp" method="post">
 用戶<input type="text" name="admin"><br>
 密碼<input type="password" name="password"><br>
 <input type="submit" value="登陸"><br>
</form>
</body>
</html>
<%
 String error=requestgetParameter("error");
 error=new String(errorgetBytes("_")"gb");
 if(error==null) {}
 else{
  %>
  <%=error%>
  <%
 }
%>

  checkjsp

  <%@ page contentType="text/html;charset=gb"%>
<%@ page import="loginCheckBean"%>
<%
 String admin = requestgetParameter("admin");
 String password = requestgetParameter("password");
%>
<jsp:useBean id="checkBean" class="loginCheckBean"/>
<jsp:setProperty name="checkBean" property="admin" value="<%= admintrim() %>"/>
<jsp:setProperty name="checkBean" property="password" value="<%= passwordtrim() %>"/>
<%
 String result = checkBeancheckIt();
 if(resultequals("密碼正確!")){
  sessionsetAttribute("admin"admin);
  responsesendRedirect("mainjsp");
 }
 else
 {
  %>
  <jsp:forward page="indexjsp">
   <jsp:param name="error" value="<%=result%>"/>
  </jsp:forward>      
  <%
 }
%>

  mainjsp

  <%@ page contentType="text/html;charset=gb"%>
<%
 String admin =(String)(sessiongetAttribute("admin"));
 if(admin==null){
  responsesendRedirect("indexjsp");
 }
 else{
%>
<html><head><title>新聞發布</title></head>
<body>
<form name="pub" action="pubjsp" method="post">
 題目<input type="text" name="title"><br>
 內容<textarea cols="" rows="" name="context"></textarea><br>
 <input type="submit" value="提交"><br>
</form>
</body>
</html>
<%}%>

  pubjsp

  <%@ page contentType="text/html;charset=gb"%>
<%
 String admin = (String)(sessiongetAttribute("admin"));
 String title = requestgetParameter("title");
 String context = requestgetParameter("context");
 if(admin == null){
  responsesendRedirect("indexjsp");
 }
 else{
 %>
  <jsp:useBean id="pubBean" class="loginPubBean"/>
  <jsp:setProperty name="pubBean" property="title" value="<%= titletrim() %>"/>
  <jsp:setProperty name="pubBean" property="context" value="<%= context %>"/>
 <%
  pubBeanpubIt();
  responsesendRedirect("displayjsp");
 }
%>

  displayjsp

  <%@ page contentType="text/html;charset=gb"%>
<%@ page import="javasql*"%>
<%
 ClassforName("sunjdbcodbcJdbcOdbcDriver");
 Connection conn=DriverManagergetConnection("jdbc:odbc:PostgreSQL""""");
 Statement stmt=conncreateStatement(); 
%>
<html><head><title>新聞</title></head>
<body>
<%
 ResultSet rs=stmtexecuteQuery("SELECT * FROM news");
 //顯示記錄
 while(rsnext()){
  outprint("<a href=news/ice"+rsgetString()+"htm target=_blank>"+rsgetString

  ()+"</a>");
  outprintln("<br>");
 }  %>
</body>
</html>

  
好了基本的東西都實現了希望現在已經可以給你一個完整的面貌了在後面的文章中我再把程序一步步

  的完善增加一些新的功能!

  import javalangreflect*;

  public class MD {
        /* 下面這些SS實際上是一個*的矩陣在原始的C實現中是用#define 實現的
        這裡把它們實現成為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計算過程中用到的個核心數據在原始的C實現中
           被定義到keyBean_CTX結構中

  */
        private long[] state = new long[];  // state (ABCD)
        private long[] count = new long[];  // number of bits modulo ^ (lsb first)
        private byte[] buffer = new byte[]; // input buffer

  /* digestHexStr是keyBean的唯一一個公共成員是最新一次計算結果的
          進制ASCII表示
        */
        public String digestHexStr;

  /* digest是最新一次計算結果的進制內部表示表示bit的keyBean值
        */
        private byte[] digest = new byte[];

  /*
          getkeyBeanofStr是類keyBean最主要的公共方法入口參數是你想要進行keyBean變換的字符串
          返回的是變換完的結果這個結果是從公共成員digestHexStr取得的.
        */
        public String getkeyBeanofStr(String inbuf) {
                keyBeanInit();
                keyBeanUpdate(inbufgetBytes() inbuflength());
                keyBeanFinal();
                digestHexStr = "";
                for (int i = ; i < ; i++) {
                        digestHexStr += byteHEX(digest[i]);
                }
                return digestHexStr;

  }
        // 這是keyBean這個類的標准構造函數JavaBean要求有一個public的並且沒有參數的構造函數
        public MD() {
                keyBeanInit();

  return;
        }

  
        /* keyBeanInit是一個初始化函數初始化核心變量裝入標准的幻數 */
        private void keyBeanInit() {
                count[] = L;
                count[] = L;
                ///* Load magic initialization constants

  state[] = xL;
                state[] = xefcdabL;
                state[] = xbadcfeL;
                state[] = xL;

  return;
        }
        /* F G H I 是個基本的keyBean函數在原始的keyBean的C實現中由於它們是
        簡單的位運算可能出於效率的考慮把它們實現成了宏在java中我們把它們
       實現成了private方法名字保持了原來C中的 */

  private long F(long x long y long z) {
                return (x & y) | ((~x) & z);

  }
        private long G(long x long y long z) {
                return (x & z) | (y & (~z));

  }
        private long H(long x long y long z) {
                return x ^ y ^ z;
        }

  private long I(long x long y long z) {
                return y ^ (x | (~z));
        }

  /*
          FFGGHH和II將調用FGHI進行近一步變換
          FF GG HH and II transformations for rounds and
          Rotation is separate from addition to prevent recomputation
       */

  private long FF(long a long b long c long d long x long s
                long ac) {
                a += F (b c d) + x + ac;
                a = ((int) a << s) | ((int) a >>> ( s));
                a += b;
                return a;
        }

  private long GG(long a long b long c long d long x long s
                long ac) {
                a += G (b c d) + x + ac;
                a = ((int) a << s) | ((int) a >>> ( s));
                a += b;
                return a;
        }
        private long HH(long a long b long c long d long x long s
                long ac) {
                a += H (b c d) + x + ac;
                a = ((int) a << s) | ((int) a >>> ( s));
                a += b;
                return a;
        }
        private long II(long a long b long c long d long x long s
                long ac) {
                a += I (b c d) + x + ac;
                a = ((int) a << s) | ((int) a >>> ( s));
                a += b;
                return a;
        }
        /*
         keyBeanUpdate是keyBean的主計算過程inbuf是要變換的字節串inputlen是長度這個
         函數由getkeyBeanofStr調用調用之前需要調用keyBeaninit因此把它設計成private的
        */
        private void keyBeanUpdate(byte[] inbuf int inputLen) {

  int i index partLen;
                byte[] block = new byte[];
                index = (int)(count[] >>> ) & xF;
                // /* Update number of bits */
                if ((count[] += (inputLen << )) < (inputLen << ))
                        count[]++;
                count[] += (inputLen >>> );

  partLen = index;

  // Transform as many times as possible
                if (inputLen >= partLen) {
                        keyBeanMemcpy(buffer inbuf index partLen);
                        keyBeanTransform(buffer);

  for (i = partLen; i + < inputLen; i += ) {

  keyBeanMemcpy(block inbuf i );
                                keyBeanTransform (block);
                        }
                        index = ;

  } else

  i = ;

  ///* Buffer remaining input */
                keyBeanMemcpy(buffer inbuf index i inputLen i);

  }

  /*
          keyBeanFinal整理和填寫輸出結果
        */
        private void keyBeanFinal () {
                byte[] bits = new byte[];
                int index padLen;

  ///* Save number of bits */
                Encode (bits count );

  ///* Pad out to mod
                index = (int)(count[] >>> ) & xf;
                padLen = (index < ) ? ( index) : ( index);
                keyBeanUpdate (PADDING padLen);

  ///* Append length (before padding) */
                keyBeanUpdate(bits );

  ///* Store state in digest */
                Encode (digest state );

  }

  /* keyBeanMemcpy是一個內部使用的byte數組的塊拷貝函數從input的inpos開始把len長度的
字節拷貝到output的outpos位置開始
        */

  private void keyBeanMemcpy (byte[] output byte[] input
                int outpos int inpos int len)
        {
                int i;

  for (i = ; i < len; i++)
                        output[outpos + i] = input[inpos + i];
        }

  /*
           keyBeanTransform是keyBean核心變換程序有keyBeanUpdate調用block是分塊的原始字節
        */
        private void keyBeanTransform (byte block[]) {
                long a = state[] b = state[] c = state[] d = state[];
                long[] x = new long[];

  Decode (x block );

  /* Round */
                a = FF (a b c d x[] S xdaaL); /* */
                d = FF (d a b c x[] S xecbL); /* */
                c = FF (c d a b x[] S xdbL); /* */
                b = FF (b c d a x[] S xcbdceeeL); /* */
                a = FF (a b c d x[] S xfcfafL); /* */
                d = FF (d a b c x[] S xcaL); /* */
                c = FF (c d a b x[] S xaL); /* */
                b = FF (b c d a x[] S xfdL); /* */
                a = FF (a b c d x[] S xdL); /* */
                d = FF (d a b c x[] S xbfafL); /* */
                c = FF (c d a b x[] S xffffbbL); /* */
                b = FF (b c d a x[] S xcdbeL); /* */
                a = FF (a b c d x[] S xbL); /* */
                d = FF (d a b c x[] S xfdL); /* */
                c = FF (c d a b x[] S xaeL); /* */
                b = FF (b c d a x[] S xbL); /* */

  /* Round */
                a = GG (a b c d x[] S xfeL); /* */
                d = GG (d a b c x[] S xcbL); /* */
                c = GG (c d a b x[] S xeaL); /* */
                b = GG (b c d a x[] S xebcaaL); /* */
                a = GG (a b c d x[] S xdfdL); /* */
                d = GG (d a b c x[] S xL); /* */
                c = GG (c d a b x[] S xdaeL); /* */
                b = GG (b c d a x[] S xedfbcL); /* */
                a = GG (a b c d x[] S xecdeL); /* */
                d = GG (d a b c x[] S xcdL); /* */
                c = GG (c d a b x[] S xfddL); /* */
                b = GG (b c d a x[] S xaedL); /* */
                a = GG (a b c d x[] S xaeeL); /* */
                d = GG (d a b c x[] S xfcefafL); /* */
                c = GG (c d a b x[] S xfdL); /* */
                b = GG (b c d a x[] S xdacaL); /* */

  /* Round */
                a = HH (a b c d x[] S xfffaL); /* */
                d = HH (d a b c x[] S xfL); /* */
                c = HH (c d a b x[] S xddL); /* */
                b = HH (b c d a x[] S xfdecL); /* */
                a = HH (a b c d x[] S xabeeaL); /* */
                d = HH (d a b c x[] S xbdecfaL); /* */
                c = HH (c d a b x[] S xfbbbL); /* */
                b = HH (b c d a x[] S xbebfbcL); /* */
                a = HH (a b c d x[] S xbecL); /* */
                d = HH (d a b c x[] S xeaafaL); /* */
                c = HH (c d a b x[] S xdefL); /* */
                b = HH (b c d a x[] S xdL); /* */
                a = HH (a b c d x[] S xdddL); /* */
                d = HH (d a b c x[] S xedbeL); /* */
                c = HH (c d a b x[] S xfacfL); /* */
                b = HH (b c d a x[] S xcacL); /* */

  /* Round */
                a = II (a b c d x[] S xfL); /* */
                d = II (d a b c x[] S xaffL); /* */
                c = II (c d a b x[] S xabaL); /* */
                b = II (b c d a x[] S xfcaL); /* */
                a = II (a b c d x[] S xbcL); /* */
                d = II (d a b c x[] S xfcccL); /* */
                c = II (c d a b x[] S xffeffdL); /* */
                b = II (b c d a x[] S xddL); /* */
                a = II (a b c d x[] S xfaefL); /* */
                d = II (d a b c x[] S xfeceeL); /* */
                c = II (c d a b x[] S xaL); /* */
                b = II (b c d a x[] S xeaL); /* */
                a = II (a b c d x[] S xfeL); /* */
                d = II (d a b c x[] S xbdafL); /* */
                c = II (c d a b x[] S xaddbbL); /* */
                b = II (b c d a x[] S xebdL); /* */

  state[] += a;
                state[] += b;
                state[] += c;
                state[] += d;

  }

  /*Encode把long數組按順序拆成byte數組因為java的long類型是bit的
          只拆低bit以適應原始C實現的用途
        */
        private void Encode (byte[] output long[] input int len) {
                int i j;

  for (i = j = ; j < len; i++ j += ) {
                        output[j] = (byte)(input[i] & xffL);
                        output[j + ] = (byte)((input[i] >>> ) & xffL);
                        output[j + ] = (byte)((input[i] >>> ) & xffL);
                        output[j + ] = (byte)((input[i] >>> ) & xffL);
                }
        }

  /*Decode把byte數組按順序合成成long數組因為java的long類型是bit的
          只合成低bitbit清零以適應原始C實現的用途
        */
        private void Decode (long[] output byte[] input int len) {
                int i j;

  
                for (i = j = ; j < len; i++ j += )
                        output[i] = biu(input[j]) |
                                (biu(input[j + ]) << ) |
                                (biu(input[j + ]) << ) |
                                (biu(input[j + ]) << );

  return;
        }

  /*
          biu是我寫的一個把byte按照不考慮正負號的原則的"升位"程序因為java沒有unsigned運算
        */
        public static long biu(byte b) {
                return b < ? b & xF + : b;
        }

  /*byteHEX()用來把一個byte類型的數轉換成十六進制的ASCII表示
         因為java中的byte的toString無法實現這一點我們又沒有C語言中的
          sprintf(outbuf"%X"ib)
        */
        public static String byteHEX(byte ib) {
                char[] Digit = {
                ABCDEF };
                char [] ob = new char[];
                ob[] = Digit[(ib >>> ) & XF];
                ob[] = Digit[ib & XF];
                String s = new String(ob);
                return s;
        }
/*
        public static void main(String args[]) {

  MD m = new MD();
                Systemoutprintln("我愛你"+mgetkeyBeanofStr("我愛你"));
        }
        */

  }


From:http://tw.wingwit.com/Article/program/Java/JSP/201311/20523.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.