Java incoming user name and password and automatically submits form implementation login to other systems

Source: Internet
Author: User

Instead of single sign-on, simulate the login page form of a remote project, and automatically submit a form to this item's login action when you visit this page to enable logging in to other systems.

SSH Framework Project

1. The following is the action code for the Local system:

1 Importjava.io.IOException;2 Importjava.util.List;3 ImportJava.io.BufferedReader;4 Importjava.io.IOException;5 ImportJava.io.InputStreamReader;6 ImportJava.io.PrintWriter;7 ImportJava.net.URL;8 Importjava.net.URLConnection;9 Ten  Public classmyloginaction { One  A     /** - * Query If user is registered -      * @return the      * @throwsException -      */ -      Public voidCheckUser ()throwsexception{ -Loginer Loginer =(Loginer) request.getsession () +. getattribute ("Loginer"); -          +String url = "http://www.youtest.com/login.php";//Remote system login action address AString param = "username=tom&password=123456";//Parameters atString temp = "alert (' User name or password error ');";//The returned information, here is the error message, is used for comparison. Subject to availability -         Booleanresult =false ; -         //Verify data is logged in -result =sendpost (URL, param, temp); -         if(Result) { -             return"Login"; in}Else{ -             return"Register"; to         } +         } -  the     //Accessing the remote login action and getting the returned information *      Public Static Booleansendpost (string url, string param, String temp) { $PrintWriter out =NULL;Panax NotoginsengBufferedReader in =NULL; -             Booleanresult =true; the             Try { +URL Realurl =Newurl (url); A                     //opening and linking between URLs theURLConnection conn =realurl.openconnection (); +                     //to set common request properties -Conn.setrequestproperty ("Accept", "*/*"); $Conn.setrequestproperty ("Connection", "keep-alive"); $Conn.setrequestproperty ("User-agent", "mozilla/4.0" (compatible; MSIE 6.0; Windows NT 5.1; SV1) "); -                     //to send a POST request, you must set the following two lines -Conn.setdooutput (true); theConn.setdoinput (true); -                     //gets the output stream corresponding to the URLConnection objectWuyiout =NewPrintWriter (Conn.getoutputstream ()); the                     //Send Request Parameters - out.print (param); Wu                     //buffer for flush output stream - Out.flush (); About                     //defines the response of the BufferedReader input stream to read the URL $in =NewBufferedReader (NewInputStreamReader (Conn.getinputstream (), "UTF-8")); - String Line; -                      while(line = In.readline ())! =NULL) { -                         if(Temp.equals ((Line.trim () ))) { ASystem.out.println ("Wrong line:" +Line ); +result =false; the                         } -                     } $}Catch(Exception e) { theresult =false; theLogger.error ("Send POST request exception!") "+e); theSYSTEM.OUT.PRINTLN ("Send POST request exception!") "+e); the e.printstacktrace (); -}finally{ in                     Try{ the                         if(out!=NULL){ the out.close (); About                         } the                         if(in!=NULL){ the in.close (); the                         } +}Catch(IOException ex) { - Logger.error (ex); the ex.printstacktrace ();Bayi                     } the             } the             returnresult; -         }  -}

2. The demo login page:

1 <HTML>2 <Head></Head>3 <Body>4        <Scripttype= "Text/javascript">5          variframe=Document.createelement ("iframe");6 iframe.src= "http://www.youtest.com/login.php?uname=<%=username%>&upwd=<%=pwd%>";7 Iframe.style.display="None";8          9          varSTA="false;"Ten          if(iframe.attachevent) { One Iframe.attachevent ("onload", function(){ A window.location.href="http://www.youtest.com/index.html"; -              }); -          } Else { the Iframe.onload= function(){ - window.location.href="http://www.youtest.com/index.html"; -              }; -          } + Document.body.appendChild (IFRAME); -  </Script> +    </Body> A </HTML>

Java incoming user name and password and automatically submits form implementation login to other systems

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.