• 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);
    }
    }

  • 相关阅读:
    第二次作业——结对项目之需求分析与原型模型设计
    Git进行代码管理的心得
    安卓开发环境演变
    软件工程的实践项目的自我目标
    若一切能重来
    随堂练习——Rational rose
    第五次软件个人作业
    第三次作业——结对编程
    第二次作业——结对项目之需求分析与原型模型设计
    软工实践练习——使用Git进行代码管理
  • 原文地址:https://www.cnblogs.com/mumulailai/p/14906021.html
Copyright © 2020-2023  润新知