简体   繁体   English

JSP聊天-如何将HTML中的值解析为JSP页面?

[英]JSP Chat - How I can parse the value from HTML to JSP page?

I have to make a chat with JSP, AJAX and Java and I have a problem: when I try to use my variable to store value of a input text, this variable is null. 我必须与JSP,AJAX和Java进行聊天,但遇到一个问题:当我尝试使用变量存储输入文本的值时,此变量为null。 If I add 'action' property to the form, the variable 'textParam' will have the value of the input text, but, if I do that I have to redirect with action to a page and I don't what that. 如果我在表单中添加'action'属性,则变量'textParam'将具有输入文本的值,但是,如果这样做,我必须将action重定向到页面,而我不是那样。

I need to process something bigger in the JSP page and then to reload in the HTML page (which is a JSP page) (the reload part is not on actual probem). 我需要在JSP页面中处理更大的东西,然后在HTML页面(这是一个JSP页面)中重新加载(重新加载部分不在实际的probem上)。

How I can make to populate 'textParam' with the input text value when I press the button? 当我按下按钮时,如何使输入的文本值填充'textParam'?

PS: I need to make it with pure javascript, not with some libraries :) PS:我需要使用纯JavaScript而不是某些库来实现它:)

The JSP which have to process is: 必须处理的JSP是:

String textParam = request.getParameter("chatMessage");
System.out.println("textParam = " + textParam);

My form it look like that: 我的表格看起来像这样:

<form id="frmmain" name="frmmain" onsubmit="return blockSubmit();">
        <input type="text" id="chatMessage" name="chatMessage" style="width: 447px;" />
        <input type="button" name="btn_send_chat" id="btn_send_chat" value="Send" onclick="sendChatText();" />
</form>

The .js file it's this: .js文件是这样的:

var request = getXmlHttpRequestObject();
var response = getXmlHttpRequestObject();
var lastMessage = 0;
var mTimer;

function getXmlHttpRequestObject() {                
    if (window.XMLHttpRequest) {                    
        return new XMLHttpRequest();
    } else if(window.ActiveXObject) {                   
        return new ActiveXObject("Microsoft.XMLHTTP");
    }
}

function sendChatText() {
    if(document.getElementById('chatMessage').value == '') {
        alert("You have not entered a message");
        return;
    }
    if (request.readyState == 4 || request.readyState == 0) {
        request.open("POST", 'getChat2.jsp?chat=1&last=' + lastMessage, true);
        request.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
        request.onreadystatechange = handleSendChat; 
        var param = 'message=' + document.getElementById('chatMessage').value;                                  
        param += '&chat=1';
        request.send(param);
        document.getElementById('chatMessage').value = '';
    }                           
}

function handleSendChat() {             
    clearInterval(mTimer);
    getChatText();
}

function blockSubmit() {
    sendChatText();
    return false;
}

The problem is here: 问题在这里:

String textParam = request.getParameter("chatMessage");

I was trying to get 'chatMessage' parameter, which it was only the name of the input. 我试图获取'chatMessage'参数,它只是输入的名称。 The solve is to get 'message' param which it was defined and requested in js: 解决的办法是获取在js中定义和请求的“消息”参数:

String textParam = request.getParameter("message");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM