简单前后台交互、传值、验证(JSP+Servlet)

一个JSP文件 index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>


   
    


$END$
用户名:
密码:

一个Servlet文件 MyServlet.java

package controller;

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 java.io.IOException;
import java.io.PrintWriter;
import java.util.HashSet;
import java.util.Set;

@WebServlet(name = "MyServlet", urlPatterns = "/myServlet.do")
public class MyServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        String userName = request.getParameter("userName");
        Set nameSet = new HashSet() {
            {
                add("zxy");
                add("ldh");
                add("lm");

            }
        };
        PrintWriter writer=response.getWriter();
        if (nameSet.contains(userName)) {
            writer.write("此用户名已被占用");
        } else {
            writer.write("此用户名可用");
        }

       // response.getWriter().write("\n完成操作");//这句是在前端显示的
    }
}

目前的功能就是 

1.鼠标脱离input元素后调用haha函数 来进行检查 查看是否可用并显示√或者×

2.里面有一个nameSet集合 就不可重名

 

。。我在继续改进

你可能感兴趣的:(ajax)