为什么跳转不到login_success.jsp页面?

来源:4-11 阶段案例——实现用户登录

老子尼克杨

2018-12-10 23:56

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

String username="";

String password="";

request.setCharacterEncoding("UTF-8");

username=request.getParameter("username");

password=request.getParameter("password");

//如果用户名和密码都等于admin,则登录成功;

if("admin".equals(username) && "admin".equals(password))

{

  session.setAttribute("loginUser",username);

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

}else{

  response.sendRedirect("login_failure.jsp");//如果登录名或密码错误,则登录失败;

}

%>


写回答 关注

2回答

  • 老子尼克杨
    2018-12-11 21:32:15

    login.jsp页面的代码:

    <%@ page language="java" import="java.util.*" contentType="text/html; charset=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>imooc-login</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>

       <div id="container">

          <div class="logo">

             <a href="dologin.jsp"><img src="assets/006.jpg" alt="" /></a>

          </div>

          <div id="box">

             <form action="dologin.jsp" method="post">

             <p class="main">

                 <label>用户名:</label>

                 <input name="username" value="" />

                 <label>密码:</label>

                 <input type="password" name="passsword" />

             </p>

             <p class="space">

                <input type="submit" value="登录" class="login" style=""/>

             </p>

             </form>

          </div>

       </div>

      </body>

    </html>

    login_success.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 'index.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>

       <div id="container">

          <div class="logo">

             <a href="dologin.jsp"><img src="assets/006.jpg" alt="" /></a>

          </div>

          <div id="box">

          <%

             String loginUser="";

             if(session.getAttribute("loginUser")!=null){

                    loginUser = session.getAttribute("loginUser").toString();        

             }  

           %>

             欢迎您<font color="red"><%=loginUser%></font>,登陆成功!

          </div>

       </div>

      </body>

    </html>

    login_failure.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 'index.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>

       <div id="container">

          <div class="logo">

             <img src="assets/006.jpg" alt="" />

          </div>

          <div id="box">

             登陆失败!请检查用户名或密码!<br/>

             <a href="login.jsp">返回登录</a>

          </div>

       </div>

      </body>

    </html>

    这些就是所有页面的代码了


    扶月

    dologin.jsp少写个do

    2019-02-08 00:37:59

    共 2 条回复 >

  • Narcissulyh
    2018-12-11 09:55:55

    你最好吧别的页面的代码,也发上来。

    老子尼克杨

    已经发了,可以帮我看一下吗?谢谢

    2018-12-11 21:33:17

    共 1 条回复 >

JAVA遇见HTML——JSP篇

Java Web入门级教程JSP,带你轻松的学习JSP基础知识

248277 学习 · 3071 问题

查看课程

相似问题