1. 程式人生 > >JSP-Servlet實現網上BBS專案小案例

JSP-Servlet實現網上BBS專案小案例

專案功能:實現一般bbs論壇專案的功能,發表說說 其他好友可以評論

一、資料庫的建立:
這裡寫圖片描述
sql語句如下:

    create table `bbs`.`user`(
        `userid` INT not null auto_increment,
       `username` CHAR(20) not null,
       `userpassword` CHAR(20) not null,
        primary key (`userid`)
    );

    create unique index `PRIMARY` on `bbs`.`user`
(`userid`);

    create table `bbs`.`article`(
        `articleid` INT not null auto_increment,
       `title` CHAR(50) not null,
       `context` CHAR(200) not null,
       `articletime` TIME not null,
       `userid` INT not null,
        primary key (`articleid`)
    );

    alter table `bbs`.`article`
add index `article_user_fk`(`userid`), add constraint `article_user_fk` foreign key (`userid`) references `bbs`.`user`(`userid`);
create unique index `PRIMARY` on `bbs`.`article`(`articleid`); create index `article_user_fk` on `bbs`.`article`(`userid`);

    create
table `bbs`.`comment`( `commentid` INT not null auto_increment, `commenttext` CHAR(200) not null, `commenttime` TIME not null, `userid` INT not null, `articleid` INT not null, primary key (`commentid`) );
alter table `bbs`.`comment` add index `comment_article_fk`(`articleid`), add constraint `comment_article_fk` foreign key (`articleid`) references `bbs`.`article`(`articleid`); alter table `bbs`.`comment` add index `comment_user_fk`(`userid`), add constraint `comment_user_fk` foreign key (`userid`) references `bbs`.`user`(`userid`); create unique index `PRIMARY` on `bbs`.`comment`(`commentid`); create index `comment_user_fk` on `bbs`.`comment`(`userid`); create index `comment_article_fk` on `bbs`.`comment`(`articleid`);

二、專案程式碼如下:
這裡寫圖片描述

程式碼的主要實現:解決中文亂碼的問題:

public class MyFilter implements Filter {

    private String encoding=null;  

    public void doFilter(ServletRequest arg0, ServletResponse arg1,
            FilterChain arg2) throws IOException, ServletException {
        arg0.setCharacterEncoding(encoding);   
        arg2.doFilter(arg0, arg1);  
    }

    public void init(FilterConfig arg0) throws ServletException {
        encoding = arg0.getInitParameter("encoding"); //獲得配置檔案中的encoding
    }

    public void destroy() {

    }
}

在web.xml檔案中配置

<!-- 過濾中文亂碼的問題 -->
    <filter>
        <filter-name>MyFilter</filter-name>
        <filter-class>com.xuliugen.util.MyFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>utf-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>MyFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>