[Extjs Login] The form is submitted by ajax, And the logon instance background is servlet

Source: Internet
Author: User

Js Code:

Ext. onReady (function () {var _ window = new Ext. window ({title: "login", width: 300, height: 150, layout: "form", plain: true, bodyStyle: "padding: 5px", labelWidth: 45, resizable: false, defaults: {xtype: "textfield", width: "200"}, items: [{fieldLabel: "username", name: "username", id: "username", allowBlank: false, blankText: "user name cannot be blank"}, {fieldLabel: "password", inputType: "password", name: "userpwd", id: "userpwd", allowBlank: false, blankText: "password cannot be blank"}], buttons: [{text: "login", type: "submit", handler: function () {var _ username = Ext. getCmp ('username '). getValue (); var _ userpwd = Ext. getCmp ('userpwd '). getValue (); Ext. messageBox. buttonText. OK = "OK"; // change the word on the button to OK if (_ username = "") {Ext. msg. alert ("prompt", "username cannot be blank, please enter username");} else if (_ userpwd = "") {Ext. msg. alert ("prompt", "the password cannot be blank, please enter the login password");} else {Ext. messageBox. show ({title: "Please wait", msg: "loading... ", progress: true, // progress bar for rendering. The default value is 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 ("prompt", "Logon successful"); else {Ext. msg. alert ("prompt", "Logon Failed") ;}, failure: function (response, opts) {Ext. msg. alert ("prompt", "Logon Failed") ;}, params :{ username: _ username }}}, {text: "canceled"}]}); _ window. show ();});

  

Add web. xml:

<Servlet> <servlet-name> login </servlet-name> <servlet-class> servlet address </servlet-class> </servlet> <servlet-mapping> <servlet- name> login </servlet-name> <url-pattern>/loginTest </url-pattern> </servlet-mapping>

 

 

Servlet code:

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);}}

  

The effect is as follows:

 

Code download

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.