首页 > 代码库 > 新闻发布系统(二)
新闻发布系统(二)
新闻发布系统
首先我将先介绍这个新闻发布系统的基本结构: index.jsp:登陆界面 main.jsp:添加新闻的页面 pub.jsp:发布信息的页面 display:显示所有的新闻 即当你从index -> main -> display 走一趟你基本就可以完成一个新闻发布系统的基本功能了!
首先,肯定就是登陆了,之前不用连接数据库,写死就可以了,现在要连接数据库,其实也挺好写的,下面就看看是怎么连接数据库的吧(数据库我用的是MySQL)
LoginImpl log=new LoginImpl();
String name=request.getParameter("uname");
String pwd=request.getParameter("upwd");
//session.getAttribute("syso")!=null && log.login(name, pwd)
if(log.login(name, pwd)){
session.setAttribute(name, "uname");
request.getRequestDispatcher("/newspages/admin.jsp").forward(request,response);
}else{
request.getRequestDispatcher("/index.jsp").forward(request,response);
}
================程序部分==================
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+"</titlepw.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="http://www.mamicode.com/登陆"><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="http://www.mamicode.com/"/> <jsp:setProperty name="checkBean" property="password" value="http://www.mamicode.com/"/>
<% 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="http://www.mamicode.com/"/> </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="http://www.mamicode.com/提交"><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="http://www.mamicode.com/"/>
<jsp:setProperty name="pubBean" property="context" value="http://www.mamicode.com/"/>
<% pubBean.pubIt(); response.sendRedirect("display.jsp"); } %> display.jsp <%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%> <% Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection conn=DriverManager.getConnection("jdbc:odbc:PostgreSQL","","");
Statement stmt=conn.createStatement(); %>
<html><head><title>新闻</title></head> <body> <% ResultSet rs=stmt.executeQuery("SELECT * FROM news");
//显示记录
while(rs.next()){
out.print("<a href=http://www.mamicode.com/news/ice"+rs.getString(2)+".htm target=_blank>"+rs.getString (1)+"</a>"); out.println("<br>"); } %>
</body>
</html>
新闻发布系统(二)