prototye+java实现ajax登录实例

用prototype开源框架写了一个简单的ajax登录程序。

TestAjax.jsp

 

<% @ page language="java" import="java.util.*" pageEncoding="GBK" %>

<%
    
String contextPath = request.getContextPath();
%>

< html >
< head >
< title > 操作完成 </ title >
< meta  http-equiv ="Content-Type"  content ="text/html; charset=GBK" >
< link  rel ="stylesheet"  href ="<%=contextPath%>/mc/common/css/grayStyle.css" >
< script  language ="JavaScript"  src ="<%=contextPath%>/mc/common/include/js/prototype.js" ></ script >
</ head >
< script  type ="text/javascript" >
function login(){

    
var name = $F("name");
    
var psd = $F("password");
    
var paras = 'name='+name+'&password='+psd;
  
var url = "<%= contextPath %>/mc/inducorp/mc/di/agreement/test/TestAjaxServlet";
  
var myAjax = new Ajax.Updater(
              
'result',
            url,
            
{
                method: 
'get',
                parameters: paras
            }
);

    }


      
function showResponse(originalRequest)
        
{

       $(
"result").value=originalRequest.responseText;
        
//alert("showResponse:"+originalRequest.responseText);

        }



</ script >
< body  >
< form  action  = ""  method ="get"  name ="agreementform" >
  
< label > Name:
  
< input  type ="text"  name ="name"  id ="name" >
  
</ label >


  
< p >
    
< label > password:
    
< input  type ="text"  name ="password"  id ="password" >
    
</ label >
  
</ p >
  
< p >
    
< label >
    
< input  type ="button"  name ="button"  id ="button"  value ="Login"  onClick ="login()" >
    
</ label >
  
</ p >
  
< p > &nbsp; </ p >
  
< p > &nbsp; </ p >
  
< p >< span  id ="content" >
  
< textarea  id =result  cols =60  rows =10  ></ textarea >

  
</ span ></ p >
</ form >
</ body >
</ html >

在Ajax.Updater构造方法中传入了一个元素 id,用于显示后台处理后传回的结果。

TestAjaxServlet.java

 

package  com.icss.mc.inducorp.mc.di.agreement.test;

import  java.io.Writer;

import  javax.servlet.http.HttpServletRequest;
import  javax.servlet.http.HttpServletResponse;

import  com.icss.mc.base.servlet.BaseServlet;


public   class  TestAjaxServlet  extends  BaseServlet  {

    
public void performTask(HttpServletRequest request,
            HttpServletResponse response) 
throws Exception {

        String name 
= this.getParameter(request, "name"true,true,false,"");
        String password 
= this.getParameter(request, "password",true,true,false,"");
        log.debug(
"登录:"+name+":"+password);
        String msg 
= "登录失败!";
        
if(name.equals("test")&&password.equals("test")){
            msg 
= "登录成功!";
        }


        response.setContentType(
"text/xml; charset=UTF-8");
        response.setHeader(
"Cache-Control","no-cache");

        StringBuffer sb 
= new StringBuffer();
        sb.append(msg);
        Writer writer 
= response.getWriter();
        writer.write(sb.toString());
        writer.flush();
        writer.close();


    }


}

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
本课程详细讲解了以下内容:    1.jsp环境搭建及入门、虚拟路径和虚拟主机、JSP执行流程    2.使用Eclipse快速开发JSP、编码问题、JSP页面元素以及request对象、使用request对象实现注册示例    3.请求方式的编码问题、response、请求转发和重定向、cookie、session执行机制、session共享问题     4.session与cookie问题及application、cookie补充说明及四种范围对象作用域     5.JDBC原理及使用Statement访问数据库、使用JDBC切换数据库以及PreparedStatement的使用、Statement与PreparedStatement的区别     6.JDBC调用存储过程和存储函数、JDBC处理大文本CLOB及二进制BLOB类型数据     7.JSP访问数据库、JavaBean(封装数据和封装业务逻辑)     8.MVC模式与Servlet执行流程、Servlet25与Servlet30的使用、ServletAPI详解与源码分析     9.MVC案例、三层架构详解、乱码问题以及三层代码流程解析、完善Service和Dao、完善View、优化用户体验、优化三层(加入接口和DBUtil)    1 0.Web调试及bug修复、分页SQL(Oracle、MySQL、SQLSERVER)     11.分页业务逻辑层和数据访问层Service、Dao、分页表示层Jsp、Servlet     12.文件上传及注意问题、控制文件上传类型和大小、下载、各浏览器下载乱码问题     13.EL表达式语法、点操作符和中括号操作符、EL运算、隐式对象、JSTL基础及set、out、remove     14.过滤器、过滤器通配符、过滤器链、监听器     15.session绑定解绑、钝化活化     16.以及Ajax的各种应用     17. Idea环境下的Java Web开发

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值