Search code examples
jspcustom-error-pageslogin-control

Jsp - two pages in one


So I just started writing my first code in jsp.

I made a login page that the user writes his username and his password and if either of them does not exists in the database(MySQl),it print's a message in the same page(the user thinks it's the same page).

I did that by sending the information to a file (looks in the database),and if the information is correct then goes to the next page and if its wrong goes to an error page.

Now my question to you is that if I have two pages with the same code(Login.jsp and ErrorLogin.jsp) and the only difference is the extra error message how to join them?

can I avoid my the two same pages? :/ My first thinking was to put an if statement but its confusing in this language.

Here is the 2 files I have created(I don't think that the Validation_Login.jsp is important for this,but if it is let me know to upload it)


Login.jsp

<body> 

        <form action="Validation_Login.jsp">             
                <font size="+3"> Login </font>
                <br/><br/>

                <div>
                    USERNAME: <input type="text" placeholder="Enter Username" name="firstname"/>
                    <br/><br/>
                </div>

                <div>
                    PASSWORD: <input type="password" placeholder="Password" name="Password" />
                    <br/><br/>
                </div>

                <div>
                    <input type="submit" value="login" />
                </div>
        </form>

        <form action="Create_Account.jsp">
                <input type="submit" value="Create_Account">
        </form>

    </body>

ErrorLogin.jsp

<body>         
        <form action="Validation_Login.jsp" >
                <font size="+3"> Login </font>
                <br/><br/>

                <div>
                    USERNAME: <input type="text" placeholder="Enter Username" name="firstname"/>
                    <br/><br/>
                </div>

                <div>
                    PASSWORD: <input type="password" placeholder="Password" name="password" />
                    <br/><br/>
                </div>

                <div>
                    <input type="submit" value="login" />
                </div>
        </form>

        <form action="Create_Account.jsp">
            <input type="submit" value="Create_Account">
        </form>

        <div id="message">
            Wrong UserName Or Password
            <br/>
            &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Please Try Again
        </div>
    </body>

Solution

  • The best way to achieve this is using jstl tag "include"

    <jsp:include page="./path-to-template/template_name.jsp">
        <jsp:param name="content" value="content_page_name"></jsp:param>
    </jsp:include>
    

    To make this possible, the content of the "template_name" jsp page should be:

    <%@page contentType="text/html" pageEncoding="ISO-8859-6"%>
    <!DOCTYPE html>
    <html lang="en">
        <head>
            <title>MyGrowth - Track Education While in Migration</title>
            <meta charset="utf-8">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <meta http-equiv="X-UA-Compatible" content="IE=edge">
        </head>
        <body>
            <div class="container">
                <%@include file="header.jsp" %>
                <jsp:include page="../content/${param.content}.jsp"></jsp:include>
                </div>                      
            </body>
        <%@include file="footer.jsp"%>
    </html>
    

    The crucial part is where you include the page you got from the parameter:

           <jsp:include page="../content/${param.content}.jsp"></jsp:include>
    

    So, when we pass the page with name 'content_page_name' to the template page, then that page is inserted at the place where the 'include' tag is, in the 'template' page.

    *NOTE: The content pages should contain only html body elements (since the head is already defined in the template page.