JSP-二合一

时间:2018-06-21 22:17:41

标签: jsp custom-error-pages login-control

所以我刚开始用jsp编写我的第一个代码。

我做了一个登录页面,用户输入了他的用户名和密码,如果数据库(MySQl)中不存在它们,则它将在同一页面上打印一条消息(用户认为这是同一页面)。

我是通过将信息发送到文件(在数据库中查找)来实现的,如果信息正确,则转到下一页,如果错误则转到错误页面。

现在我要问的是,如果我有两个页面具有相同的代码(Login.jsp和ErrorLogin.jsp),唯一的区别是额外的错误消息如何将它们加入?

我可以避开我的两个页面吗? :/ 我的第一个想法是在该语言中添加if语句,但令人困惑。

这是我创建的2个文件(我不认为Validation_Login.jsp对此很重要,但是如果让我知道上传它的话)


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>

2 个答案:

答案 0 :(得分:1)

实现此目标的最佳方法是使用jstl标签“ include”

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

要使其成为可能,jsp页面“ template_name”的内容应为:

<%@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>

关键是要包含从参数中获得的页面:

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

因此,当我们将名称为“ content_page_name”的页面传递到模板页面时,该页面将插入“模板”页面中“包含”标记所在的位置。

*注意:content页面应仅包含html正文元素(因为在模板页面中已经定义了head。

答案 1 :(得分:0)

您可以轻松地做到这一点,方法是在检查登录的代码中(在您尚未发布的页面中)设置一些标志,然后在login.jsp中检查该标志以确定是否显示错误消息。

您可以使用Java标准标记库执行此操作。

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

在两个JSP中添加指令:

from selenium import webdriver import time chrome_path = r"C:\Users\Andrei\Desktop\driver\chromedriver.exe" driver = webdriver.Chrome(chrome_path) driver.get('https://meshb.nlm.nih.gov/treeView') driver.implicitly_wait(1) # wait until page will be loaded search_word = "Cardiovascular System" elements_xpath = "//span[@ng-if = 'node.HasChildren']/i[@ng-click='getTreeChildren(node)']" spans_xpath = "//span[contains(., '" + search_word + "')]/parent::*/parent::*/descendant-or-self::node()/a/span" link_xpath = "//span[@class = 'ng-binding ng-scope']" # expand all nodes first level elements = driver.find_elements_by_xpath(elements_xpath) for element in elements: element.click() time.sleep(0.3) # search for span position elements = driver.find_elements_by_xpath(link_xpath) i = 0 for element in elements: if search_word in element.text: break i += 1 # i is the position where the 'Cardiovascular' was found # now is time to expand all child nodes at i position end = i + 1 elements = driver.find_elements_by_xpath("//span[@class = 'ng-scope']") old_length = len(elements) while i < end: elements[i].click() i += 1 time.sleep(0.3) elements = driver.find_elements_by_xpath("//span[@class = 'ng-scope']") end = end + len(elements) - old_length old_length = len(elements) # all child nodes are expanded # time to collect information spans = driver.find_elements_by_xpath(spans_xpath) for span in spans: print(span.text)

登录失败:

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

在登录页面上:

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