服务器间进行通信只能通过流(Stream)的方式进行,不能用方法的返回值。
1.Java代码创建一个连接并请求该连接返回的数据
doGet()方法,execute()方法中调用
packagedemo2.x.com; importjava.io.BufferedReader; importjava.io.InputStream; importjava.io.InputStreamReader; importjava.net.HttpURLConnection; importjava.net.URL; importjavax.print.attribute.standard.RequestingUserName; importjavax.servlet.http.Cookie; importjavax.servlet.http.HttpServletRequest; importorg.apache.struts2.ServletActionContext; importcom.opensymphony.xwork2.ActionSupport; /** * Demo2主页,访问主页要先验证cookie * * @author: qlq * @date : 2017年8月29日下午12:02:31 */ public class Demo2Action extendsActionSupport { privateString gotoUrl; publicString getGotoUrl() { returngotoUrl; } public voidsetGotoUrl(String gotoUrl) { this.gotoUrl =gotoUrl; } @Override public String execute() throwsException { HttpServletRequest request =ServletActionContext.getRequest(); Cookie cookies[] =request.getCookies(); if (cookies != null) { for(Cookie cookie : cookies) { if ("ssocookie".equals(cookie.getName())) { String result = this.doGet("http://check.x.com:8080/sso/checkCookie.action", cookie.getName(), cookie.getValue()); if ("1".equals(result)) { returnSUCCESS; } } } } //登陆失败后将gotoUrl写到JSP页面 gotoUrl = "http://demo2.x.com:8080/demo2/main.action"; returnLOGIN; } publicString doGet(String url, String cookieName, String cookieValue) { //用于接收返回的数据 StringBuffer sb = newStringBuffer(); //创建一个连接的请求 HttpURLConnection httpURLConnection = null; try{ //包装请求的地址 URL urls = new URL(url + "?cookieName=" + cookieName + "&cookieValue=" +cookieValue); //打开连接 httpURLConnection =(HttpURLConnection) urls.openConnection(); httpURLConnection.setRequestMethod("GET"); //通过BufferReader读取数据 InputStream iStream =httpURLConnection.getInputStream(); InputStreamReader inputStreamReader = newInputStreamReader(iStream); BufferedReader bReader = newBufferedReader(inputStreamReader); String temp = null; while ((temp = bReader.readLine()) != null) { sb.append(temp); } //关闭流(先开后关,后开先关) bReader.close(); inputStreamReader.close(); iStream.close(); } catch(Exception e) { e.printStackTrace(); } finally{ if (httpURLConnection != null) { //关闭连接 httpURLConnection.disconnect(); } } returnsb.toString(); } }
2.接收请求的连接
checkCookie()方法
packagecheck.x.com; importjava.awt.image.VolatileImage; importjava.io.IOException; importjavax.servlet.http.Cookie; importjavax.servlet.http.HttpServletResponse; importorg.apache.struts2.ServletActionContext; importcom.opensymphony.xwork2.ActionSupport; importcom.sun.org.apache.bcel.internal.generic.IF_ACMPEQ; importcheck.x.com.utils.CheckCookie; public class LoginAction extendsActionSupport { privateString username; privateString password; privateString gotoUrl; privateString cookieName; privateString cookieValue; publicString getCookieName() { returncookieName; } public voidsetCookieName(String cookieName) { this.cookieName =cookieName; } publicString getCookieValue() { returncookieValue; } public voidsetCookieValue(String cookieValue) { this.cookieValue =cookieValue; } publicString getGotoUrl() { returngotoUrl; } public voidsetGotoUrl(String gotoUrl) { this.gotoUrl =gotoUrl; } publicString getUsername() { returnusername; } public voidsetUsername(String username) { this.username =username; } publicString getPassword() { returnpassword; } public voidsetPassword(String password) { this.password =password; } @Override public String execute() throwsException { boolean OK = this.check(); if(OK) { Cookie cookie = new Cookie("ssocookie", "sso"); //设置cookie的作用范围是父域(.x.com) cookie.setDomain(".x.com"); //斜杠代表设置到父域的顶层,也就是父域下的所有应用都可访问 cookie.setPath("/"); HttpServletResponse response =ServletActionContext.getResponse(); //增加cookie,未设置生命周期默认为一次会话 response.addCookie(cookie); returnSUCCESS; } return null; } public void checkCookie() throwsIOException{ String result="0"; if(CheckCookie.checkCookie(cookieName, cookieValue)){ result="1"; } HttpServletResponse response =ServletActionContext.getResponse(); response.getWriter().print(result); response.getWriter().close(); } public booleancheck() { if ("user".equals(username) && "123".equals(password)) return true; return false; } }
简单的使用方法可以参考: