📄 registerservlet.java
字号:
package com.alumni.servlet;
import javax.servlet.jsp.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;
import java.util.*;
import com.alumni.bean.*;
import com.jspsmart.upload.*;
public class RegisterServlet extends HttpServlet
{
ServletConfig config;
public void init(ServletConfig config) throws ServletException
{
this.config=config;
super.init(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
{
String username="";
String password="";
String email="";
String realName="";
String qq="";
String msn="";
String head="";
String canUpload="";
String uploadHead="";
String regDate="";
int adminClass=0;
int exp=0;
int totalPublish=0;
int totalVote=0;
int totalLogin=0;
SmartUpload su=null;
//使用SmartUpload组件的request对象
Request req=null;
//session
HttpSession session=request.getSession();
//out
PrintWriter out=response.getWriter();
//application
ServletContext application=this.getServletConfig().getServletContext();
DBConnection dbConn=null;
Connection conn=null;
//使用SmartUpload上传组件
try
{
//初始化SmartUpload对象
su=new SmartUpload();
su.initialize(config,request,response);
//设定最大上传的文件大小
su.setMaxFileSize(50000);
su.setTotalMaxFileSize(50000);
//设定允许上传的文件格式
su.setAllowedFilesList("jpg,gif,png");
su.upload();
//获取表单数据对象
req=su.getRequest();
//利用SmartUpload的Request对象提取表单数据
username=req.getParameter("username");
password=req.getParameter("password");
email=req.getParameter("email");
realName=req.getParameter("realname");
qq=req.getParameter("qq");
msn=req.getParameter("msn");
regDate=new java.util.Date().toLocaleString();
canUpload=req.getParameter("canUpload");
//下面开始判断用户是否选择上传图片
if(canUpload!=null&&canUpload.equals("yes"))
{
com.jspsmart.upload.Files files=su.getFiles();
com.jspsmart.upload.File file=files.getFile(0);
if(!file.isMissing())
{
//如果表单有值就另存为
String fileExt=file.getFileExt();
//文件另存为
java.util.Date time=new java.util.Date();
Long preFile=new Long(time.getTime());
String fileName="/upload/uploadHead/"+preFile.toString()+"."+fileExt;
file.saveAs(fileName,su.SAVE_VIRTUAL);
head=fileName;
}
else
{
//表单值为空的话就转至出错页面
response.sendRedirect("/error.jsp?code=FormEmptyException");
}
}
else
{
//没有自定义的话就把选择的头像添加到数据库中
head="/image/head/"+formatString(req.getParameter("head"));
}
//检查用户名是否已经被注册了
dbConn=new DBConnection();
conn=dbConn.getConnectionToAccess(application.getRealPath("db/alumni.mdb"));
Statement stmt=conn.createStatement();
ResultSet rs=stmt.executeQuery("SELECT ID FROM USERINFO WHERE USERNAME='"+username+"'");
if(rs.next())
{
response.sendRedirect("/error.jsp?code=UsernameHasBeenRegisteredException");
}
else
{
StringBuffer sb=new StringBuffer();
sb.append("INSERT INTO USERINFO(USERNAME,PASSWORD,EMAIL,REALNAME,HEAD,QQ,MSN,EXP,REG_DATE,ADMIN_CLASS,TOTAL_PUBLISH,TOTAL_VOTE,TOTAL_LOGIN)");
sb.append(" VALUES('");
sb.append(username);sb.append("','");
sb.append(password);sb.append("','");
sb.append(email);sb.append("','");
sb.append(realName);sb.append("','");
sb.append(head);sb.append("','");
sb.append(qq);sb.append("','");
sb.append(msn);sb.append("',");
sb.append(exp);sb.append(",'");
sb.append(regDate);sb.append("',");
sb.append(adminClass);sb.append(",");
sb.append(totalPublish);sb.append(",");
sb.append(totalVote);sb.append(",");
sb.append(totalLogin);sb.append(")");
stmt=conn.createStatement();
stmt.executeUpdate(sb.toString());
//关闭数据库
if(dbConn!=null)
{
dbConn.close();
}
response.sendRedirect("/login.jsp");
}
}
catch(SmartUploadException s)
{
if(dbConn!=null)
{
dbConn.close();
}
response.sendRedirect("/error.jsp?code=SmartUploadException");
}
catch(SQLException sqle)
{
if(dbConn!=null)
{
dbConn.close();
}
response.sendRedirect("/error.jsp?code=SQLException");
}
catch(Exception e)
{
if(dbConn!=null)
{
dbConn.close();
}
response.sendRedirect("/error.jsp?code=UnknownException");
}
}
//格式化字符串为GB2312
private String formatString(String input) throws UnsupportedEncodingException
{
if(input==null)
{
return "";
}
else
{
return new String(input.getBytes("ISO-8859-1"),"GB2312");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -