JAVA日报
用户管理系统开发(servlet)
servlet主要接收前台操作 进行分析后对数据库进行操作 返回前台页面等
package servlet;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import bean.*;
import dao.*;
@WebServlet("/UserServlet")
public class UserServlet extends HttpServlet{
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html; charset=utf-8");
String method = request.getParameter("method");
if ("add".equals(method)){
try {
add(request,response);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (ServletException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}else if("del".equals(method)){
try {
del(request,response);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (ServletException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}else if("gai".equals(method)){
try {
gai(request,response);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (ServletException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
else if("enter".equals(method)){
try {
enter(request,response);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (ServletException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (SQLException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, ClassNotFoundException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html; charset=utf-8");
String UserID=request.getParameter("UserID");
String UserName=request.getParameter("UserName");
String sex=request.getParameter("sex");
String idNumber=request.getParameter("idNumber");
String Phone=request.getParameter("Phone");
String User=request.getParameter("User");
String Password=request.getParameter("Password");
User user=new User(UserID,UserName,sex,idNumber,Phone,User,Password);
UserDao userdao=new UserDao();
userdao.addC(user);
request.getRequestDispatcher("enter.jsp").forward(request,response);
}
protected void del(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, ClassNotFoundException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html; charset=utf-8");
String UserID=request.getParameter("UserID");
System.out.println(UserID);
UserDao userdao=new UserDao();
userdao.delC(UserID);
request.getRequestDispatcher("user.jsp").forward(request,response);
}
protected void gai(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, ClassNotFoundException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html; charset=utf-8");
String role=request.getParameter("role");
String id1=request.getParameter("id1");
String id3=request.getParameter("UserID");
System.out.println(id3);
UserDao userdao=new UserDao();
userdao.gaiC(role,id1,id3);
request.getRequestDispatcher("user.jsp").forward(request,response);
}
protected void enter(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, ClassNotFoundException, SQLException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html; charset=utf-8");
String role=request.getParameter("role");
String id=request.getParameter("UserID");
String password=request.getParameter("Password");
ArrayList<User> users=new UserDao().getAlluserinfos();
int temp=0;
for(User user:users) {
if(user.get("UserID").equals(id)) {
temp++;
if(user.get("Password").equals(password)) {
request.setAttribute("message", "密码正确");
}else {
request.setAttribute("message", "密码错误");
}
}
}
if(temp==0) {
request.setAttribute("message", "没有该ID");
}
request.getRequestDispatcher("enter.jsp").forward(request,response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}