STRTUS2 Framework uses HttpServletResponse response data

Source: Internet
Author: User

-----------------------------------------------------------------------------------------JSP----------------------------- ---------------------------------------------------

<script type= "Text/javascript" src= "/struts2.0/js/jquery-1.11.3.min.js" ></SCRIPT>
<script Type= "Text/javascript";
function Checkusername () {
//Gets the value of the text box
var usernamevalue = $ ("#username"). Val ();
Send request to Server
$.post ("/struts2.0/checkusername", {"username": usernamevalue},function (data) {
//var jsonobject = Eval ("(" +data+ ")");
var html= "";
if (data.flag) {
html= "<font color= ' green ' >" +data.message+ "</font>";
} else{
html= "<font color= ' Red ' >" +data.message+ "</font>";
}
$ ("#username_msg"). HTML (HTML);
}, "json");
}
</script>
<body>
username:<input type= "text" name= "username" id= " Username "onblur=" checkusername () "><span id=" username_msg "></SPAN><BR>
password:< Input type= "password" name= "password" ><BR>
</body>

-----------------------------------------------------------------------------------------Action-------------------------- ------------------------------------------------------

public class Loginaction {
public void Checkusername () {
1. Acceptance of data
String username = servletactioncontext.getrequest (). GetParameter ("username");
Result result = new result ();
2. Determine if username is available
if ("Tom". Equalsignorecase (username)) {
Result.setflag (FALSE);
Result.setmessage ("User name is occupied");
}else{
Result.setflag (TRUE);
Result.setmessage ("User name can be used");
}
Convert result to JSON
String JSON = jsonobject.tojsonstring (result);
try {
Response to Browser via response
Servletactioncontext.getresponse (). setcharacterencoding ("Utf-8");
Servletactioncontext.getresponse (). Getwriter (). write (JSON);

STRTUS2 Framework uses HttpServletResponse response data

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.