A simple registration reflection (written in the servlet)

Source: Internet
Author: User

1      Public voidDoPost (httpservletrequest request, httpservletresponse response)2     throwsservletexception, IOException {3 4         Try{5Request.setcharacterencoding ("Utf-8");6Response.setcharacterencoding ("Utf-8");7Response.setcontenttype ("text/html");8PrintWriter out =Response.getwriter ();9             TenString nstr = request.getparameter ("name"); OneString pstr = Request.getparameter ("pwd"); A              -             /********* start reflection mechanism ***********************************************************************************/ -             //1. Create a class from a package theclass<?> C = class.forname ("entity. User ");  -             //2. Creating objects based on classes -User obj =(User) c.newinstance (); -             //3. Find all the methods of the object +Method[] Methods =Obj.getclass (). Getdeclaredmethods (); -              +             //4. Create StringBuffer object to encapsulate object properties into Jeson format AStringBuffer SB =NewStringBuffer ();  atSb.append ("{"); -              for(Method method:methods) { -                 if(Method.getname (). StartsWith ("Get")) { -Object o =Method.invoke (obj); -Sb.append ("\" "+method.getname (). ReplaceAll (" Get "," ") +" \ ""); -Sb.append (":"); inSb.append ("\" "+o+" \ ""); -Sb.append (","); to                 } +  -             } theSb.deletecharat (Sb.length ()-1); *Sb.append ("}"); $             Panax Notoginseng             //5. Judgment method, operation for attribute assignment -              for(Method method:methods) { the                 if(Method.getname (). Equals ("SetName")) { + method.invoke (obj, nstr); A                 } the                 if(Method.getname (). Equals ("SetPwd")) { + method.invoke (obj, pstr); -                 } $             } $             /******************* above the reflection part to this end *********** is normal business interaction **************************************/ -             //6. Business with service tier -Iuserservice Service =NewUserserviceimpl (); the             intresult =Service.returnregisterresult (obj); -             if(Result ==1 ){WuyiRequest.getrequestdispatcher ("page/loginsuccess.jsp"). Forward (request, response); the}Else { -Out.println ("<script>"); WuOut.println ("alert (' Username or password is wrong! ‘);"); -Out.println ("location=" + request.getcontextpath () + "/page/register.jsp" "); AboutOut.println ("</script>"); $             } -}Catch(Exception e) { - e.printstacktrace (); -     } A}

A simple registration reflection (written in the servlet)

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.