qq_51978250 2021-10-19 14:28 采纳率: 0%
浏览 21

#JSP内置对象#代码错误

为什么我login_check.jsp的

    request.getRequestDispatcher("login_check.jsp").forward(request, response);

代码出错,要实现第二次免登录,但是第一次可以登录,第二次就出问题
全部代码:
login.jsp

<%@ page language="java" import="java.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'login.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>
  
  <body>
   <%!
              String name="";
              String pwd="";
      %>        
      <%
              Cookie [] cookies=request.getCookies();
              
              if(cookies!=null && cookies.length>0){
                  for(Cookie c:cookies){
                      if(c.getName().equals("name")){
                      //    System.out.print(name);
                          name=c.getValue();
                      }
                      if(c.getName().equals("pwd")){
                          pwd=c.getValue();
                      }
                      if(c.getName().equals("name")){
                      System.out.print(name);
                          request.getRequestDispatcher("login_check.jsp").forward(request, response);
                      }
                  }
                  
              }
           %>
        <table border="1" align="center">
        <form name="form1" action="login_check.jsp" method="post">
            <tr>
                <td align="center" colspan="2">用户登陆</td>
            </tr>
            <tr>
                <td>账号:</td>
                <td><input name="name" type="text" value="<%=name%>"></td>
            </tr>
            <tr>
                <td>密码:</td>
                <td><input name="pwd" type="password" value="<%=pwd%>"></td>
            </tr>
            <tr>
                <td>保存登录</td>
                <td><input type="checkbox" name="save" value="yes"></td>
            </tr>
            <tr>
                <td></td>
                <td><input type="submit" value="登陆">
                <input type="reset" value="重置"></td>
            </tr>
        </form>
    </table>
  
  </body>
</html>

login_check.jsp

<%@ page language="java" import="java.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'login_check.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>
      <h1 align="center">登陆操作</h1><hr>
  <body>

   <%
           
           
           String name=request.getParameter("name");
        String pwd=request.getParameter("pwd");
        System.out.print(name+"uh");
        String save=request.getParameter("save");
        String [][] tab={{"iot","iot"},{"admin","admin"}};
        if(save!=null&&save.equals("yes")){

            Cookie cookie1=new Cookie("name",name);
            Cookie cookie2=new Cookie("pwd",pwd);
            cookie1.setMaxAge(600*24);
            cookie2.setMaxAge(600*24);
            response.addCookie(cookie1);
            response.addCookie(cookie2);
        }
        
            if(name==null || pwd==null){
    %>
        <h4 align="center">用户名或密码为空!请重新<a href="login.jsp">登录</a>系统!</h4>
    <%             
            }
            for(int i=0;i<tab.length;i++){
              if(name.equals(tab[i][0])==true && pwd.equals(tab[i][1])==true){
                  session.setAttribute("name",name);
                  response.setHeader("refresh", "3;welcome.jsp");
                  out.print("<h4 align='center'>"+"用户登录成功,三秒后跳转到欢迎页面!"+"</h4>");
                out.print("<h4 align='center'>"+"如果没有跳转,请点击"+"<a href='welcome.jsp'>"+"这里!"+"</a></h3>");
                break;
              }
              else if(i==tab.length-1){        
                  out.println("<h4 align='center'>"+"用户名或密码出错!请重新"+"<a href='login.jsp'>"+"登录"+"</a>"+"系统!"+"</h3>");
              }
              } 
        
        
%>
              
      <%
    if(name==null){
        System.out.println("123");
        Cookie cookies[]=request.getCookies();
        for(int i=0;i<cookies.length;i++)
        {
            cookies[i].setMaxAge(0);
            response.addCookie(cookies[i]);
        }
        response.sendRedirect("login.jsp");
    }
    
    
    
%>
     
  </body>
</html>

welcome.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'welcome.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
    <%
        int i=0;
        if(application.getAttribute("num")==null)
            application.setAttribute("num","1");
        else{
             if(session.isNew()){
                String str=application.getAttribute("num").toString();  
                i=Integer.parseInt(str); 
                i++;
                application.setAttribute("num",i);
            }
        }


    %>
  </head>
  
  <body>
    <h2 align="center">登陆成功</h2>
    <h4 align="center">欢迎<%=session.getAttribute("name") %>光临本系统,您是第<%=application.getAttribute("num") %>位访客!
    <% request.setAttribute("name",session.getAttribute("name") ); %>
    <a href="over.jsp">注销</a></h4>
  </body>
</html>

over.jsp:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'over.jsp' starting page</title>
    
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

  </head>

  <body>
          <% session.getAttribute("name");
              session.invalidate(); 
              Cookie cookies[]=request.getCookies();
            for(int i=0;i<cookies.length;i++)
            {
                cookies[i].setMaxAge(0);
                response.addCookie(cookies[i]);
            }
        %>
        <h4 align="center">您已成功退出本系统,3秒后跳转回首页!</h4>
        <%
            response.setHeader("refresh", "3;URL=login.jsp");
        %>
        <h4 align="center">如果没有跳转,请按<a href="login.jsp">这里</a></h4>    
  </body>
</html>
  • 写回答

1条回答 默认 最新

  • BCS-点心 2021-10-19 14:30
    关注

    抓紧时间往后学,jsp面试问不到,工作用不到。

    评论

报告相同问题?

问题事件

  • 创建了问题 10月19日

悬赏问题

  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多