javaweb项目实现用户进行预约时对你弹窗提醒,离线登录后也可以进行弹窗

我做的项目实现的是当用户预约你,就是对数据库进行修改时或者增加时,对你进行消息弹窗提示,当你不在线,再次登录时。系统检测到你登录,就会进行消息弹窗提示。前提是服务器不会断开连接。

基本思路是:当对数据库进行操作时,将相应的消息存到一个数组中;

效果如下:

弹窗的html代码:

<div class="col-sm-3 col-xs-8 messageTC" style="opacity:1;background:#fff;z-index:999;display:none;height:200px;position: fixed;border:1px solid #e2dbdb;border-radius: 10px; bottom: 0;right: 0;border">
		<button type="button" class="close" οnclick="javascript:$(this).parent().slideUp()" aria-label="Close"><span aria-hidden="true">×</span></button>
		<h3 class="text-info">消息通知</h3>
		<hr>
		<textarea class="form-control messageTextTC" rows="3" readonly></textarea>
	</div>

相应的javascript代码:

<c:if test="${foreigner!=null||normaluser!=null }">
<script type="text/javascript">
	$(function(){
	setInterval("messagePost()",2000);
	});
	function messagePost(){
		$.post("${pageContext.request.contextPath}/message","",function(data){
			var dat = eval("("+data+")");
			if(dat.length>0){
				$(".messageTC").slideDown();
			}
			for(var i=0;i<dat.length;i++){
				var text="用户 "+dat[i].name+" "+dat[i].message+"\n";
				$(".messageTextTC").prepend(text);
			}
		});
	}
</script>
</c:if>

最重要的一个工具类:

public class MessageUtils {
	private static ArrayList<Message> telmessage = new ArrayList<>();
	public static void addMessage(Message mes) {
		telmessage.add(mes);
	}
	public static String getMessage(String tel) {
		ArrayList<Message> arr = new ArrayList<>();
		for(int i=0;i<telmessage.size();i++) {
			if(telmessage.get(i).getTel().equals(tel)) {
				arr.add(telmessage.get(i));
			}
		}
		telmessage.removeAll(arr);
		JSONArray json = JSONArray.fromObject(arr);
		return json.toString();
	}
}

相应的消息类:

public class Message {
    private String name;
    private String message;
    private String tel;
    public String getTel() {
        return tel;
    }
    public void setTel(String tel) {
        this.tel = tel;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getMessage() {
        return message;
    }
    public void setMessage(String message) {
        this.message = message;
    }
}
在对数据库相应的操作的时候,顺便将相应的消息封装到Message中,然后添加到util中,最后再发到用户中就可以了

  • 0
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
下面是基于MVC模式的JavaWeb项目中使用RSA算法进行管理员登录的示例代码: (1)Model层代码 ```java public class Admin { private String username; private String password; // getters and setters // 使用RSA算法进行密码加密 public String encryptPassword(String publicKey) throws Exception { byte[] encryptedBytes = null; try { PublicKey publicKeyObj = getPublicKey(publicKey); Cipher cipher = Cipher.getInstance("RSA"); cipher.init(Cipher.ENCRYPT_MODE, publicKeyObj); byte[] inputBytes = password.getBytes(); encryptedBytes = cipher.doFinal(inputBytes); } catch (Exception e) { e.printStackTrace(); } return new String(Base64.getEncoder().encode(encryptedBytes)); } // 获取公钥对象 private PublicKey getPublicKey(String publicKey) throws Exception { byte[] keyBytes = Base64.getDecoder().decode(publicKey.getBytes()); X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes); KeyFactory keyFactory; PublicKey publicKeyObj = null; keyFactory = KeyFactory.getInstance("RSA"); publicKeyObj = keyFactory.generatePublic(keySpec); return publicKeyObj; } } ``` (2)View层代码 ```jsp <%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>管理员登录</title> </head> <body> <h1>管理员登录</h1> <form action="${pageContext.request.contextPath}/admin/login" method="post"> <label>用户名:<input type="text" name="username"></label><br> <label>密码:<input type="password" name="password"></label><br> <input type="submit" value="登录"> </form> </body> </html> ``` (3)Controller层代码 ```java public class AdminController extends HttpServlet { private AdminService adminService = new AdminService(); @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String username = req.getParameter("username"); String password = req.getParameter("password"); Admin admin = new Admin(); admin.setUsername(username); admin.setPassword(password); try { // 调用Service层方法进行登录验证 boolean loginSuccess = adminService.login(admin); if (loginSuccess) { // 登录成功,跳转到管理后台首页 resp.sendRedirect(req.getContextPath() + "/admin/home"); } else { // 登录失败,返回错误提示信息到前页面 req.setAttribute("errorMsg", "用户名或密码错误,请重试!"); req.getRequestDispatcher("/WEB-INF/views/admin/login.jsp").forward(req, resp); } } catch (Exception e) { e.printStackTrace(); // 出现异常,返回错误提示信息到前页面 req.setAttribute("errorMsg", "登录失败,请重试!"); req.getRequestDispatcher("/WEB-INF/views/admin/login.jsp").forward(req, resp); } } } ``` (4)Service层代码 ```java public class AdminService { // 随机生成RSA公钥和私钥,存储在服务器 private String publicKey = ""; private String privateKey = ""; public AdminService() { try { KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA"); keyPairGenerator.initialize(512); KeyPair keyPair = keyPairGenerator.generateKeyPair(); publicKey = new String(Base64.getEncoder().encode(keyPair.getPublic().getEncoded())); privateKey = new String(Base64.getEncoder().encode(keyPair.getPrivate().getEncoded())); } catch (Exception e) { e.printStackTrace(); } } // 登录验证 public boolean login(Admin admin) throws Exception { // 获取加密后的密码 String encrypedPassword = admin.encryptPassword(publicKey); // 从数据库中查询是否存在该管理员 AdminDao adminDao = new AdminDao(); Admin adminInDB = adminDao.findByUserName(admin.getUsername()); if (adminInDB == null) { return false; // 用户名不存在,登录失败 } // 如果密码匹配,则登录成功 if (encrypedPassword.equals(adminInDB.getPassword())) { return true; } else { return false; } } } ``` (5)Dao层代码 ```java public class AdminDao { // 根据用户查询管理员 public Admin findByUserName(String username) { String query = "SELECT * FROM admin WHERE username = ?"; try (Connection conn = DBUtil.getConnection(); PreparedStatement stmt = conn.prepareStatement(query)) { stmt.setString(1, username); ResultSet rs = stmt.executeQuery(); if (rs.next()) { Admin admin = new Admin(); admin.setUsername(rs.getString("username")); admin.setPassword(rs.getString("password")); return admin; } } catch (SQLException e) { e.printStackTrace(); } return null; } } ``` 需要注意的是,上述代码中的EncryptUtil工具类,是用于RSA加密操作的工具类,代码实现可以根据具体要求自行完成。此外,为了更好地理解MVC模式的实现逻辑,以上示例代码中未使用任何额外的框架,仅使用了Servlet、JSP和JDBC等基础技术。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

梦幻D开始

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值