功能介绍
游客:查看首页最新问卷、问卷列表、私有问卷、参与问卷调查;
管理员:登录系统、网站信息配置、问卷管理(问卷添加、题目管理、问卷审核、问卷统计)、管理员管理、友情链接管理。
部分功能模块代码
package com.survey.ctrl;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLEncoder;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.survey.dao.AdminDAO;
import com.survey.dao.DAOFactory;
import com.survey.dao.QuestionDAO;
import com.survey.dao.SurveyDAO;
import com.survey.dto.Admin;
public class AdminManage extends HttpServlet {
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String mutex1="";
//String mutex2="";
String op=request.getParameter("op");
if("AddAdmin".equals(op)){
String username=request.getParameter("username");
String pwd=request.getParameter("pwd");
AdminDAO dao=DAOFactory.getAdminDAO();
Admin admin=new Admin();
admin.setA_user(username);
admin.setA_pass(pwd);
boolean ret1=dao.addAdmin(admin);
if(ret1)
response.sendRedirect("../admin/AdminList.jsp");
else
response.sendRedirect("../admin/OpResult.jsp?op=default&ret=false&words="+URLEncoder.encode("增加管理员出错!请联系管理员", "UTF-8") );
}
else if("DelAdmin".equals(op)){
Long aid=Long.valueOf(request.getParameter("aid"));
AdminDAO dao=DAOFactory.getAdminDAO();
boolean ret1=dao.delAdmin(aid);
if(ret1)
response.sendRedirect("../admin/AdminList.jsp");
else
response.sendRedirect("../admin/OpResult.jsp?op=default&ret=false&words="+URLEncoder.encode("删除管理员出错!请联系管理员", "UTF-8"));
}else if("EditAdmin".equals(op)){
Long aid=Long.valueOf(request.getParameter("aid"));
String oldpwd=request.getParameter("oldpwd");
String pwd=request.getParameter("pwd");
String username=request.getParameter("username");
AdminDAO dao=DAOFactory.getAdminDAO();
if(dao.checkPwd(username, oldpwd)!=true){
response.sendRedirect("../admin/OpResult.jsp?op=default&ret=false&words="+URLEncoder.encode("旧密码错误", "UTF-8"));
return;
}
synchronized(mutex1){
Admin admin=dao.findAdmin(aid);
admin.setA_user(username);
admin.setA_pass(pwd);
boolean ret1=dao.updateAdmin(admin);
if(ret1)
response.sendRedirect("../admin/AdminList.jsp");
else
response.sendRedirect("../admin/OpResult.jsp?op=default&ret=false&words="+URLEncoder.encode("编辑管理员出错!请联系管理员", "UTF-8"));
}
}
}
}
package com.survey.dao.mssqlimpl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.sql.RowSet;
import com.survey.dao.AdminDAO;
import com.survey.dto.Admin;
import com.survey.dto.Config;
import com.survey.dto.Survey;
import com.survey.sql.ConnectionFactory;
import com.survey.sql.SQLCommand;
public class AdminDAOimpl implements AdminDAO{
public boolean addAdmin(Admin admin) {
Connection conn = ConnectionFactory.getConnection();
PreparedStatement pstmt=null;
String sql = "insert into admins(a_user,a_pass) values(?,?)";
try {
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, admin.getA_user());
pstmt.setString(2, admin.getA_pass());
return pstmt.executeUpdate()==1?true:false;
} catch (SQLException e) {
e.printStackTrace();
return false;
}finally{
SQLCommand.close(pstmt);
SQLCommand.close(conn);
}
}
public boolean checkPwd(String username, String pwd) {
SQLCommand cmd=new SQLCommand();
String realpwd=cmd.queryScalar("select a_pass from admins where a_user='"+username+"'");
if(pwd.equals(realpwd))
return true;
else
return false;
}
public boolean delAdmin(long a_id) {
SQLCommand cmd = new SQLCommand();
int ret = cmd.executeSQL("delete from admins where a_id="+a_id);
if(ret==1)
return true;
else
return false;
}
public Admin findAdmin(long a_id) {
SQLCommand cmd = new SQLCommand();
RowSet rs = cmd.queryRowSet("select * from admins where a_id="+a_id);
Admin admin=new Admin();
try {
if (rs.next()) {
admin.setA_id(rs.getLong("a_id"));
admin.setA_user(rs.getString("a_user"));
admin.setA_pass(rs.getString("a_pass"));
return admin;
}
return null;
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
public Admin findAdmin(String username) {
SQLCommand cmd = new SQLCommand();
RowSet rs = cmd.queryRowSet("select * from admins where a_user='"+username+"'");
Admin admin=new Admin();
try {
if (rs.next()) {
admin.setA_id(rs.getLong("a_id"));
admin.setA_user(rs.getString("a_user"));
admin.setA_pass(rs.getString("a_pass"));
return admin;
}
return null;
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
public List listAllAdmin() {
SQLCommand cmd=new SQLCommand();
RowSet rs=cmd.queryRowSet("select * from admins");
List<Admin> list=new ArrayList<Admin>();
Admin admin;
try {
while(rs.next()){
admin=new Admin();
admin.setA_id(rs.getLong("a_id"));
admin.setA_user(rs.getString("a_user"));
admin.setA_pass(rs.getString("a_pass"));
list.add(admin);
}
return list;
} catch (SQLException e) {
e.printStackTrace();
return null;
}
}
public boolean updateAdmin(Admin admin) {
Connection conn = ConnectionFactory.getConnection();
PreparedStatement pstmt=null;
String sql = "UPDATE admins set a_user=?,a_pass=? where a_id=?";
try {
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, admin.getA_user());
pstmt.setString(2, admin.getA_pass());
pstmt.setLong(3, admin.getA_id());
return pstmt.executeUpdate()==1?true:false;
} catch (SQLException e) {
e.printStackTrace();
return false;
}finally{
SQLCommand.close(pstmt);
SQLCommand.close(conn);
}
}
}