Jsp - two pages in one

2.1k Views Asked by At

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>
3

There are 3 best solutions below

1
JovanToroman On BEST ANSWER

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.

0
Alan Hay On

You can easily do this by setting some flag in the code that checks the login (in the page you haven't posted) and then check for this flag in login.jsp to determine whether or not to show the error message.

You can use the Java Standard Tag Library to do this.

https://www.tutorialspoint.com/jsp/jsp_standard_tag_library.htm

In both JSPs add the directive:

<%@ taglib uri = "http://java.sun.com/jsp/jstl/core" prefix = "c" %>

On failed login:

<c:set var="loginFailed value="true" scope="request"/>

on the login page:

<c:if test="${loginFailed eq true}">
        <div id="message">
            Wrong UserName Or Password. Please Try Again
        </div>
</c:if>
0
Sharan madhav On

hope this solution works well instead of having multiple files use if elseif else statements let me explain the flow first let me explain the flow of the program

jsp is a server side program and it starts its execution from the beginning

flow

declare a variable and initialize 0 to it

use tag with the action # i.e. to refresh the page when submit is clicked

<form action="#" method="get">

by default inside the program you will be checking for the user credentials

if user credentials match just go with the flow else initialize the flag variable to 1

<% if(flag==1)
{%>
<html>your code xyz</html>
<% }
%>

thankyou!