JSP(Java Server Pages)几个内置对象的使用(以登录注册为例)

代码实现的功能

        1、假定用户名:系统管理员,密码为123可以登录成功,其他用户拒绝登录,正确实现页面                的跳转和显示

        2、在session中保存用户信息并跳转至index.jsp页面中,并在该页面显示用户名,注:需要                  对session进行是否为空的判断

              index.jsp页面中提供注销按钮,点击注销按钮之后页面跳转至userLogin.jsp登录页面

        3、在cookie中保存用户名

        4、使用application实现统计登录人数

login.jsp

<%
    String username1 = "";
    Cookie[] cookies = request.getCookies();
    for(int i=0;i<cookies.length;i++){
        if(cookies[i].getName().equals("username")){
            username1 = cookies[i].getValue();
        }
    }
%>
    <form action="loginScuess.jsp" method="get">
        <p>
            用户名:<input value="<%=username1%>" name="username" type="text" id="username">
        </p>
        <p>
            密码:<input type="password" name="pwd" value="" id="pwd">
        </p>
        <p>
            <input value="提交" name="" type="submit">
            <input value="重置" name="" type="reset">
        </p>
    </form>
<%
    //String mess = (String)request.getAttribute("mess");
    String mess = request.getParameter("info");
    if(mess != null){
%>
    登录结果:<%=mess%>
<%
    }
%>

loginsuccess.jsp

   <%
        String username = request.getParameter("username");
        String pwd = request.getParameter("pwd");
        //使用转发实现页面跳转
//        if(username.equals("admin") && pwd.equals("123")){
//            request.setAttribute("mess","欢迎登录!");
//            request.getRequestDispatcher("index.jsp").forward(request,response);
//        }else {
//            request.setAttribute("mess","您没有权限登录!");
//            request.getRequestDispatcher("login.jsp").forward(request,response);
//        }
        //使用redirect实现页面跳转
        if(username.equals("admin") && pwd.equals("123")){
            String info  = "登录成功!";
            info = URLEncoder.encode(info,"utf-8");
            //使用session保存用户信息并跳转至index.jsp
            session.setAttribute("username",username);
            //response.sendRedirect("index.jsp?info="+info);
            //使用cookie保存用户名
            Cookie cookie = new Cookie("username",username);
            cookie.setMaxAge(60*60);
            cookie.setPath("/");
            response.addCookie(cookie);
            response.sendRedirect("login.jsp");
            int count = 0;
            Object obj = application.getAttribute("count");
            if(obj !=null){
                count = (int)obj;
            }
            if(count == 0){
                application.setAttribute("count",1);
            }else {
                count++;
                application.setAttribute("count",count);
            }
        }else {
            String info = "登录失败";
            info = URLEncoder.encode(info,"utf-8");
            response.sendRedirect("login.jsp?info="+info);
        }
    %>
    用户名:<%=username%>
    密码:<%=pwd%>

首页代码——index.jsp

欢迎登录
  <%
        String username = (String) session.getAttribute("username");
    %>
  欢迎您!<%=username%>
<%
  int count = 0;
  Object obj = application.getAttribute("count");
  if (obj != null) {
    count = (int)obj;
  }

%>
在线人数:<%=count%>

注销页代码——loginOut

<%
    session.invalidate();
    int count  = 0;
    Object obj = application.getAttribute("count");
    if(obj != null){
        count = (int)obj;
    }
    count--;
    application.setAttribute("count",count);
    response.sendRedirect("login.jsp");
%>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值