Servlet + Ajax简单demo

GET

AjaxServlet.java

@WebServlet("/ajax")
public class AjaxServlet extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws  ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8" );
        PrintWriter out = response.getWriter();
        String old = request.getParameter("username" );
        if  (old ==  null  || old.length() ==  0 ) {
            out.println("用户名不能为空" );
        } else  {
            if  (old.equals( "admin" )) {
                out.println("用户名 ["  + old +  "] 已经存在,请使用其他用户名" );
            } else  {
                out.println("用户名 ["  + old +  "] 尚未存在,可以使用该用户名注册" );
            }
        }
    }
}

ajax.html



    ajax
    





POST

LoginServlet.java

@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    public static final String DBDRIVER = "com.mysql.jdbc.Driver";
    public static final String DBURL = "jdbc:mysql://localhost:3306/login?useSSL=false";
    public static final String DBUSER = "root";
    public static final String DBPASS = "123";
    boolean flag = false;
    String username = null;

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Connection conn = null;  //数据库连接
        PreparedStatement pstmt = null;    //数据库预处理操作
        ResultSet rs = null;    //查询要处理结果集

        try{
            Class.forName(DBDRIVER);
            conn = DriverManager.getConnection(DBURL,DBUSER,DBPASS);

            String sql = "select username from user where username = ? and password = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, request.getParameter("username"));
            pstmt.setString(2, request.getParameter("password"));

            rs = pstmt.executeQuery();
            if(rs.next()){          //如果有数据就执行
                flag = true;        //表示登陆成功
                username = rs.getString(1);
            }
            System.out.println(username);
        } catch (Exception e){
            e.printStackTrace();
        } finally{
            try{
                rs.close();
                pstmt.close();
                conn.close();
            }catch(Exception e){
                e.printStackTrace();
            }
        }

        response.setContentType("text/html; charset=UTF-8");
        PrintWriter out=response.getWriter();
        if(flag == true) {
            out.println("success");
        } else {
            out.print("error");
        }
        out.flush();
        out.close();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}

login.html




    
    Ajax
    



用户名:
密码:

你可能感兴趣的:(Servlet + Ajax简单demo)