HttpClient_001_初步实现项目01的servlet,与项目02的servlet,之间数据访问
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></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,之间数据访问的更多相关文章
- Servlet MVC 项目实战实例
MVC的架构模式,一直是JavaEE开发中所遵循的标准,如今很多框架都已经很好的实现了MVC,像大家所熟知的Struts,SpringMVC,JSF等,但是如果没有任何框架的支持,仅仅通过JavaWe ...
- 201871030132-熊文婷 实验二 个人项目―《D{0-1}KP问题》项目报告
项目 内容 课程班级博客链接 https://edu.cnblogs.com/campus/xbsf/2018CST 这个作业要求链接 https://www.cnblogs.com/nwnu-dai ...
- 在Myeclipse buildpath 加server lib (server runtime)/项目导入时报错:The import javax.servlet.http.HttpServletRequest cannot be resolved
来源于:http://blog.csdn.net/dingqinghu/article/details/8805922 http://yl-fighting.iteye.com/blog/140946 ...
- 天坑 之 java web servlet+jsp项目 配置后 404 (MyEclipse转eclipse)
最近搞一个自己的博客系统玩,用了servlet+jsp,结果发现了两个大问题: 1.无法 Export 出 WAR文件: 2.生成WAR,放置到TOMCAT的 webapps目录后,http://lo ...
- JAVAEE——BOS物流项目01:学习计划、搭建环境、主页设计(jQuery EasyUI)
1 学习计划 1.项目概述 项目背景介绍 2.搭建项目开发环境 数据库环境 maven项目搭建 svn环境搭建 3.主页设计(jQuery EasyUI) layout页面布局 accordion折叠 ...
- 项目导入时报错:The import javax.servlet.http.HttpServletRequest cannot be resolved 解决方法
Error: The import javax.servlet cannot be resolved The import javax.servlet.http.HttpServletRequest ...
- servlet创建项目过程中,servlet内容重写的两种搭建,tomcat的配置,class的存放位置,web.xml的搭建等注意事项与易错点
运行一个servlet项目:需要做这些前提工作: 1.配置tomcat,在server选项卡的设置也就基本的设置,HTTP port与JMX port等端口号:基本都是默认的.这里需要注意的是,有的教 ...
- 用SpringSecurity从零搭建pc项目-01
注:之前写过一些列的SpringSecurity的文章,重新写一遍是为了把某些不必要的步骤省去,留下精简的,因为工作中有一些不需要. 在java的权限框架里,shiro和SpringSecurity是 ...
- SpringMVC-Spring-Hibernate项目搭建之一-- 搭建maven 项目 & servlet的demo
一. 搭建maven项目 1. 新建maven项目,选择maven Project --> Next 2. 勾选 Create a simple project --> Next 3. ...
随机推荐
- Solr5.0配置中文分词包
Solr中默认的中文分词是用Lucene的一元分词包. 现在说明在Solr5.0中配置Lucene的SmartCN中文分词包. 1,进入Solr的安装目录,我这里是:/root/nutch/solr- ...
- android-发送短信
- Linux_MySql安装
1.卸载原始mysql-lib sudo rpm -e --nodeps mysql-libs-xx 2.yum安装mysql-server sudo yum -y install mysql -se ...
- html5 语义
页面示意图
- HTML静态网页 Window.document对象
一.找到元素: docunment.getElementById("id"):根据id找,最多找一个: var a =docunment.getElementById(&qu ...
- Hack技术
Hack技术 1.IE条件注释法,微软官方推荐的hack方式. 只在IE下生效 <!--[if IE]> <link rel="stylesheet" href= ...
- DG配置实验
1.配置主库环境 2.为备库创建备份文件 3.启动主备库验证配置 4.DG测试 5.DG主备库切换 6.DG主备库第二次切换
- CSS,bootstrap表格控制当td内容过长时用省略号表示,以及在不使用bootstrap时过长也用省略号表示
首先需要在table中设置table-layout:fixed; <table style="table-layout:fixed"></table> 然后 ...
- sql替换指定字段指定字符串
REPLACE ('字段','string','newstring') UPDATE dw_site SET username =REPLACE (username,'凯鋆','韩优')
- html5 调用摄像头
---移动设备--- <input type="file" capture="camera" accept="image/*" id= ...