Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Struts Ajax Call

843842Sep 1 2008 — edited Sep 15 2008
I have written a sample struts login page with ajax call. I am using <input type="button">. We can submit using the struts form using <input type="submit"> only. Here in my code I am using 'button', instead of 'submit' because of AJAX call. Now how i have to do submit the form to output jsp page. Whether I have to redirect the page. I don't have any idea. Please Help!

I have Login.jsp. I have to submit this, so that it will go to success.jsp. The Ajax response is executing successfully, but how to submit the form?

Login .Jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script type="text/javascript" src="js/AjaxCall.js">
</script>
</head>
<body>

 <html:form action="login" method="post" name="loginForm" type="com.homepage.LoginForm">
   <div id="header">Login Page</div><br>
   <html:text property="userName" size="16"/><br>
   <html:text property="password" size="16"/><br>
   <html:button property="mysubmit" value="Click" onclick="makeGetRequest();"/><br>
   <html:reset/>
 </html:form>
</body>
</html> 
AjaxCall.js
function createRequestObject() {
		var tmpXmlHttpObject;
		if(window.XMLHttpRequest) {
			tmpXmlHttpObject = new XMLHttpRequest();
		} else if (window.ActivexObject) {
			tmpXmlHttpObject = new ActiveXobject("Microsoft.XMLHTTP");
		}
		return tmpXmlHttpObject;
	}
	
	var http;
	function makeGetRequest() {
		http = createRequestObject();
		http.open('get', 'Login.jsp', true);
		http.onreadystatechange = processResponse;
		http.send(null);
	}
	
	function processResponse() {
		if(http.readyState == 4) {
			document.getElementById('header').innerHTML = "You have not typed Anything";
	}
	}
struts-config.xml
<?xml version="1.0" encoding="ISO-8859-1" ?>

<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">

<struts-config>

	<form-beans>
        <form-bean name="loginForm" type="com.homepage.LoginForm"/>
    </form-beans>
           
    <action-mappings>
       <action path="/login" type="LoginAction" name="loginForm" input="/Login.jsp">
            <forward name="success" path="/success.jsp"/>
            <forward name="failure" path="/Login.jsp"/>
        </action>
    </action-mappings>
	          
</struts-config>
success.jsp
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>

    <h1>Output Page</h1>
    You have Successfully Login!<br>
    <% String userName = request.getParameter("userName");
    String password = request.getParameter("password");%>
    <b>User Name :</b> <%=userName %><br>
    <b>Password : </b><%=password %>
    </body>
</html>
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 13 2008
Added on Sep 1 2008
10 comments
649 views