java_Web 实战05
java_Web 实战05
本次我们逐一的分析每一个功能,首先对于查看个人信息的功能由于我们将数据存储在session中可以直接使用session中的数据所以不用访问在servlet,中访问数据库进行查询操作,可以直接在页面中进行处理
<%--
Created by IntelliJ IDEA.
User: 16029
Date: 2024/12/9
Time: 14:57
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page isELIgnored="false"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%-- 顾客--%>
<c:if test="${sessionScope.type.equals(\"0\")}">
<p> 顾客ID ${shopper.userId} </p>
<p> 姓名 ${shopper.userName}</p>
<p> 性别
<c:if test="${sessionScope.shopper.sex.equals(\"0\")}">
女
</c:if>
<c:if test="${sessionScope.shopper.sex.equals(\"1\")}">
男
</c:if>
</p>
<p> 身份证号 ${shopper.idNumber} </p>
<p> 手机 ${shopper.phone}</p>
<p> 家庭住址 ${shopper.user} </p>
</c:if>
<%-- 经纪人--%>
<c:if test="${sessionScope.type.equals(\"1\")}">
查看个人信息
<p> 工号 ${agent.agentId} </p>
<p> 房产经纪人姓名 ${agent.agentName}</p>
<p> 家庭住址 ${agent.agentAddress} </p>
<p> 手机 ${agent.phone} </p>
</c:if>
</body>
</html>
如此可以成功的实现对于个人页面信息的展示
对于第二个功能个人密码的修改.我们首先要录入原来的密码和更改后的密码.然后我这里使用message的方式来实现就是在后端判断之后发送message到前端,最后实现提示前端页面修改的状态.
<%--
Created by IntelliJ IDEA.
User: 16029
Date: 2024/12/9
Time: 22:47
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
<form action="/Homes/ChangePassword" method="post">
<p>原密码 <input type="password" name="oldPassword"></p>
<p>新密码 <input type="password" name="newPassword"></p>
<p> <input type="submit" value="提交"></p>
</form>
</head>
<body>
</body>
</html>
package com.home.servlet;
import com.home.pojo.User;
import com.home.service.UserService;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet("/ChangePassword")
public class ChangePasswordServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String oldPassword=req.getParameter("oldPassword");
String newPassword=req.getParameter("newPassword");
User user=(User)req.getSession().getAttribute("user");
if(!oldPassword.equals(user.getPassword())||newPassword.equals(user.getPassword())){
resp.sendRedirect("changePassword.jsp");
}
else {
UserService userService=new UserService();
user.setPassword(newPassword);
userService.updatePassword(user);
resp.sendRedirect("worker.jsp");
}
}
}