使用Ajax实现登录验证

get提交方式:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"
	contentType="text/html; charset=utf-8"%>



	
		My JSP 'loginAjax.jsp' starting page
		
	

	
		
用户名:
密 码:


public class LoginServlet 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 {
		String un = request.getParameter("username");
		byte[] by = un.getBytes("ISO8859-1");
		un = new String(by, "UTF-8");
		String str = "用户名可以注册";
		System.out.println(un);
		if ("张三".equals(un)) {
			str = "用户名已经注册";
			System.out.println("zs");
		}
		response.setContentType("text/html;charset=utf-8");
		PrintWriter pw = response.getWriter();
		pw.print(str);
		pw.flush();
		pw.close();

	}
}

post提交方式

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"
	contentType="text/html; charset=utf-8"%>




My JSP 'loginAjax.jsp' starting page




	
用户名:
密 码:


public class AjaxTimeServlet2 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");
		String un = request.getParameter("username");
		byte[] by = un.getBytes("ISO8859-1");
		un = new String(by, "UTF-8");
		System.out.println(un);
		String img = "images/MsgSent.gif";
		System.out.println(un);

		if ("张三".equals(un)) {
			img = "images/MsgError.gif";
		}
		response.setContentType("text/html;charset=utf-8");
		PrintWriter pw = response.getWriter();
		pw.write(img);
		pw.flush();
		pw.close();
	}

}


 


你可能感兴趣的:(Ajax)