ajax determine whether the login is successful

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/Mr_hanghang/article/details/102741464

Renderings

Account password is correct successful landing

Here Insert Picture Description

Account or password is incorrect login fails

Here Insert Picture Description

Let's look at the code

index.jsp

<body>
   账户:<input type="text" name="name" id="name"/><br/>
    密码:<input type="text" name="pwd" id="pwd"/><br/>
    <input type="button" value="登录"/><br/>
    <b style="color: red">登陆失败</b>
    <script type="text/javascript">
    
    $(function () {
		
	$(":button").on("click",function(){
	  $.ajax({
	  url:"test",
	  type:"post",
	  data:{
	  name:$("#name").val(),
	  pwd:$("#pwd").val(),
	  },
	  dataType:"text",
	  success:function(data){
	  $("#show").text(data);
	  }
	  });
	});
    
    
    })
    
    </script>
  </body>

servlet code

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class yanServlet 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 {
		PrintWriter out = response.getWriter();
	response.setContentType("text/html;charset=utf-8");
	request.setCharacterEncoding("utf-8");
	String name="张三";
	String pwd="123456admin";
	String ajaxname=request.getParameter("name");
	String ajaxpwd=request.getParameter("pwd");
	System.out.println(ajaxname+":"+ajaxpwd);

	if(name.equals(ajaxname)&&pwd.equals(ajaxpwd)){
		out.print("登陆成功");
	}else{
		out.print("登陆失败");
	}
	out.flush();
	out.close();
	}

}

These are the verification ajax login code, if BUG, ​​please reply to the comments section or private chat! ! !

Guess you like

Origin blog.csdn.net/Mr_hanghang/article/details/102741464