登录功能_实现_前台页面
$(function (){ //1.给登录按钮绑定点击事件 $("#btn_sub").click(function (){ //2.发送ajax请求,提交表单数据 $.post("loginServlet",$("loginForm").serialize(),function (data){ //data:{flag:false,errorMsg:''} if (data.flat){ //登录成功 location.href = "index.html"; }else { //登录失败 $("#errorMsg").html(data.errorMsg); } }); }); });
登录功能_姓名提示
登录功能_姓名提示
header.html:
<script> $(function (){ $.get("findUserServlet",{},function (data){ //{uid:1,name:'李四'} var msg = "欢迎回来,"+data.name $("#span_username").html(msg); }); }); </script>
package cn.itcast.travel.web.servlet; import com.fasterxml.jackson.databind.ObjectMapper; import javax.servlet.*; import javax.servlet.http.*; import javax.servlet.annotation.*; import java.io.IOException; @WebServlet("/findUserServlet") public class FindUserServlet extends HttpServlet { @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //从session中获取登录用户 Object user = request.getSession().getAttribute("user"); //将user写会客户端 ObjectMapper mapper = new ObjectMapper(); response.setContentType("application/json;charset=utf-8"); mapper.writeValue(response.getOutputStream(),user); } @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } }
翻译
搜索
复制
标签:function,功能,登录,servlet,import,data,response,页面 From: https://www.cnblogs.com/x3449/p/17167317.html