%(percentage)符号不能从文本框中获取,并通过AJAX使用JavaScript在jsp页面的textarea中显示在文本区域中]] << [

问题描述 投票:1回答:3
在我的jsp页面中,

<form id="indexForm" name="indexForm" action="LoginAction" method="post"> <table> <tr> <td id="fname" class="fontStyle">First Name :</td> <td id="fvalue"><input type="text" id="firstname" name="firstname" /></td> <td id="lname" class="fontStyle">Last Name :</td> <td id="lvalue"><input type="text" id="lastname" name="lastname" /></td> </tr> <tr> <td></td> <td> <center><!-- <input type="button" value="End Chat" onclick="getValues()" /> --> <a id="button" href="javascript:getValues();">Start chat</a></center> </td> <td> <center><!-- <input type="button" value="End Chat" /> --> <a id="button" href="javascript:logout();">Stop chat</a></center> </td> <td></td> </tr> </table> </form> </div> <div style="display: none;" id="div"> <div><textarea id="textarea" rows="10"></textarea></div> <div> <table> <tr> <td id="msg" class="fontStyle">message :</td> <td id="msgvalue"><input size="40" type="text" id="message" /></td> <td style="width: 5%;"></td> <td><!-- <input type="button" value="send" onclick="sendMessage()" /> --> <a id="button3" href="javascript:sendMessage();">Send</a></td> </tr> </table> </div> </div>

在我的JavaScript中,

var message ; var textarea ; function sendMessage(){ message = document.getElementById("message").value; textarea = document.getElementById("textarea"); try { xmlhttp.onreadystatechange=function() { //alert("Status : "+xmlhttp.status+"\nreadyState : "+xmlhttp.readyState); if (xmlhttp.readyState==4 && xmlhttp.status==200) { var checkMsg = encodeURIComponent(xmlhttp.responseText.toString()); if(checkMsg != "null" && checkMsg != null){ //document.getElementById("textarea").innerHTML += checkMsg; if(textarea.value == "") textarea.value = checkMsg; else textarea.value += "\n"+ checkMsg ; } } }; xmlhttp.open("POST","SendMessageAction?message="+encodeURIComponent(message)+"&sid"+Math.random(),true); xmlhttp.send(); } catch(err) { alert(err.description); } }

在我的servlet中,

package com.tps.flexchat.action; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.genesyslab.platform.webmedia.protocol.FlexChatProtocol; import com.tps.flexchat.Request.SendMessage; import com.tps.flexchat.info.ApplicationInfo; import com.tps.flexchat.info.CustomerInfo; public class SendMessageAction extends HttpServlet { private static final long serialVersionUID = 1L; private String msg; private String seckey; private String uid; private String sessionId; private int counter; private FlexChatProtocol protocol = null; private SendMessage message; public SendMessageAction() { super(); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); msg = request.getParameter("message"); seckey = request.getParameter("securekey"); uid = request.getParameter("userId"); sessionId = request.getParameter("sessionId"); //counter =Integer.parseInt(request.getParameter("counter")); counter = 1; protocol = ApplicationInfo.flexProtocol; message = new SendMessage(); message.send(msg, seckey, uid, sessionId, counter, protocol); CustomerInfo customer = ApplicationInfo.customerDetails.get(uid); out.print(customer.getMessage()); } }

sessionId,userId,secureKey可以由genesys生成并且可以正常工作。仅MSG存在问题...

在java类中,

package com.tps.flexchat.Request; import com.genesyslab.platform.commons.protocol.ChannelState; import com.genesyslab.platform.commons.protocol.Message; import com.genesyslab.platform.commons.protocol.ProtocolException; import com.genesyslab.platform.webmedia.protocol.FlexChatProtocol; import com.genesyslab.platform.webmedia.protocol.flexchat.EventInfo; import com.genesyslab.platform.webmedia.protocol.flexchat.MessageText; import com.genesyslab.platform.webmedia.protocol.flexchat.TreatAs; import com.genesyslab.platform.webmedia.protocol.flexchat.UserType; import com.genesyslab.platform.webmedia.protocol.flexchat.events.EventStatus; import com.genesyslab.platform.webmedia.protocol.flexchat.requests.RequestRefresh; import com.tps.flexchat.info.ApplicationInfo; import com.tps.flexchat.info.CustomerInfo; public class SendMessage { int i = 0; public void send(String msg,String seckey, String uid, String sessionId, int counter, FlexChatProtocol protocol){ CustomerInfo customer = ApplicationInfo.customerDetails.get(uid); RequestRefresh refresh = RequestRefresh.create(); refresh.setMessageText(MessageText.create(null, TreatAs.NORMAL, msg)); refresh.setFromPosition(customer.getFromPosition()+1); refresh.setSecureKey(seckey); refresh.setUserId(uid); try { if(protocol.getState() == ChannelState.Opened){ Message resp = protocol.request(refresh); if(resp != null){ EventStatus status = (EventStatus)resp; if(status.messageId() == EventStatus.ID){ String userId = status.getUserId(); if(status.getFlexTranscript() != null && status.getFlexTranscript().getEventInfoList() != null){ EventInfo info = (EventInfo) status.getFlexTranscript().getEventInfoList().getLast(); if(customer != null){ customer.setFromPosition(status.getFlexTranscript().getLastPosition()); customer.addMessage(info.getText()); } } } } //System.out.println(msg+";"+seckey+";"+uid+";"+sessionId+";"+counter); }else{ protocol.open(); } } catch (ProtocolException e) { e.printStackTrace(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } } }

[当我在文本字段中键入任何特殊字符并按下发送按钮时,它将调用JavaScript中的sendMessage(),并在文本区域中显示除%符号之外的值。

[%符号以外的所有特殊字符都起作用。

如何显示%符号?

在我的jsp页面中,

[[[[First Name: ...
java javascript ajax jsp servlets
3个回答
1
投票
首先不要使用escape(),请改用encodeURIComponent()。有关详细信息,请参见this

message = encodeURIComponent(document.getElementById("message").value);

<textarea>而不是.value.innerText中设置值。

textarea.value = textarea.value ? textarea.value + '\n' + checkMsg : checkMsg;

第三,在发出Content-type:application/x-www-form-urlencoded请求时,需要在requestHeader中设置POST。并使用xhr.send(data)发送有效负载,而不用查询字符串传递它。

xmlhttp.open("POST", "SendMessageAction?sid="+Math.random(), true); xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); xmlhttp.send("message=" + message);

全部加起来,假设您的回答是简单的(不是用url编码编码的,

这是不必要的!]])>>var messageEl = document.getElementById("message"); var textareaEl = document.getElementById("textarea"); function sendMessage(){ var xhr = new XMLHttpRequest(); xhr.onload = function(evt) { var checkMsg = xhr.responseText; if (textareaEl.value) textareaEl.value += '\n'; textareaEl.value += checkMsg; }; xhr.onerror = function(evt) { // handle the error. }; xhr.open('POST', "SendMessageAction?sid="+Math.random(), true); xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); xhr.send("message=" + encodeURIComponent(messageEL.value)); }

而且您的问题应自行解决。

我可以看到您已经在转义邮件,但是看起来您还需要URL -encode,然后-decode邮件。 URL中使用+符号表示空格,因此,除非对它们进行编码(除非将其编码为'%2B'),否则所有实际的+符号都将被删除。

在Java语言方面,您可以使用encodeURIComponent()decodeURIComponent(),而在Java语言方面,可以使用URLDecoderURLEncoder

在您的Javascript中添加类似...

var checkMsg = decodeURIComponent(xmlhttp.responseText.toString()); ... xmlhttp.open("POST","SendMessageAction?message=" + encodeURIComponent(message) +"&sid"+Math.random(),true);

...以及在发送/接收消息的Java代码中...

String inMessage = URLDecoder.decode(rawMessage, "UTF-8"); ... return URLEncoder.encoder(outMessage, "UTF-8");

解决方法是,

当使用AJAX将值发送到servlet时,我使用了encodeURIComponent()和encodeURIComponent()而不是escape()。

我的脚本是这样的,

var checkMsg = decodeURIComponent(xmlhttp.responseText.toString()); ... xmlhttp.open("POST","SendMessageAction?message=" + encodeURIComponent(message)

感谢所有支持我的人....

0
投票
我可以看到您已经在转义邮件,但是看起来您还需要URL -encode,然后-decode邮件。 URL中使用+符号表示空格,因此,除非对它们进行编码(除非将其编码为'%2B'),否则所有实际的+符号都将被删除。

0
投票
解决方法是,
© www.soinside.com 2019 - 2024. All rights reserved.