⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 postservlet.java

📁 jsp+javabean写的论坛
💻 JAVA
字号:
package com.alumni.servlet;
import java.io.*;
import java.sql.*;
import javax.servlet.jsp.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
import com.alumni.bean.*;
import com.jspsmart.upload.*;

public class PostServlet extends HttpServlet
{
	ServletConfig config;
	//初始化
	public void init(ServletConfig config) throws ServletException
	{
		super.init(config);
		this.config=config;
	}
	//调用doPost方法
	public void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException
	{
		this.doPost(request,response);
	}
	//doPost核心方法
	public void doPost(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException
	{
		int userID=0;
		String username=null;
		int state=0;
		String mood=null;
		String content=null;
		String pubDate=null;
		String picName=null;
		int subjectID=0;
		//有没有上传文件
		boolean hasFile=false;
		
		DBConnection dbConn=null;
		Connection conn=null;
		Statement stmt=null;
		
		//使用SmartUpload组件
		SmartUpload su=null;
		Request req=null;
		
		HTMLFilter htmlFilter=new HTMLFilter();//html过滤器
		FaceFilter faceFilter=new FaceFilter();//表情过滤器
		UbbCode ubbFilter=new UbbCode();//ubb过滤器
		
		//session
		HttpSession session=request.getSession();
		//out
		PrintWriter out=response.getWriter();
		//application
		ServletContext application=this.getServletConfig().getServletContext();
		
		//判断是否登陆过
		Boolean hasLogin=(Boolean)session.getAttribute("hasLogin");
		
		if(hasLogin!=null&&hasLogin.booleanValue()==true)
		{
			try
			{
				//提取SESSION中的用户名
				username=(String)session.getAttribute("username");
				
				dbConn=new DBConnection();
				conn=dbConn.getConnectionToAccess(application.getRealPath("db/alumni.mdb"));
				stmt=conn.createStatement();
				
				//从SESSION中获取用户ID
				String uID=(String)session.getAttribute("userID");
				userID=Integer.parseInt(uID);
			
				//初始化SmartUpload对象
				su=new SmartUpload();
				su.initialize(config,request,response);
				//设定最大上传的文件大小
				su.setMaxFileSize(1000000);
				su.setTotalMaxFileSize(1000000);
				//设定允许上传的文件格式
				su.setAllowedFilesList("jpg,JPG,gif,GIF,png,PNG,jpeg,JPEG,bmp,BMP");
				su.upload();
				
				//获取表单数据对象
				req=su.getRequest();
				//利用SmartUpload的Request对象提取表单数据
				mood=req.getParameter("mood");
				content=req.getParameter("content");
				state=1;
				pubDate=(new java.util.Date()).toLocaleString();
				
				//过滤表情和UBB代码
				if(content!=null)
				{
					htmlFilter.setSource(content);
					htmlFilter.run();
					content=htmlFilter.getResult();
					
					ubbFilter.setSource(content);
					ubbFilter.run();
					content=ubbFilter.getResult();
					
					faceFilter.setSource(content);
					faceFilter.run();
					content=faceFilter.getResult();
				}
				else
				{
					response.sendRedirect("/error.jsp?code=no value in content");
				}
				
				StringBuffer sql=new StringBuffer();
				sql.append("INSERT INTO SUBJECT(CONTENT,USER_ID,PUB_DATE,STATE,MOOD) ");
				sql.append("VALUES('");
				sql.append(content);sql.append("',");
				sql.append(userID);sql.append(",'");
				sql.append(pubDate);sql.append("',");
				sql.append(state);sql.append(",'");
				sql.append(mood);sql.append("')");
				stmt.executeUpdate(sql.toString());
				
				//总发表数+1
				String updateString="UPDATE USERINFO SET TOTAL_PUBLISH=TOTAL_PUBLISH+1 WHERE ID="+userID;
				stmt.executeUpdate(updateString);
				String updateExp="UPDATE USERINFO SET EXP=EXP+5 WHERE ID="+userID;
				stmt.executeUpdate(updateExp);
				
				//上传好帖子后获取该帖子的ID
				StringBuffer getIDString=new StringBuffer();
				getIDString.append("SELECT TOP 1 ID FROM SUBJECT WHERE USER_ID=");
				getIDString.append(userID);
				getIDString.append(" ORDER BY PUB_DATE DESC");
				ResultSet rsID=stmt.executeQuery(getIDString.toString());
				rsID.next();
				subjectID=rsID.getInt("ID");
				
				//开始上传文件
				com.jspsmart.upload.Files files=su.getFiles();
				com.jspsmart.upload.File file=files.getFile(0);
				if(!file.isMissing())
				{
					//如果表单有值就另存为
					hasFile=true;
					String fileExt=file.getFileExt();
					//文件另存为
					java.util.Date time=new java.util.Date();
					Long preFile=new Long(time.getTime());
					String fileName=preFile.toString()+"."+fileExt;
					//获得上传之后的文件名
					picName=fileName;
					StringBuffer uploadSql=new StringBuffer();
					uploadSql.append("INSERT INTO UPLOADPIC(SUBJECT_ID,PIC_NAME,USER_ID) ");
					uploadSql.append("VALUES(");
					uploadSql.append(subjectID);uploadSql.append(",'");
					uploadSql.append(picName);uploadSql.append("',");
					uploadSql.append(userID);uploadSql.append(")");
					
					//经验值+10
					stmt.executeUpdate(uploadSql.toString());
					String expSql="UPDATE USERINFO SET EXP=EXP+10 WHERE ID="+userID;
					stmt.executeUpdate(expSql);
					//保存文件
					file.saveAs("/upload/uploadPic/"+fileName,su.SAVE_VIRTUAL);
					
				}
				//上传完毕后关闭数据库
				if(dbConn!=null)
				{
					dbConn.close();
				}
				response.sendRedirect("/index.jsp");
				
			}
			catch(SmartUploadException s)
			{
				if(dbConn!=null)
				{
					dbConn.close();
				}
				response.sendRedirect("/error.jsp?code=SmartUploadException");
			}
			catch(SQLException se)
			{
				if(dbConn!=null)
				{
					dbConn.close();
				}
				response.sendRedirect("/error.jsp?code=SQLException");
			}
			catch(ClassNotFoundException cnte)
			{
				if(dbConn!=null)
				{
					dbConn.close();
				}
				response.sendRedirect("/error.jsp?code=ClassNotFoundException");
			}
			catch(Exception e)
			{
				if(dbConn!=null)
				{
					dbConn.close();
				}
				response.sendRedirect("/error.jsp?code=UnknownException");
			}
		}
		else
		{
			if(dbConn!=null)
			{
				dbConn.close();
			}
			//还未登陆
			response.sendRedirect("/login.jsp");
		}
	}
	
	//格式化字符串为GB2312
	private String formatString(String input) throws UnsupportedEncodingException
	{
		if(input==null)
		{
			return null;
		}
		else
		{
			return new String(input.getBytes("ISO-8859-1"),"GB2312");
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -