【Extjs登陸】表單ajax提交,登入執行個體 後台為servlet

來源:互聯網
上載者:User

js代碼:

Ext.onReady(function(){var _window=new Ext.Window({   title:"登入",   width:300,   height:150,   layout:"form",   plain:true,   bodyStyle:"padding:5px",   labelWidth:45,   resizable:false,   defaults:{xtype:"textfield",width:"200"},   items:[{    fieldLabel:"使用者名稱",    name:"username",    id:"username",    allowBlank:false,    blankText:"使用者名稱不可為空"   },{    fieldLabel:"密 碼",    inputType:"password",    name:"userpwd",    id:"userpwd",    allowBlank:false,    blankText:"密碼不可為空"   }],   buttons:[{    text:"登入",    type:"submit",    handler:function(){     var _username=Ext.getCmp('username').getValue();     var _userpwd=Ext.getCmp('userpwd').getValue();     Ext.MessageBox.buttonText.ok="確定";//換按鈕上的字,將ok換為確定     if(_username=="")     {           Ext.Msg.alert("提示","使用者名稱不可為空,請輸入使用者名稱");     }else if(_userpwd=="")     {      Ext.Msg.alert("提示","密碼不可為空,請輸入登入密碼");     }     else{        Ext.MessageBox.show({       title:"請稍等",       msg:"正在載入...",         progress:true,//渲染進度條,預設是false         closable:true,         animEl:"loading"      });      var f = function(v) {              return function(){                var i = v / 11;                Ext.MessageBox.updateProgress(i, '');               }             }               Ext.Ajax.request({          url:"http://192.168.0.100:8088/ajaxTest/loginTest",          method:"post",          success:function(response,opts){                var sf=response.responseText;                if(sf=="success")                 Ext.Msg.alert("提示","登入成功");               else{                   Ext.Msg.alert("提示","登入失敗");                  }               },          failure:function(response,opts){                    Ext.Msg.alert("提示","登入失敗");          },          params:{             username:_username          }     })    }    }      },{    text:"取消"   }]});_window.show();});

  

web.xml添加:

<servlet><servlet-name>login</servlet-name><servlet-class>servlet地址</servlet-class></servlet><servlet-mapping><servlet-name>login</servlet-name><url-pattern>/loginTest</url-pattern></servlet-mapping>

 

 

servlet代碼:

package com.wjl.test;import java.io.IOException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class LoginTest extends HttpServlet {private static final long serialVersionUID = 1L;public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException{   response.setContentType("text/html");   response.setCharacterEncoding("utf-8");   String str=request.getParameter("username");   if(str.equals("admin"))   {    response.getWriter().print("success");   }   else{    response.getWriter().print("failure:ture");   }  }public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException{   doGet(request,response);}}

  

效果如下:

 

 代碼下載
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.