Browse DevX
Sign up for e-mail newsletters from DevX


iFrames + JSP = Enhanced Web Content Retrieval : Page 3

Used with JavaScript and JSP code, iFrames elegantly resolve the "hang" issue that occurs when a Web application fetches values from the server.




Building the Right Environment to Support AI, Machine Learning and Deep Learning

Installing and Running the Sample
I use JDeveloper 10g to build and run Web applications. The source code contains a workspace that you can open in JDeveloper to run the registration page, thereby running the application. If you don't want to use JDeveloper, extract the contents of the included WAR file and open it in your favorite IDE. You can also deploy the WAR directly to any JSP/Servlet engine.

Registration Page: registration.jsp
The registration.jsp file is the registration page you'll usually find in Web applications. Clicking the "check name" button calls the following JavaScript function:

function checkName() { // Some Validation //Set login_name field in the iframe's page. window.frames['iframe1'].document.forms[0].login_name.value=
document.forms[0].login_name.value; //Submit iFrame's page. window.frames['iframe1'].document.forms[0].submit(); }

This function sets the login field in the iFrame page and submits the page's form.

The iFrame Page: check_name.jsp
The iFrame page contains a form with one text field:

<form action="<%= request.getContextPath() %>/registrationServlet/checkName" method="post" > <input type="text" name="login_name"/> </form>

The text field is populated with the value the user enters in the parent page (registration.jsp). When the page is submitted to the server, a Servlet (RegistrationServlet) processes the request. The Servlet checks whether the user name is already used and sets an attribute in the request before forwarding it to check_name.jsp:

request.setAttribute("loginNameUsed", new Boolean(true) );

In check_name.jsp, JSP code generates JavaScript code that will populate the parent page's message area, indicating whether the user name has already been used:

<% // If condition only executed after the page has submitted a request to RegistrationServlet if ( request.getAttribute("loginNameUsed") != null ) { Boolean loginNameUsed= (Boolean) request.getAttribute("loginNameUsed"); if ( loginNameUsed.booleanValue() == true ) { %> <script> parent.document.all['msg_area'].innerText='Login name already used'; </script> <% } else { %> <script> parent.document.all['msg_area'].innerText='Valid Login name'; </script> <% } } %>

Last Step: Make It Invisible
You can hide the iFrame once you've completed development and testing by setting its height and width to zero.

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date