简体   繁体   English

重定向到同一页面但其中有一条消息

[英]Redirect to the same page but with a message in it

I am making a login page in JSP. 我正在用JSP创建登录页面。 I have an index.jsp page where exist the form and some javascript scriplets. 我有一个index.jsp页面,其中存在表单和一些JavaScript脚本。 Connectivity to oracle database and checking for username and password in database is done in check1.jsp file 连接到oracle数据库并检查数据库中的用户名和密码是在check1.jsp文件中完成的

My issue is after entering username and password, when I press login button, I have linked the form to check1.jsp , if username and password matches and exist, it redirects to welcome.jsp , but if username doesnot exist or password is not matched I have to get back to index.jsp showing a small message below box that username doesn't exist OR Password is not matched , currently I am just redirecting to index.jsp . 我的问题是输入用户名和密码后,按登录按钮时,我已将表单链接到check1.jsp ,如果用户名和密码匹配并存在,它将重定向到welcome.jsp ,但是如果用户名不存在或密码不匹配我必须返回index.jsp 在框下方显示一条小消息,提示用户名不存在或密码不匹配 ,当前我只是重定向到index.jsp

How should I show that appropriate small message below login box on that same index.jsp page?? 我应如何在同一index.jsp页面上的登录框下方显示相应的小消息?

You can use session object here: 您可以在此处使用session对象:

In check1.jsp : check1.jsp

if (loginSuccess) {
    //redirect to welcome.jsp
}
else {
    session.setAttribute("error", "Username or Password is incorrect");
    //redirect to index.jsp
}

In index.jsp : index.jsp

String msg = session.getAttribute("error");
if (msg != null) {
    %><p style="color:red"><%= msg %></p><%
}

Also there is other more simpler ways exists with EL and JSTL, but its just for an starting tip. EL和JSTL还存在其他更简单的方法,但这只是一个入门技巧。

You should not redirect to index.jsp, but forward to index.jsp in this case. 在这种情况下,您不应重定向到index.jsp,而应转发到index.jsp。 Store the message in some request attribute, forward to the index.jsp, and display the message if it's present in the request attribute. 将消息存储在某个请求属性中,转发到index.jsp,并显示该消息(如果请求属性中存在)。

You should use an MVC framework, in order to always make your URLs point to actions (controllers) implemented in Java, make these actions call the business ogic (here, log the user in) and have those actions forward or redirect to the appropriate view (index.jsp or welcome.jsp). 您应该使用MVC框架,以便始终使您的URL指向用Java实现的动作(控制器),使这些动作称为业务ogic(在此登录用户),并使这些动作转发或重定向到适当的视图。 (index.jsp或welcome.jsp)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM