我的问题是如何从一个jsp页面指向另一个jsp页面?

时间:2017-06-13 05:38:04

标签: mysql jsp

     This is my login.jsp page

    <%@ page import ="java.sql.*" %>
    <%@page import="java.sql.ResultSet"%>
    <%@page import="java.sql.Statement"%>
    <%@page import="java.sql.Connection"%>
    <!DOCTYPE html>
    <html>
    <style>
    /* Full-width input fields */
    input[type=text], input[type=password] {
        width: 100%;
        padding: 12px 20px;
        margin: 8px 0;
        display: inline-block;
        border: 1px solid #ccc;
        box-sizing: border-box;
    }

    /* Set a style for all buttons */
    button {
        background-color: #4CAF50;
        color: white;
        padding: 14px 20px;
        margin: 8px 0;
        border: none;
        cursor: pointer;
        width: 100%;
    }

    /* Extra styles for the cancel button */
    .cancelbtn {
        padding: 14px 20px;
        background-color: #f44336;
    }

    /* Float cancel and signup buttons and add an equal width */
    .cancelbtn,.signupbtn {
        float: left;
        width: 50%;
    }

    /* Add padding to container elements */
    .container {
        padding: 16px;
    }

    /* Clear floats */
    .clearfix::after {
        content: "";
        clear: both;
        display: table;
    }

    /* Change styles for cancel button and signup button on extra small screens */
    @media screen and (max-width: 300px) {
        .cancelbtn, .signupbtn {
           width: 100%;
        }
    }
    </style>
    <head>
    <meta charset="ISO-8859-1">
    <title>Registration Form</title>
    </head>
    <form method="post" action="success.jsp">

    <div class="container">
    <h2>Login Form</h2>
         <label><b>Username</b></label>
        <input type="text" placeholder="Enter Username" name="Username" required>
             <label><b>Password</b></label>
            <input type="password" placeholder="Enter Password" name="Password" required>
        <div class="clearfix">
        <button type="submit" value="submit">Login</button>

        </div>
        </div>
    </html>
    <%
        String Username= request.getParameter("Username");    
        String Password = request.getParameter("Password");
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mysql","root","root");
        Statement st = con.createStatement();
        ResultSet rs;
        rs=st.executeQuery("SELECT * FROM customers WHERE Username='" + Username + "' and Password='" + Password + "'");
        if (rs.next()) {
              session.setAttribute("Username",Username);
          // out.println("Welcome " + Username);
           //out.println("<a href='logout.jsp'>Log out</a>");
            response.sendRedirect("success.jsp");
        }
        else
        {
            out.println("Invalid Username or password Please try again");
        }
    %>
    I am using eclipse,tomcat,mysql db.When i run login.jsp page it is asking me validation details and total code works fine but in output page i am getting
    O/P:Welcome null.It is throwing me null value.And also it is directly going to else loop and giving me output in the output page like this "Invalid Username or Password Please try again".I used both HTML and java code in jsp file is that correct???

    success.jsp:

    <%
        if ((session.getAttribute("Username") == "null") || (session.getAttribute("Password") =="null")) {
    %>
    You have not logged in<br/>
    <a href="login.jsp">Please Login</a>
    <%} else {
    %>
    Welcome <%=session.getAttribute("Username")%>
    <a href='logout.jsp'>Log out</a>
    <%
        }
    %>

欢迎&lt;%= session.getAttribute(&#34;用户名&#34;)%&gt;

                                              //预期输出欢迎rohan542         login.jsp直接从login.jsp到success.jsp.换句话说,它必须从登录页面指向成功页面。没有堆栈跟踪,我得到输出但不是我期待的。我认为代码有一点点错误请纠正它。我在java中使用会话。代码必须检查用户名和密码是否正确如果它们是正确的那么它必须验证并转到成功页面旁边我提供了一个注销链接直接退出那里

1 个答案:

答案 0 :(得分:0)

您可以使用

<c:redirect url = "your-url"/>

标记重定向到较新的网址。

由于您的代码位于同一页面,因此您的操作不应指向success.jsp页面。它应该指向相同的登录页面。请修改问题描述以避免混淆。

请更改以下行。

 <form method="post" action="success.jsp"> to
 <form method="post" action="login.jsp">