ajax应用:服务器端为什么不能正常返回字符串?

Fufuliusun 发布于 2011/04/14 10:29
阅读 1K+
收藏 1

解读下一代网络:算力网络正从理想照进现实!>>>

服务器端:

开发工具NetBeans IDE 6.7.1 服务器:GlassFish v2.1 JavaEE版本:JavaEE 5 框架:Visual Web JavaServer Faces

1)写一个EJB3Bean对数据库Microsoft SQL 2005的数据进行基本操作;

2)写一个JavaBean(类名:AjaxBeanInBom),注入1)中EJB,把数据解析成字符串;

3)建立一个VWP网页(Navigation.jsp),对应版本为jsp2.1Servlet2.5,运用2)中Bean处理请求和响应,在该网页的头部嵌入以下代码:

                    <jsp:directive.page import="java.util.*,java.text.*"/>
                    <jsp:useBean class="ajaxbeantest.AjaxBeanInBom" id="ajaxbean" scope="page"/>
                    <jsp:scriptlet>
                        response.addHeader("Cache-Control", "no-cache");
                        response.addHeader("Expires", "Thu, 01 Jan 1970 00:00:01 GMT");
                        request.setCharacterEncoding("UTF-8");    [color=#FF0000]//疑问1[/color]
                        String date1=null;
                        date1=(String)request.getParameter("method");
                        if(date1!=null){
                                String mystr=null;
                                ajaxbean.setRequest(request);
                                mystr=ajaxbean.getResponseXML();
                                System.out.println(mystr);
                                response.setContentType("text/html;charset=UTF-8");
                                response.getWriter().print(mystr);
                                response.getWriter().close();
                        }
                    </jsp:scriptlet>

客户端:

IE浏览器,利用javascriptajax操作页面内容

4)采用post方式发送请求和获取响应,关键代码如下:

function accessdb_S(jspname,sSQLobj){   //select rows
    var requestString;
    //注明:因不影响阅读,中间requestString的具体赋值舍去
    	createXMLHttpRequest();
	xmlHttp.open("POST", jspname,false);
	xmlHttp.onreadystatechange = handleStateChange;
	xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");
	xmlHttp.send(requestString);  
}

function handleStateChange() {
    if(xmlHttp.readyState == 4) {
        if(xmlHttp.status == 200) {
            alert(xmlHttp.status);
            alert(xmlHttp.responseText);    //[color=#FF0000]疑问2[/color]
        }
    }
}

问题:

疑问1):服务器端报错:PWC4011:由于已读取请求参数或已调用 ServletRequest.getReader(),因此无法将请求字符编码从上下文 /fuBom 设置为 UTF-8

疑问2):由System.out.println(mystr)可以查看到服务器输出自己预期想要的字符串:

0||-1||Tree Example||www.baidu.com$$1||0||节点配置||treeNodeConfig.jsp$$2||1||新浪||www.sina.com$$3||0||搜狐||www.sohu.com$$4||1||微软||www.microsoft.com$$5||2||雅虎||www.yahoo.com

但是在客户端由alert(xmlHttp.responseText)查看时,在该字符串前却加上了有关html的代码,详细如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:wairole="http://www.w3.org/2005/01/wai-rdf/GUIRoleTaxonomy#" xmlns:waistate="http://www.w3.org/2005/07/aaa">
<head>
<title></title><script type="text/javascript">var webui_suntheme4_2={"webuiJsfx":false,"webuiAll":false,"theme":{"locale":"zh-cn"},"ajax":{"isJsfx":true},"isDebug":false,"isStyleSheet":true,"parseOnLoad":true};</script><script type="text/javascript" src="/fuBom/theme/com/sun/webui/jsf/suntheme4_2-080320/javascript/bootstrap.js"></script><link id="link1" rel="stylesheet" type="text/css" href="/fuBom/resources/stylesheet.css" />
<link id="link2" rel="stylesheet" type="text/css" href="/fuBom/resources/dtree.css" />
<script id="script1" type="text/javascript" src="/fuBom/PublicJS_justforbom.js"></script>
<script id="script2" type="text/javascript" src="/fuBom/ajax_dtree.js"></script>
<script id="script3" type="text/javascript" src="/fuBom/createDtree.js"></script>
</head><body id="body1" style="-rave-layout: grid" onload="refreshDtree();return false;"><div id="mydtree" class="dtree"></div><form id="form1" class="form" method="post" action="/fuBom/faces/Navigation.jsp" enctype="application/x-www-form-urlencoded">
<input id="form1:button1" type="submit" name="form1:button1" value="Submit" style="left: 360px; top: 120px; position: absolute" />
<input id="form1_hidden" name="form1_hidden" value="form1_hidden" type="hidden" />
<input type="hidden" name="javax.faces.ViewState" id="javax.fa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zkcH60bySzErX94gERCTFxVIjxEKJkVu4zggd1KRARUGWPksHcxcfMZo8KLBkAU+S9/70S0mtIlwP058G+Fogo2CLewQyPz3ZoZHg2bvXxumNKKExNKkyNh2E9EBHw5RVF1OAwnxHkZTtUUEOG8b4pmAxjrtUZCw8FDuN5IxBhVK/5Xp4Z8KLIXzmQW4pjbE8ctJ0LhB/V3GhU3nXm+ZvN7Do18Sh6tVxtO4yLm6Mm9rfZoT1uBiKM5qZn++kMaiO36oVxBJNbgRgxeSZTp1g83DafYHP+2C3qX7rcv0a9an8pEBHlIm97Eg4OCxQ35JWn4F65mqWYO9RKN5DJcNbfNL0o77BFE/Vth6+lm9TQD3TJwd6IJIGRetMDEVET3pODDPWqSMF+KhK60X1cqufG/HdU6g7eG16ycSAiki2fOsN4R8chCX1h3O0jj+a0KoG1fXRyqTuIanrwueA9nciYe0mxhavO+kTZqXUMPgaqWn7exA1RAqsvLhE3m+Y2WUsvr6g5sZkhnwJraVRjtulw7Nljyp46m/8BewCfqxwNAAA=" autocomplete="off" />
</form>
<script type="text/javascript">webui.suntheme4_2.dojo.addOnLoad(function() {new webui.suntheme4_2.body('/Navigation.jsp', '/fuBom/faces/Navigation.jsp',null,null,'com_sun_webui_util_FocusManager_focusElementId',true);});</script></body></html>[/color]
0||-1||Tree Example||www.baidu.com$$1||0||节点配置||treeNodeConfig.jsp$$2||1||新浪||www.sina.com$$3||0||搜狐||www.sohu.com$$4||1||微软||www.microsoft.com$$5||2||雅虎||www.yahoo.com
0

自己想要的仅仅是那些从数据库中得到的字符串,而这些包含了HTML信息的字符串并非自己所需要,由于未获得正确字符串,从而导致客户端web页面无法实现ajax变化;即使在js中对返回的字符串进行截取,截掉包含html的那部分,一样是失败。为什么会出现这种情况?是不是利用responseText得到的字符串一定包含html信息部分?如何解决?

 

 

补充:

当开发环境其他不变,仅作下述改变时,上述两个错误都不会发生:

服务器:Sun Java System Application Server 8.2(更早的一个服务器版本); JavaEE版本:JavaEE 1.4

对应web页面:jsp1.2Servlet2.4

 

请各位大牛和神人急救!该问题困扰本人十多天之久,若能帮我提供解决之策,万分感谢!

 

 

加载中
返回顶部
顶部