在进行完文件的配置之后,就要按照数据库封装bean,放在pojo层中,然后创建相应的mapper.xml文件(创建时要用/间隔)
之后根据项目要求,搭建主界面。
根据不同角色的功能,搭建各自的界面,以其中一个为例
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>顾客应用系统</title>
<style>
.form {
width: 600px;
margin: 0 auto;
/*border: 1px solid red;*/
}
.form table {
margin: 0 auto;
}
.form table tr td {
width: 100px;
height: 30px;
border: 1px solid #000;
text-align: center;
}
button {
display: block;
margin-top: 10px;
padding: 10px;
background-color: #007bff;
color: #fff;
border: none;
border-radius: 3px;
cursor: pointer;
}
</style>
</head>
<body>
<h1 style="text-align: center">顾客应用系统</h1>
<!-- 其余代码不变 -->
<div class="form">
<table border="1px" cellspacing="0" width="600px">
<tr>
<th>编号</th>
<th>功能</th>
</tr>
<tr>
<td>1</td>
<td>
<a href="/middle33/customer/viewInfo" target="_blank">
<button>查看个人信息</button>
</a >
</td>
</tr>
<tr>
<td>2</td>
<td><a href="updatePassword.jsp" target="_blank">
<button>修改个人密码</button>
</a >
</td>
</tr>
<tr>
<td>3</td>
<td>
<a href="/middle33/house/selectByStatus1" target="_blank">
<button>浏览房产信息</button>
</a >
</td>
</tr>
<tr>
<td>4</td>
<td>
<a href="selectByCondition1.jsp" target="_blank">
<button>查询房产</button>
</a >
</td>
</tr>
<tr>
<td>5</td>
<td>
<a href="/middle33/house/selectByStatus1" target="_blank">
<button>购买房产</button>
</a >
</td>
</tr>
</table>
</div>
</body>
</html>
其中要求实现不同角色进入不同的界面,所以要在登录部分的代码加上判断条件,根据不同的身份,进入不同界面,这就要使用session,在登录时sql语句执行后返回user对象,将user对象储存在session中,这样在后续的操作中就可以再session中获取user对象,从而得到该用户的角色信息。
import com.wjh.pojo.User;
import com.wjh.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 javax.servlet.http.HttpSession;
import java.io.IOException;
@WebServlet("/loginServlet")
public class loginServlet extends HttpServlet {
UserService userService = new UserService();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
HttpSession session = req.getSession();
//接收数据
String username = req.getParameter("username");
String password = req.getParameter("password");
User user = userService.login(username, password);
String contextPath = req.getContextPath();
//存Session
session.setAttribute("user", user);
session.setAttribute("username", username);
if (user != null) {
if (user.getStatus().equals("顾客")) {
resp.sendRedirect(contextPath + "/customer.html");
} else if (user.getStatus().equals("房产经纪人")) {
resp.sendRedirect(contextPath + "/agent.html");
} else if (user.getStatus().equals("管理员")) {
resp.sendRedirect(contextPath + "/manager.html");
}else {
System.out.println("数据非法!");
}
}
else {
//登陆失败
//存储错误信息
req.setAttribute("login_msg", "用户名或密码错误");
//跳转
req.getRequestDispatcher("/index.jsp").forward(req, resp);
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req, resp);
}
}
标签:分析,练习,javaweb,resp,req,session,user,import,servlet
From: https://www.cnblogs.com/wjhfree/p/18621143