在安装的tomcat文件夹下的webapps\JSP_DB(这个是我的)下面建文件夹news
MySQL数据库中建database new 建表news
create table news(title varchar(100),fileName varchar(500));
//创建pub.jsp
<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>简单新闻发布系统</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<center>
<form action="pub_do.jsp" method="post">
<table width="600" border="0">
<tr>
<th scope="col">新闻发布系统</th>
</tr>
<tr>
<td><div align="center">新闻标题:
<input name="title" type="text" size="60"/>
</div></td>
</tr>
<tr>
<td><hr size="2" /></td>
</tr>
<tr>
<td><div align="center">新闻内容</div></td>
</tr>
<tr>
<td><div align="center">
<textarea name="content" rows="15" cols="65"></textarea>
</div></td>
</tr>
<tr>
<td><hr size="2"/></td>
</tr>
<tr>
<td><div align="center">
<input type="submit" name="Submit" value="发布" />
</div></td>
</tr>
</table>
</form>
</center>
</body>
</html>
//创建pub_do.jsp
<%@ page language="java" import="java.util.*" pageEncoding="GB2312"%>
<%@page import="java.io.*" %>
<%@page import="java.sql.*"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>发布页面</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%!
//根据提交的内容创建HTML文件
private void createFile(String title,String content,File file)throws IOException,SQLException{
if(!file.exists()){
file.createNewFile();//创建新文件
}
PrintWriter pw=new PrintWriter(new FileOutputStream(file));//输出流
pw.println("<title>"+title+"</title>");//向文件写入新闻标题
pw.println(content);//向文件写入新闻的内容
pw.close();//关闭输出流
}
%>
<%!
//把新闻标题,与其对应的HTML文件名写入数据库
private void insertSQL(String title,String fileName)throws SQLException{
String sql="insert into news values('"+title+"','"+fileName+"')";
Statement st=getStatement();//取得Statement对象
st.executeUpdate(sql);
st.close();
}
%>
<%!
//取得Statement对象
private Statement getStatement()throws SQLException{
try{
Class.forName("com.mysql.jdbc.Driver");
}catch(Exception e){}
String url="jdbc:mysql://localhost:3306/new?user=root&password=root&characterEncoding=gb2312";
Connection con=DriverManager.getConnection(url);
return con.createStatement();
}
%>
<%
String title=request.getParameter("title");
title=new String(title.getBytes("ISO-8859-1"));
String content=request.getParameter("content");
content=new String(content.getBytes("ISO-8859-1"));
java.util.Date date=new java.util.Date();
String str="news"+date.getYear()+date.getMonth()+date.getDay()+date.getHours()
+date.getMinutes()+date.getSeconds();
File file=new File("E:\\apache-tomcat-6.0.35\\webapps\\JSP_DB\\news",str+".htm");
//创建一个具有唯一名称的HTML文件
try{
createFile(title,content,file);//创建文件函数
String fileName=file.getName();
insertSQL(title,fileName);//将新闻标题、与其对应的HTML 文件放到数据库中 的函数调用
response.sendRedirect("news/"+fileName);//页面转向新生成的HTML页面
}catch(Exception e){
out.print(e.toString());
}
%>
</body>
</html>