String msg = request.getParameter("msg");if(msg!=null&&msg.equals("msg")){ request.setAttribute("msg", "msg"); }
时间: 2023-11-20 15:32:07 浏览: 46
这段代码是Java Servlet中的一段代码,用于获取HTTP请求中名为 "msg" 的参数并将其存储到请求对象的属性中。如果该参数存在且等于字符串 "msg",则会将一个名为 "msg" 的属性添加到请求对象中,属性值为字符串 "msg"。此代码片段可能用于接受用户输入的信息并对其进行处理,或者将消息传递给其他部分进行处理。
相关问题
public String loginIn(HttpServletRequest request, Model model){ //获取用户名与密码 String username = request.getParameter("username"); String password = request.getParameter("password"); String code=request.getParameter("captcha"); // String type=request.getParameter("type"); //判断验证码是否正确(验证码已经放入session) HttpSession session = request.getSession(); String realCode = (String)session.getAttribute("VerifyCode"); if (!realCode.toLowerCase().equals(code.toLowerCase())){ model.addAttribute("msg","验证码不正确"); return "login"; }else{ //验证码正确则判断用户名和密码 // if(type.equals("1")){//管理员信息 model.addAttribute("msg","12345"); //用户名和密码是否正确 Admin admin=adminService.queryUserByNameAndPassword(username,password); ReaderInfo readerInfo=readerService.queryUserInfoByNameAndPassword(username,password); if(admin!=null){//该用户不存在 session.setAttribute("user",admin); session.setAttribute("type","admin"); } else if (admin==null && readerInfo==null){ model.addAttribute("msg","用户名或密码错误"); return "login"; } else if(readerInfo!=null){ session.setAttribute("user",readerInfo); session.setAttribute("type","reader"); } return "index"; } }
这段代码是一个登录功能的代码,接收用户输入的用户名、密码和验证码,然后进行验证。首先,通过HttpServletRequest对象获取到用户名、密码和验证码。然后,从session中获取真实的验证码,并与用户输入的验证码进行比较。如果验证码不正确,返回登录页面并显示错误信息。如果验证码正确,则继续验证用户名和密码。
在验证用户名和密码之前,首先判断用户类型,这里只展示了管理员和读者两种类型。如果用户类型为管理员,则调用adminService的queryUserByNameAndPassword方法来查询管理员信息,如果查询结果不为空,则将用户信息和类型存入session中。如果用户类型为读者,则调用readerService的queryUserInfoByNameAndPassword方法来查询读者信息,同样将查询结果和类型存入session中。
最后,跳转到首页。
需要注意的是,这段代码存在一些安全性和逻辑性问题,例如没有对用户名、密码进行加密处理、没有检查用户名和密码是否为空等。在实际开发中需要进行相应的处理。
public class ManagerLogin extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.getWriter().append("Served at: ").append(request.getContextPath()); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 设置头文件 response.setContentType("application/json; charset=utf8"); PrintWriter out = response.getWriter(); // 获取账号密码 String user = request.getParameter("user"); String psw = request.getParameter("psw"); // 设置响应map HashMap<String, Object> hashMap = new HashMap<String, Object>(); Manager manager = new Manager(); String result = null; try { result = manager.login(user, psw); } catch (ClassNotFoundException | SQLException e) { e.printStackTrace(); } if (result.equals("1")) { HttpSession session = request.getSession(); session.setAttribute("manager", user); session.setAttribute("manager_first", "1"); // 登录 hashMap.put("code", 0); hashMap.put("msg", "登录成功"); hashMap.put("url", request.getContextPath() + "/manager/01nav.jsp"); } else { hashMap.put("code", 1); hashMap.put("msg", result); } // response.sendRedirect(request.getContextPath() +"/test.jsp"); JSONObject json = JSONObject.fromObject(hashMap); out.write(json.toString()); }
这段代码是一个Java Servlet,处理了前端通过Ajax POST请求发送的登录数据。在doPost方法中,首先设置了响应头部的Content-Type为application/json; charset=utf8,并获取了请求中的账号和密码。接着,创建了一个HashMap对象,用于存储响应数据。然后,调用Manager类的login方法,返回一个结果字符串,如果结果字符串为"1",表示登录成功,将登录状态保存在session中,并将code设置为0,msg设置为"登录成功",url设置为"/manager/01nav.jsp"。否则,将code设置为1,msg设置为结果字符串。最后,将HashMap对象转换成JSON格式,通过PrintWriter将响应数据返回给前端。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)