zoukankan      html  css  js  c++  java
  • JAVA日报

    javaweb(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);
    }
    }

  • 相关阅读:
    bat %n 判断传入的参数值和使用注意
    git ls-remote url,判断 url 是否存在
    bat 读取 ini 文件
    bat 提示窗口,带换行
    python .pth 文件 和 site 模块
    python Pillow 图片处理模块,好强大有没有
    Python 获取 exe 的 icon 并且保存
    git 判断路径是否是 git 仓库
    Python 去除字符串中的空行
    Android平台程序崩溃的类型及原因列举
  • 原文地址:https://www.cnblogs.com/mumulailai/p/14912433.html
Copyright © 2011-2022 走看看