ActionTest.java
package com.ztx.action;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import com.opensymphony.xwork2.ActionSupport;
public classActionTest extends ActionSupport {
private InputStream inputStream;
private String nameId;
public String getNameId() {
return nameId;
}
public void setNameId(StringnameId) {
try {
//%df%a4转换为字符串,即使用指定的编码机制对 application/x-www-form-urlencoded 字符串解码
this.nameId = java.net.URLDecoder.decode(nameId,"UTF-8");
}catch(UnsupportedEncodingException e) {
e.printStackTrace();
}
}
public InputStreamgetInputStream() {
return inputStream;
}
public String execute() throws Exception {
if (nameId.equals("zhangsan")){
inputStream = new ByteArrayInputStream("用户名正确".getBytes("utf-8"));
}else{
inputStream = new ByteArrayInputStream("用户名不存在".getBytes("utf-8"));
}
return SUCCESS;
}
}
struts.xml
<?xml version="1.0"encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD StrutsConfiguration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<constant name="struts.configuration.xml.reload" value="true" />
<package name="ztx" extends="struts-default">
<action name="testAjax" class="com.ztx.action.ActionTest">
<result type="stream">
<param name="contentType">application/octet-stream</param>
<param name="inputName">inputStream</param>
</result>
</action>
</package>
</struts>
<%@ page language="java"import="java.util.*" pageEncoding="utf-8"%>
<%
Stringpath = request.getContextPath();
StringbasePath = request.getScheme() + "://"
+request.getServerName() + ":" + request.getServerPort()
+path + "/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTDHTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP'index.jsp' starting page</title>
<meta http-equiv="pragma"content="no-cache">
<meta http-equiv="cache-control"content="no-cache">
<meta http-equiv="expires"content="0">
<meta http-equiv="keywords"content="keyword1,keyword2,keyword3">
<meta http-equiv="description"content="This is my page">
<!--
<linkrel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript">
var xmlHttp;
function validate() {
var idField = document.getElementById("userName");
// varxhrUrl = "DoAjaxServlet.do?nameId="+escape(idField.value);
//这样写的后果就是:
//如果传递中文,就会抛出异常
//警告: Parameters: Character decoding failed. Parameter skipped.
//java.io.CharConversionException: isHexDigit.
var xhrUrl = "testAjax.action?nameId=" + idField.value;
xhrUrl = encodeURI(xhrUrl); //写一个不行。如果写一个就是????号。
xhrUrl = encodeURI(xhrUrl); //写2个,则输出%df%a4这中.application/x-www-form-urlencoded字符串
if (window.XMLHttpRequest) {
xmlHttp = new XMLHttpRequest();
} else {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlHttp.open("get", xhrUrl, true);
xmlHttp.onreadystatechange =callback;
xmlHttp.send();
}
function callback() {
if (xmlHttp.readyState == 4 &&xmlHttp.status == 200) {
document.getElementById("info").innerHTML =xmlHttp.responseText;
}
}
</script>
</head>
<body>
<form action="" method="post">
<input type="text" id="userName"name="userName" onblur="validate()"/>
<div id="info"></div>
<br /> <input type="text"id="password" name="password"/><br /> <input
type="submit" value="提交">
</form>
</body>
</html>