----------------------- web.xml---------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Java</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
----------------------- index.jsp---------------------------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<script type="text/javascript">
function get_random(){
var ranNum= Math.floor(Math.random()*5);
return ranNum;
}
</script>
<head>
<META HTTP-EQUIV="Refresh" CONTENT="0;URL=example/showHomePage.action?rand=<%=(Math.random()) %>">
</head>
<body>
<p>Loading ...</p>
</body>
</html>
--------------------- struts.xml---------------------------------
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="false" />
<package name="example" namespace="/example" extends="struts-default">
<action name="showHomePage" class="example.Login">
<result>/example/LoginPage.jsp</result>
</action>
<action name="validateUsername" class="example.Login" method="validateUsername">
<result>/example/resp.jsp</result>
<result name="input">/example/resp.jsp</result>
</action>
</package>
</struts>
------------------ LoginPage.jsp------------------------------
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<script type="text/javascript">
var xmlhttp;
function getHTTPObjectForBrowser(){
if (window.XMLHttpRequest){
xmlhttp=new XMLHttpRequest();
}else{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
return xmlhttp;
}
function setAjaxOutput(){
if(xmlhttp.readyState == 4){
var result = xmlhttp.responseText;
if(result=='input'){
document.getElementById('username').value='';
document.getElementById('password').value='';
document.getElementById('username').focus();
}
if(result=='success'){
document.getElementById('result').value=result;
}
}
}
function validateUser(){
xmlhttp= getHTTPObjectForBrowser();
var username = document.getElementById('username').value;
var password = document.getElementById('password').value;
if (xmlhttp!= null) {
var url = "validateUsername.action?username="+username+'&password='+password;
xmlhttp.open("GET", url, true);
xmlhttp.send(null);
xmlhttp.onreadystatechange = setAjaxOutput;
}
}
</script>
</head>
<body>
<s:form name="loginPage" id="LoginPage" action="loginSubmit">
<s:textfield label="username" id="username"> </s:textfield>
<s:textfield label="password" id="password" onblur="validateUser();"></s:textfield>
<s:textfield label="result" id="result" name="result"></s:textfield>
</s:form>
</body>
</html>
-------------------------- Login.java---------------------------
import com.opensymphony.xwork2.ActionSupport;
public class Login extends ActionSupport {
private static final long serialVersionUID = 1L;
private String username;
private String password;
private String resp;
public String execute() {
return SUCCESS;
}
public String validateUsername() {
if (username.equals("admin") && password.equals("admin")) {
resp = "success";
return SUCCESS;
} else {
resp = "input";
return INPUT;
}
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public void setResp(String resp) {
this.resp = resp;
}
public String getResp() {
return resp;
}
public void setPassword(String password) {
this.password = password;
}
public String getPassword() {
return password;
}
}
-------------- resp.jsp-------------------------------------
${resp}
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Java</display-name>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
----------------------- index.jsp---------------------------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<script type="text/javascript">
function get_random(){
var ranNum= Math.floor(Math.random()*5);
return ranNum;
}
</script>
<head>
<META HTTP-EQUIV="Refresh" CONTENT="0;URL=example/showHomePage.action?rand=<%=(Math.random()) %>">
</head>
<body>
<p>Loading ...</p>
</body>
</html>
--------------------- struts.xml---------------------------------
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<constant name="struts.devMode" value="false" />
<package name="example" namespace="/example" extends="struts-default">
<action name="showHomePage" class="example.Login">
<result>/example/LoginPage.jsp</result>
</action>
<action name="validateUsername" class="example.Login" method="validateUsername">
<result>/example/resp.jsp</result>
<result name="input">/example/resp.jsp</result>
</action>
</package>
</struts>
------------------ LoginPage.jsp------------------------------
<%@ page contentType="text/html; charset=UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<html>
<head>
<script type="text/javascript">
var xmlhttp;
function getHTTPObjectForBrowser(){
if (window.XMLHttpRequest){
xmlhttp=new XMLHttpRequest();
}else{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
return xmlhttp;
}
function setAjaxOutput(){
if(xmlhttp.readyState == 4){
var result = xmlhttp.responseText;
if(result=='input'){
document.getElementById('username').value='';
document.getElementById('password').value='';
document.getElementById('username').focus();
}
if(result=='success'){
document.getElementById('result').value=result;
}
}
}
function validateUser(){
xmlhttp= getHTTPObjectForBrowser();
var username = document.getElementById('username').value;
var password = document.getElementById('password').value;
if (xmlhttp!= null) {
var url = "validateUsername.action?username="+username+'&password='+password;
xmlhttp.open("GET", url, true);
xmlhttp.send(null);
xmlhttp.onreadystatechange = setAjaxOutput;
}
}
</script>
</head>
<body>
<s:form name="loginPage" id="LoginPage" action="loginSubmit">
<s:textfield label="username" id="username"> </s:textfield>
<s:textfield label="password" id="password" onblur="validateUser();"></s:textfield>
<s:textfield label="result" id="result" name="result"></s:textfield>
</s:form>
</body>
</html>
-------------------------- Login.java---------------------------
import com.opensymphony.xwork2.ActionSupport;
public class Login extends ActionSupport {
private static final long serialVersionUID = 1L;
private String username;
private String password;
private String resp;
public String execute() {
return SUCCESS;
}
public String validateUsername() {
if (username.equals("admin") && password.equals("admin")) {
resp = "success";
return SUCCESS;
} else {
resp = "input";
return INPUT;
}
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public void setResp(String resp) {
this.resp = resp;
}
public String getResp() {
return resp;
}
public void setPassword(String password) {
this.password = password;
}
public String getPassword() {
return password;
}
}
-------------- resp.jsp-------------------------------------
${resp}
Screen shot:
----------------
No comments:
Post a Comment