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中。