package com.webtier;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.tool.Chinese;
import com.tool.JDBConnection;
/**
* Servlet implementation class for Servlet: UserSevlet
*
*/
public class UserSevlet extends javax.servlet.http.HttpServlet implements
javax.servlet.Servlet {
private int method;
private JDBConnection connection = new JDBConnection();
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("gb2312");
this.method = Integer.parseInt(request.getParameter("method"));
switch (method) {
case 0: {
this.addUser(request, response); //用户注册
break;
}
case 1: {
this.queryUserOne(request, response); //查询用户是否存在
break;
}
case 2: {
this.checkUser(request, response); //用户登录操作
break;
}
case 3: {
this.queryUser(request, response); //用户查询操作
break;
}
case 4: {
this.deleteUser(request, response); //用户删除
break;
}
case 5: {
this.queryontUser(request, response); //普通用户查询
break;
}
case 6: {
this.updateUser(request, response); //修改用户
break;
}
}
}
//修改用户
public void updateUser(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String user = request.getParameter("user");
String password = request.getParameter("password");
String name = request.getParameter("name");
String sex = request.getParameter("sex");
String bron = request.getParameter("bron");
String emailAddress = request.getParameter("emailAddress");
String sql = "update tb_user set password='" + password + "',name='"
+ name + "',sex='" + sex + "',bron='" + bron
+ "',emailAddress='" + emailAddress + "' where user='" + user
+ "'";
connection.executeUpdate(sql);
connection.closeConnection();
RequestDispatcher requestDispatcher = request
.getRequestDispatcher("index.jsp");
requestDispatcher.forward(request, response);
}
//普通用户查询
public void queryontUser(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String user=Chinese.toChinese(request.getParameter("user"));
String sql="select * from tb_user where user='"+user+"'";
System.out.print(sql);
ResultSet rs=connection.executeQuery(sql);
request.setAttribute("rs",rs);
connection.closeConnection();
RequestDispatcher requestDispatcher = request
.getRequestDispatcher("user_update.jsp");
requestDispatcher.forward(request, response);
}
//用户删除
public void deleteUser(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String id=request.getParameter("id");
String sql="delete from tb_user where id='"+id+"'";
connection.executeUpdate(sql);
connection.closeConnection();
this.queryUser(request, response);
}
//用户查询操作
public void queryUser(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String sql = "select * from tb_user where admin=0";
ResultSet rs = connection.executeQuery(sql);
request.setAttribute("rs", rs);
RequestDispatcher requestDispatcher = request
.getRequestDispatcher("user_query.jsp");
requestDispatcher.forward(request, response);
}
//用户登录操作
public void checkUser(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String user = request.getParameter("user");
String password = request.getParameter("password");
String querypassword = "";
String message = "";
String admin = "";
String sql = "select * from tb_user where user='" + user + "'";
ResultSet rs = connection.executeQuery(sql);
try {
while (rs.next()) {
querypassword = rs.getString("password");
admin = rs.getString("admin");
}
} catch (SQLException e) {
e.printStackTrace();
}
if (querypassword == null || querypassword.equals("")) {
message = "您输入的账号不存在";
} else if (!querypassword.equals(password)) {
message = "您输入的密码不正确";
} else {
message = "您登录成功";
request.setAttribute("user", user);
request.setAttribute("admin", admin);
}
request.setAttribute("message", message);
RequestDispatcher requestDispatcher = request
.getRequestDispatcher("dealWith.jsp");
requestDispatcher.forward(request, response);
}
// 查询用户是否存在
public void queryUserOne(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String user = request.getParameter("user");
String message = "可以使用";
String sql = "select * from tb_user where user='" + user + "'";
ResultSet rs = connection.executeQuery(sql);
try {
while (rs.next()) {
message = "不可以使用";
}
} catch (SQLException e) {
e.printStackTrace();
}
request.setAttribute("message", message);
RequestDispatcher requestDispatcher = request
.getRequestDispatcher("register.jsp");
requestDispatcher.forward(request, response);
}
// 用户注册
public void addUser(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String user = request.getParameter("user");
String password = request.getParameter("password");
String name = request.getParameter("name");
String sex = request.getParameter("sex");
String bron = request.getParameter("bron");
String emailAddress = request.getParameter("emailAddress");
boolean flag = true;
String sql = "";
sql = "select * from tb_user where user='" + user + "'";
ResultSet rs = connection.executeQuery(sql);
try {
while (rs.next()) {
flag = false;
}
} catch (SQLException e) {
e.printStackTrace();
}
if (flag) {
sql = "insert into tb_user (user,password,name,sex,bron,emailAddress,admin) value ('"
+ user
+ "','"
+ password
+ "','"
+ name
+ "','"
+ sex
+ "','" + bron + "','" + emailAddress + "',0)";
connection.executeUpdate(sql);
request.setAttribute("message", "用户注册成功!!!");
}
connection.closeConnection();
RequestDispatcher requestDispatcher = request
.getRequestDispatcher("dealWith.jsp");
requestDispatcher.forward(request, response);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}