标签:
HttpClient_001_初步实现项目01的servlet,与项目02的servlet,之间数据访问
代码下载地址:
http://download.csdn.net/detail/poiuy1991719/8594485
项目01:
URLUtil类:
package com.west.test.httpclient; /** *01:定义访问路径 *com.west.test.httpclient.PostServlet */ public class URLUtil { public static final String HttpClient_002="http://localhost:8080/httpClient_002/";//访问本地项目2路径 public static final String POST_CONTENT=HttpClient_002+"PostContent"; }
PostServlet类:
package com.west.test.httpclient; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.NameValuePair; import org.apache.commons.httpclient.methods.PostMethod; import javax.servlet.ServletException; public class PostServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("httpClient_001:doGet方式提交"); System.out.println("========PostServlet start========="); /**02:定义实例化对象*/ HttpClient httpClient = new HttpClient(); //03:Post需要创建PostMethod PostMethod method=new PostMethod(URLUtil.POST_CONTENT); //04:创建NameValuePair对象,保存需要存入到路径的值 NameValuePair name=new NameValuePair("name", "zhagnsan"); System.out.println("NameValuePair:name:"+name.toString()); NameValuePair password=new NameValuePair("password", "password:123321"); //05:利用method设置需要加入到访问路径后面的值(相当于url传值) method.setRequestBody(new NameValuePair[]{name,password}); String rt=""; try { //06:提交数据,并且获取提交结果 int status=httpClient.executeMethod(method); //07:HttpStatus.SC_OK=200,表明提交数据成功 if(status==HttpStatus.SC_OK){ //08:获取返回的字符串 rt=method.getResponseBodyAsString(); System.out.println("httpClient_001得到httpClient_002返回的字符串:rt:"+rt); } } catch (Exception e) { e.printStackTrace(); System.out.println("网络连接失败,请联系管理员!"); } //释放HttpClient资源 method.releaseConnection(); /**09:编写outMessage()方法*/ outMessage(response, rt); } /**09:编写outMessage()方法*/ public void outMessage(HttpServletResponse response, String message) { try { PrintWriter out = response.getWriter(); out.print(message); out.close(); } catch (IOException e) { e.printStackTrace(); } } }
配置web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_ID" version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name>test</display-name> <servlet> <servlet-name>PostServlet</servlet-name> <servlet-class>com.west.test.httpclient.PostServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>PostServlet</servlet-name> <url-pattern>/PostServlet</url-pattern> </servlet-mapping> <session-config> <session-timeout>600</session-timeout> </session-config> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
项目02:
PostContent类
package com.test.servlet; import java.io.IOException; import java.io.PrintWriter; import java.io.UnsupportedEncodingException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class PostContent extends HttpServlet { @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("=========PostContent start============"); System.out.println("httpClient_002:doPost方式执行"); String name=request.getParameter("name"); String password=request.getParameter("password"); String reStr="httpClient_002:name="+name+"======password="+password; System.out.println("reStr:"+reStr); outMessage(response, reStr); } public void outMessage(HttpServletResponse response, Object message) { try { PrintWriter out = response.getWriter(); out.print(message); out.close(); } catch (IOException e) { e.printStackTrace(); } } }
<?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_ID" version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name>test002</display-name> <servlet> <servlet-name>PostContent</servlet-name> <servlet-class>com.test.servlet.PostContent</servlet-class> </servlet> <servlet-mapping> <servlet-name>PostContent</servlet-name> <url-pattern>/PostContent</url-pattern> </servlet-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> </welcome-file-list> </web-app>
HttpClient_001_初步实现项目01的servlet,与项目02的servlet,之间数据访问
标签:
原文地址:http://www.cnblogs.com/zjsy/p/4425747.html