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);}}
效果如下:
代碼下載