关于jsp做ajax验证处理的做法
我想用jsp做ajax的验证处理页面,应该怎么去写呀?例如就做一个简单的用户名的长度是否满足6-12位,应该怎么来写?关键是我不知道应该怎么去响应结果,是用respons...
我想用jsp做ajax的验证处理页面,应该怎么去写呀?
例如就做一个简单的用户名的长度是否满足6-12位,应该怎么来写? 关键是我不知道应该怎么去响应结果,是用response么?
还有,jsp文件的位置应该放到哪里,我用myEclipse放到tomcat服务器上不能读呀?
求高手解决 展开
例如就做一个简单的用户名的长度是否满足6-12位,应该怎么来写? 关键是我不知道应该怎么去响应结果,是用response么?
还有,jsp文件的位置应该放到哪里,我用myEclipse放到tomcat服务器上不能读呀?
求高手解决 展开
3个回答
展开全部
看你需要什么时候验证,比如当输入框失去焦点时,还是提交表单时,等等,
以前写了一个用prototype.js的AJAX例子,
JSP:
<%@ page contentType="text/html;charset=UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>observer.jsp</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<script type="text/javascript" src="js/prototype.js"></script>
<style type="text/css">
span{
font-size:12pt;
}
</style>
</head>
<body><br>
<div align="center">
<form>
<input type="text" name="username" id="username"/>
<span id="usable" style="color:#00FF00;display:none;"><img src="images/note_ok.gif" />您输入的用户名可以使用。</span>
<span id="unusable" style="color:#FF0000;display:none;"><img src="images/note_error.gif" />对不起,您输入的用户名已经被注册。</span>
</form>
</div>
</body>
</html>
<script type="text/javascript">
new Form.Element.Observer('username', 1, function(el, value){
new Ajax.Request('/ajax/query',{
method: 'get',
parameters:{ actionType:'observer',username: value},
onComplete: function(transport){
if(transport.status == 200){
var flag=transport.responseText;
if(flag=='true'){
$('usable').show();
$('unusable').hide();
}else{
$('unusable').show();
$('usable').hide();
}
}else{
$('cuser').innerHTML=transport.responseText;
}
}
})
});
</script>
JAVA:
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.highcom.teaching.student.domain.UserEntity;
import com.highcom.teaching.student.service.UserServiceImpl;
@SuppressWarnings( { "unchecked", "serial", "unused" })
public class UserServlet extends HttpServlet {
private String actionType;
private UserEntity ue = null;
private UserServiceImpl us = new UserServiceImpl();
public void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
actionType = request.getParameter("actionType");
System.out.println(actionType);
try {
if (actionType == null) {
query(request, response);
} else if (actionType.equals("query")) {
query(request, response);
} else if ("add".equals(actionType)) {
add(request, response);
} else if("updater".equals(actionType)){
updater(request,response);
} else if("periodicalUpdater".equals(actionType)){
periodicalUpdater(request,response);
} else if("observer".equals(actionType)){
observer(request,response);
}
} catch (Exception e) {
e.getMessage();
}
}
public void query(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
new AddThread().run();
List<Map> list = (List) us.query();
System.out.println("list" + list.size());
response.setContentType("text/xml;charset=gb2312");
response.setHeader("Cache-control", "no-cache");
try {
PrintWriter out = response.getWriter();
out.print("<?xml version=\"1.0\" encoding=\"gb2312\"?>");
out.print("<users>");
for (Map map : list) {
out.print("<user>");
out.print("<id>" + map.get("id") + "</id>");
out.print("<name>" + map.get("name") + "</name>");
out.print("<c_time>" + map.get("c_time") + "</c_time>");
out.print("</user>");
}
out.print("</users>");
} catch (IOException e) {
e.printStackTrace();
}
}
public void add(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String name = request.getParameter("username");
new AddThread().run();
us.add(name);
}
public void updater(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
new AddThread().run();
List<Map> list = (List) us.query();
try {
response.setContentType("text/xml;charset=utf-8");
response.setHeader("Cache-control", "no-cache");
PrintWriter out = response.getWriter();
out.print("<div><span>ID</span><span>姓名</span><span class='time'>创建时间</span></div>");
for (Map map : list) {
out.print("<div>");
out.print("<span>" + map.get("id") + "</span>");
out.print("<span>" + map.get("name") + "</span>");
out.print("<span class='time'>" + map.get("c_time") + "</span>");
out.print("</div>");
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void periodicalUpdater(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
List<Map> list = (List) us.queryOne();
try {
response.setContentType("text/xml;charset=utf-8");
response.setHeader("Cache-control", "no-cache");
PrintWriter out = response.getWriter();
out.print("<div><span>ID</span><span>姓名</span><span class='time'>创建时间</span></div>");
for (Map map : list) {
out.print("<div>");
out.print("<span>" + map.get("id") + "</span>");
out.print("<span>" + map.get("name") + "</span>");
out.print("<span class='time'>" + map.get("c_time") + "</span>");
out.print("</div>");
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void observer(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String name = request.getParameter("username");
List<Map> list = (List) us.queryByName(name);
response.setHeader("Cache-control", "no-cache");
PrintWriter out = response.getWriter();
if(list.size()>0){
out.print("false");
}else{
out.print("true");
}
}
}
class AddThread{
public void run() {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
以前写了一个用prototype.js的AJAX例子,
JSP:
<%@ page contentType="text/html;charset=UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>observer.jsp</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<script type="text/javascript" src="js/prototype.js"></script>
<style type="text/css">
span{
font-size:12pt;
}
</style>
</head>
<body><br>
<div align="center">
<form>
<input type="text" name="username" id="username"/>
<span id="usable" style="color:#00FF00;display:none;"><img src="images/note_ok.gif" />您输入的用户名可以使用。</span>
<span id="unusable" style="color:#FF0000;display:none;"><img src="images/note_error.gif" />对不起,您输入的用户名已经被注册。</span>
</form>
</div>
</body>
</html>
<script type="text/javascript">
new Form.Element.Observer('username', 1, function(el, value){
new Ajax.Request('/ajax/query',{
method: 'get',
parameters:{ actionType:'observer',username: value},
onComplete: function(transport){
if(transport.status == 200){
var flag=transport.responseText;
if(flag=='true'){
$('usable').show();
$('unusable').hide();
}else{
$('unusable').show();
$('usable').hide();
}
}else{
$('cuser').innerHTML=transport.responseText;
}
}
})
});
</script>
JAVA:
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.highcom.teaching.student.domain.UserEntity;
import com.highcom.teaching.student.service.UserServiceImpl;
@SuppressWarnings( { "unchecked", "serial", "unused" })
public class UserServlet extends HttpServlet {
private String actionType;
private UserEntity ue = null;
private UserServiceImpl us = new UserServiceImpl();
public void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
actionType = request.getParameter("actionType");
System.out.println(actionType);
try {
if (actionType == null) {
query(request, response);
} else if (actionType.equals("query")) {
query(request, response);
} else if ("add".equals(actionType)) {
add(request, response);
} else if("updater".equals(actionType)){
updater(request,response);
} else if("periodicalUpdater".equals(actionType)){
periodicalUpdater(request,response);
} else if("observer".equals(actionType)){
observer(request,response);
}
} catch (Exception e) {
e.getMessage();
}
}
public void query(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
new AddThread().run();
List<Map> list = (List) us.query();
System.out.println("list" + list.size());
response.setContentType("text/xml;charset=gb2312");
response.setHeader("Cache-control", "no-cache");
try {
PrintWriter out = response.getWriter();
out.print("<?xml version=\"1.0\" encoding=\"gb2312\"?>");
out.print("<users>");
for (Map map : list) {
out.print("<user>");
out.print("<id>" + map.get("id") + "</id>");
out.print("<name>" + map.get("name") + "</name>");
out.print("<c_time>" + map.get("c_time") + "</c_time>");
out.print("</user>");
}
out.print("</users>");
} catch (IOException e) {
e.printStackTrace();
}
}
public void add(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String name = request.getParameter("username");
new AddThread().run();
us.add(name);
}
public void updater(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
new AddThread().run();
List<Map> list = (List) us.query();
try {
response.setContentType("text/xml;charset=utf-8");
response.setHeader("Cache-control", "no-cache");
PrintWriter out = response.getWriter();
out.print("<div><span>ID</span><span>姓名</span><span class='time'>创建时间</span></div>");
for (Map map : list) {
out.print("<div>");
out.print("<span>" + map.get("id") + "</span>");
out.print("<span>" + map.get("name") + "</span>");
out.print("<span class='time'>" + map.get("c_time") + "</span>");
out.print("</div>");
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void periodicalUpdater(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
List<Map> list = (List) us.queryOne();
try {
response.setContentType("text/xml;charset=utf-8");
response.setHeader("Cache-control", "no-cache");
PrintWriter out = response.getWriter();
out.print("<div><span>ID</span><span>姓名</span><span class='time'>创建时间</span></div>");
for (Map map : list) {
out.print("<div>");
out.print("<span>" + map.get("id") + "</span>");
out.print("<span>" + map.get("name") + "</span>");
out.print("<span class='time'>" + map.get("c_time") + "</span>");
out.print("</div>");
}
} catch (IOException e) {
e.printStackTrace();
}
}
public void observer(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String name = request.getParameter("username");
List<Map> list = (List) us.queryByName(name);
response.setHeader("Cache-control", "no-cache");
PrintWriter out = response.getWriter();
if(list.size()>0){
out.print("false");
}else{
out.print("true");
}
}
}
class AddThread{
public void run() {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
展开全部
判断用户名长度用javascript,大概的判断方法:
<script language="javascript">
function checkUser(value){
if(value.length>12 || value.length<6)
alert("Error!");
else
alert("OK!");}
}
</script>
<input type="text" name="user" id="user" onblur="checkUser(this.value)">
jsp文件放到x:/tomcat/webapps/目录下(x是你的硬盘符),建议先安装TOMCAT,后安装MYECLIPSE,在MYECLIPSE属性界面里配置TOMCAT服务器。编写的程序要发布到TOMCAT服务器里才能运行。
<script language="javascript">
function checkUser(value){
if(value.length>12 || value.length<6)
alert("Error!");
else
alert("OK!");}
}
</script>
<input type="text" name="user" id="user" onblur="checkUser(this.value)">
jsp文件放到x:/tomcat/webapps/目录下(x是你的硬盘符),建议先安装TOMCAT,后安装MYECLIPSE,在MYECLIPSE属性界面里配置TOMCAT服务器。编写的程序要发布到TOMCAT服务器里才能运行。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
长度不用 用Ajax判断啊 在text框 用keyup事件就行了 到是判断用户名重复,需要用Ajax 因为你要无刷新的告诉用户 这个用户名是否重复 不然 输入一个字页面刷新一次要命了都,本人在开发web项目 有需要M 466169163
追问
我知道那个可以用javaScript来实现,但我现在想知道的是怎么用ajax来实现呀...
追答
1、第一步,下载ajax所需要的js 我下的是 jquery-1.3.2.js (可在网上下载)
2、需要用Ajax的页面上导入JS //导入下载的JS
3、把这个复制到 页面JS区域function方法里 或是你单独定义的JS
function checkUserName(){ //text框触发的事件
jQuery.ajax({
type: 'POST',
url: encodeURI(""),//这里写请求路径 比如要验证用户 把用户名作为参数传过去 跟请求是一样的 ->请看4、action方法
datatype:'text',
cache:false,
async: true,
success: function(status) { //这里是action有值返回JS作的处理
if(status=='yes'){
//这里提示用户 用户名可用
}else{
//提示不可用
}
}
});
4、action 方法
action原来是处理业务后 用 findForward 跳转 ,Ajax 采用读写的方式,比如验证用户名是否存在
public ActionForward checkUserName(...{
request.getParameter("");//你刚传过来的username
用方法判断用户名是否存在
PrintWriter out = response.getWriter(); //获得对象PrintWriter对象
if(用户名存在){
out.print("no");
}else{
out.print("yes");
}
out.close();
return null;
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询