使用ajax来进行登录验证
2021-02-11 16:15
标签:button value ons oca post long except orm ram servlet: jsp里面的是重点: 使用ajax来进行登录验证 标签:button value ons oca post long except orm ram 原文地址:https://www.cnblogs.com/dabu/p/13040243.html 1 @WebServlet("/login.do")
2 public class AjaxLoginServlet extends HttpServlet {
3 private static final long serialVersionUID = 1L;
4
5 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
6
7 ResultMsg rm = new ResultMsg();
8 PrintWriter out = response.getWriter();
9 Gson gson = new Gson();
10
11 String name = request.getParameter("userName");
12 String pass = request.getParameter("userPass");
13
14 //非空校验
15 if(isempty(name)||isempty(pass)){
16 rm.setMsg("user pass is null");
17 rm.setResult("0002");
18
19 out.println(gson.toJson(rm));
20 return;
21 }
22
23 //去数据库确认一下用户是不是存在,并且登录成功
24 if("dabu".equals(name)&&"123".equals(pass)){
25 rm.setResult("0000");
26 }else {
27 rm.setMsg("user pass is wrong");
28 rm.setResult("0001");
29 }
30
31 out.println(gson.toJson(rm));
32
33 //form表单 a标签; 转发或者重定向
34 //ajax的请求; out.println(json对象);
35 }
36
37 //判断一个字符串是不是为空
38 private boolean isempty(String str){
39 return null==str||"".equals(str.trim());
40 }
41
42 protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
43 // TODO Auto-generated method stub
44 doGet(request, response);
45 }
1
2
3
4
5
6
15
16
上一篇:4.K均值算法--应用
下一篇:小探究:CSSの渐变