A. javaweb寫的聊天網頁是如何向客戶端發送消息
你說的這個是一種推送技術,由伺服器主動將數據推送給客戶端
Tomcat8已經很好的支持web socket 了webSocket已經慢慢普及與Html5做這樣的推送
而你用javaweb的話,直接導入Tomcat.jar就可以使用webSocket了
網上有很多例子
B. 在java Web中如何用Ajax實現用戶名已存在
我給你做一個例子:希望能幫到你。
實現的功能:注冊頁面上當輸入逗lixin地時,顯示該用戶已被注冊。其他的名稱無所謂。希望能幫到你。歡迎追問。
一個簡單的jsp頁面:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//姿野W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<script type="text/javascript" language="javascript">
//根據瀏覽器的不同創察凳建不同的XMLHttpRequest
function createXmlHttpRequest(){
var xmlreq=false;
if(window.XMLHttpRequest){
xmlreq=new XMLHttpRequest();
}else if(window.ActiveXobject){
try{
xmlreq = new ActiveXobject("Msxm12.XMLHTTP");
}catch(e1){
try{
xmlreq = new ActiveXobject("Miscoft.XMLHTTP");
}catch(e2){
}
}
}
return xmlreq;
}
//
function usernameCheck(){
var username = document.all.username.value;//獲得text的值
var request = createXmlHttpRequest();//創建request的對象
request.open("post","servlet/ValidationServlet?username="+username);
request.send();
request.onreadystatechange = function(){
if(request.readyState==4&request.status==200)
{
var value = request.responseText;
if(value=="true"){
document.all.unc.innerHTML="該用戶名已經被注冊";}
}else{
document.all.unc.innerHTML="該用戶可以注冊";
}
}}
</script>
</head>
<body>
用戶姓名:<input type ="text" name="username" onblur="usernameCheck()" /><font color="red" size="-1" id="unc"></font>
<br>
用戶密碼:<input type ="password" name= "userpw" />
</body>
</html>
用到敗冊旅的Servlet:
package sample;
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;
public class ValidationServlet extends HttpServlet {
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
String username = request.getParameter("username");
if(username.equals("lixin")){
response.getWriter().print("true");}
else{
response.getWriter().print("false");
}
}
}
C. 聊天室發言用javaweb什麼實現
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding right ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<title>Apache Tomcat WebSocket Examples: Chat</title>
<style type="text/css"><![CDATA[
input#chat {
width: 410px
}
#console-container {
width: 400px;
}
#console {
border: 1px solid #CCCCCC;
border-right-color: #999999;
border-bottom-color: #999999;
height: 170px;
overflow-y: scroll;
padding: 5px;
width: 100%;
}
#console p {
padding: 0;
margin: 0;
}
]]></style>
<script type="application/javascript"><![CDATA[
"use strict";
var Chat = {};
Chat.socket = null;
Chat.connect = (function(host) {
if ('WebSocket' in window) {
Chat.socket = new WebSocket(host);
} else if ('MozWebSocket' in window) {
Chat.socket = new MozWebSocket(host);
} else {
Console.log('Error: WebSocket is not supported by this browser.');
return;
}
Chat.socket.onopen = function () {
Console.log('Info: WebSocket connection opened.');
document.getElementById('chat').onkeydown = function(event) {
if (event.keyCode == 13) {
Chat.sendMessage();
}
};
};
Chat.socket.onclose = function () {
document.getElementById('chat').onkeydown = null;
Console.log('Info: WebSocket closed.');
};
Chat.socket.onmessage = function (message) {
Console.log(message.data);
};
});
Chat.initialize = function() {
if (window.location.protocol == 'http:') {
Chat.connect('ws://' + window.location.host + '/websocket/chat');
} else {
Chat.connect('wss://' + window.location.host + '/websocket/chat');
}
};
Chat.sendMessage = (function() {
var message = document.getElementById('chat').value;
if (message != '') {
Chat.socket.send(message);
document.getElementById('chat').value = '';
}
});
var Console = {};
Console.log = (function(message) {
var console = document.getElementById('console');
var p = document.createElement('p');
p.style.wordWrap = 'break-word';
p.innerHTML = message;
console.appendChild(p);
while (console.childNodes.length > 25) {
console.removeChild(console.firstChild);
}
console.scrollTop = console.scrollHeight;
});
Chat.initialize();
document.addEventListener("DOMContentLoaded", function() {
// Remove elements with "noscript" class - <noscript> is not allowed in XHTML
var noscripts = document.getElementsByClassName("noscript");
for (var i = 0; i < noscripts.length; i++) {
noscripts[i].parentNode.removeChild(noscripts[i]);
}
}, false);
]]></script>
</head>
<body>
<div class="noscript"><h2 style="color: #ff0000">Seems your browser doesn't support Javascript! Websockets rely on Javascript being enabled. Please enable
Javascript and reload this page!</h2></div>
<div>
<p>
<input type="text" placeholder="type and press enter to chat" id="chat" />
</p>
<div id="console-container">
<div id="console"/>
</div>
</div>
</body>
</html>
客戶端的代碼也是很簡單,就是載入頁面的時候,創建跟伺服器的WebSocket連接。
Chat.connect('ws://' + window.location.host + '/websocket/chat');
然後就是發送信息,接收信息了。
完成上述代碼之後,就可以部署了。這里我使用的servlet容器是tomcat 8。以下是我的配置:
<Context path="/websocket" docBase="/Users/cevin/Documents/workspace/tomcat_websocket_chat/web" reloadable="true"/>
部署結束,啟動tomcat,訪問:http://localhost:8080/websocket/chat.xhtml,見到下面這個頁面,說明部署成功了。
D. java web 後端,需要aJax 么
ajax是前端請求後台所用的前端框架。後台開發是不需要用到ajax的。不過個人建議後台還是學一下ajax。技多不壓身。
E. javaweb+ajax實現修改用戶信息:
你好高消, 我說下思塌塵路吧, 首先, 建一個修改按鈕, 輸入框開始不顯示就將它們隱藏(display:none). 修改按鈕團念禪增加點擊事件, 點擊後輸入框顯示, 點擊其它地方後觸發ajax提交, 根據ajax返回結果進行相應的顯示.
F. Javaweb jquery中 ajax 請求回來的值都是String類型的嗎怎麼轉換成map或者list類型
首先,ajax回來的肯定是string 。
其次,一般是約定為json字元串的格式進行傳輸,js接收到jsonstring格式的數據後,使用
JSON.parse(data)轉換為json對象,之後按照正圓肢常處理就可以了。
$.ajax({
url:"..",
data:"parameters",
凱模success:function(e)
{
varjsonobj=console.log(JSON.parse(e));//接收到的string類型轉為json對象
}
盯腔緩})
G. java web開發,ajax請求返回整個裝飾頁面問題
因為ajax返哪判回的頁面其孫緩毀實根本沒變化,只是改變其中的樣式,例如:添加成功後,顯示這些字體。
如果改成後台的話,你載入這個頁面的時候,是相當於刷新了整個頁面。所以東西是最初你寫的jsp的效果,而js的效果無法改變。
所以,之後為了突破,才研製出了則備jQuery和ajax。
H. java web中如何用ajax技術來檢驗注冊的用戶名資料庫中是否存在的實例求大神賜教!!!!
<script>
$(document).ready(function(){
//為inputForm注冊validate函數
$("#inputForm").validate({
rules:{
username:{
remote:"${ctx}/user/user!checkUser.action"
}
},
messages:{
username:{
remote:"用戶名已存在"
}
}
});
});
</script>
用jquery.validate實現。其中,inputForm是你form表單的id,username是用戶名輸入框的name屬性,remote後邊是action的鏈接,checkUser返回true或是false(當然得是json格式的)
I. javaweb的ajax的問題,大神幫我看看下面拿錯了,為什麼就是運行不出來
有個簡單的方法,先引下凳孫枯包:ext-base.js和ext-all.js
<script type="text/棗洞javascript"
src="<%=basePath%>/js/Ext/adapter/ext/ext-base.js"></script>
<script type="text/javascript"
src="<%=basePath%>/js/Ext/ext-all.js"></script>
然後 :
<script type="text/javascript">
founction text(){
var url="tyUser.do?method=chackNextMonth";
Ext.Ajax.request({
url : url ,
method: 'get',
success: function ( result, request ) {
res = result.responseText;
var json = eval(res);
var f=json[0].flage.split(",");
if(f[0]=="true"){
if(confirm("下個月數據已存在,是否要重新生成")){
window.location.href="tyUserList.do?method=makeDuty";
}
}else{
window.location.href="tyUserList.do?method=makeDuty";
}
}
});
}
</script>
前台傳個json串:凱殲
方法里{
JSONObject jitem = new JSONObject();
JSONArray json = new JSONArray();
String flage="false";
jitem.put("flage", flage);
json.put(jitem);
response.setCharacterEncoding("utf-8");
response.getWriter().write(json.toString());
}
J. 在java Web中如何用Ajax實現用戶名已存在
在jsp 頁面使用jquery 或者是javascript 將你的表單進行ajax 提交。然後訪問,羨則碧你的servlet 進行傳遞到service -在其中組裝你的那個對象。然後訪問 進行和資料庫比對。判斷你的結果集。兄舉
一般都是使用 json 字元串進行傳遞數據。
然後在你的頁面中進行判斷。返回的數據。 jquery.ajax(); 就很好。
你可以直接定義返回的類型是json。然後你就能直接使用了。
如果返回沒有這個用盯渣戶名。哪么就顯示在一個span中。如果有就顯示alert("用戶已存在");或者也是顯示在一個span中。