登录功能、登录功能姓名提示
登录功能_前台实现
<script>
// 登录按钮绑定单机事件
$(function () {
$("#btn_sub").click(function () {
// 发送ajax请求,提交表单数据
$.post("loginServlet",$("loginForm").serialize(),function (data) {
if (data.flag){
//登录成功
location.href="index.html";
}else {
//登录失败
$("#errorMsg").html(data.errorMsg);
}
})
});
})
登录功能姓名提示
index.html页面中用户姓名的提示信息功能
<script>
$(function() {
$.get("findUserServlet",{},function(data){
var msg = "欢迎回来,"+data.name;
$("#span_username").html(msg)
})
})
@WebServlet("/findUserServlet")
public class FindUserServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 从session中获取登录用户
Object user = req.getSession().getAttribute("user");
// 将user写回客户端
ObjectMapper mapper = new ObjectMapper();
resp.setContentType("application/json;charset=utf-8");
mapper.writeValue(resp.getOutputStream(),user);
}
}