Prototye+java Implementation Ajax Login Instance

Source: Internet
Author: User

A simple AJAX login program was written with the prototype open source framework.

testajax.jsp



<% ... @ Page language="java" import=" java.util.* " pageencoding = " GBK " %>

<% ...
String contextpath = request.getcontextpath ();
%>

< HTML >
< Head >
< title > Operation Complete </ 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" > ...
functionLogin ()...{

varname=$F ("name");
varPSD=$F ("Password");
varParas= ''name=''+name+''&password=''+PSD;
varURL= "<%= contextpath%>/mc/inducorp/mc/di/agreement/test/testajaxservlet";
varMyajax= NewAjax.updater (
'' Result'',
Url
...{
Method:
'get',
Parameters:paras
}
);

}


functionShowresponse (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 >

An element ID is passed in the Ajax.updater construction method to display the results returned after background processing.

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 voidPerformtask (HttpServletRequest request,
HttpServletResponse response)
throwsException...{

String name
=  This. GetParameter (Request,"name", true,true,false,"");
String Password
=  This. GetParameter (Request,"Password",true,true,false,"");
Log.debug (
"Login:"+name+":"+password);
String msg
= "Login failed! ";
if(Name.equals ("Test")&&Password.equals ("Test"))...{
msg
= " login Successful!" ";
}


Response.setcontenttype (
"text/xml; charset=utf-8.");
Response.setheader (
"Cache-control","No-cache");

StringBuffer SB
= NewStringBuffer ();
Sb.append (msg);
Writer Writer
=Response.getwriter ();
Writer.write (Sb.tostring ());
Writer.flush ();
Writer.close ();


}


}

</

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.