Tomcat 网站配置和HTTP四种方法测试
2015-03-19 23:55
591 查看
一、tomcat的基本结构
1. conf/server.xml结构
<Server> <Connector /> <Service> <Engine> <Realm></Realm> <Host></Host> </Engine> </Service> </Server>
<connector>用来定义开放的端口
<host> 用来定义网站
2. web.xml结构
<web-app> <display-name></display-name> <description></description> <servlet></servlet> <servlet></servlet> </web-app>
网站的基本结构:
WEB-INF/classes----------存储servlet编译代码
WEB-INF/lib-----------------存储三方的库
WEB-INF/web.xml----------网站配置
根目录下---------------------- 存储jsp,html。
二、测试网站配置
阶段一:server.xml 配置
方法一:通过域名来测试在<Engine>标签下添加主机
<Host name="域名" appBase="目录" unpackWARs="true" autoDeploy="true">
方法二:通过端口来测试
在Server下添加端口:
<Connector port="9000" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />
redirectPort表示如果是SSL链接,重定向到8443端口(SSL端口)
方法三:通过目录来测试
1.在webapps下建立目录 test
2.在Server.xml中的<Host>标签下添加
<Context path="/test" docBase="test" reloadable="true" />
appBase是绝对路径、也可以是相对路径(相对于Tomcat的安装目录)
阶段二:web.xml 配置
<?xml version="1.0" encoding="ISO-8859-1"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1" metadata-complete="true"> <display-name>Welcome to Tomcat</display-name> <description> Welcome to Tomcat </description> <servlet> <servlet-name>default</servlet-name> <servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class> <init-param> <param-name>debug</param-name> <param-value>0</param-value> </init-param> <init-param> <param-name>readonly</param-name> <param-value>false</param-value> </init-param> <init-param> <param-name>listings</param-name> <param-value>false</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> </web-app>
<init-param>
<param-name>readonly</param-name>
<param-value>false</param-value>
</init-param>
表示开放网站的PUT和DELETE方法。
三、测试程序配置
import java.io.File; import java.io.IOException; import java.io.InputStream; import java.util.Timer; import java.util.TimerTask; import javax.swing.text.html.parser.Entity; import org.apache.http.Header; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpDelete; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpHead; import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPut; import org.apache.http.entity.FileEntity; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.HttpClients; //CloseableHttpClient,CloseableHttpResponse /* * HttpGet--->HttpRequestBase---->AbstractExecutionAwareRequest(类) * ----->HttpUriRequest--->HttpRequest(接口) * */ //下载速率的计算方法:一秒后数据量-一秒前数据量 public class MainApp { //设立线程的共享的数据 private static long counter=0; private static void displayThread() { //显示进程 Timer t=new Timer(); TimerTask task=new TimerTask() { private long temp=0; private long speed=0; public void run() { speed=counter-temp; temp=counter; System.out.print("spped:"+speed/1000+"KB/s"); System.out.println("\t Download Bytes:"+counter); } }; t.schedule(task, 2000,1000);//时间为毫秒,1秒=1000毫秒 } private static void testGetMethod(String url) throws ClientProtocolException, IOException { HttpClient httpclient = HttpClients.createDefault(); HttpGet request=new HttpGet(url); request.addHeader("Accept", "*/*"); //显示请求头 System.out.println(request.getRequestLine().toString()); for(Header h:request.getAllHeaders()) System.out.println(h.getName()+":"+h.getValue()); HttpResponse response=httpclient.execute(request); //显示响应头 System.out.println(); System.out.println(response.getStatusLine().toString()); for(Header h:response.getAllHeaders()) System.out.println(h.getName()+":"+h.getValue()); HttpEntity he=response.getEntity(); //接收字节统计 InputStream is=he.getContent(); while (is.read() != -1) { counter++; } } private static void testHeadMethod(String url) throws ClientProtocolException, IOException { HttpClient httpclient = HttpClients.createDefault(); HttpHead request=new HttpHead(url); //显示请求头 System.out.println(request.getRequestLine().toString()); for(Header h:request.getAllHeaders()) System.out.println(h.getName()+":"+h.getValue()); System.out.println(); HttpResponse response=httpclient.execute(request); //显示响应头 System.out.println(response.getStatusLine().toString()); for(Header h:response.getAllHeaders()) System.out.println(h.getName()+":"+h.getValue()); } private static void testPostMethod(String url) throws ClientProtocolException, IOException { HttpClient httpclient = HttpClients.createDefault(); HttpPost request=new HttpPost(url); //显示请求头 //request.setHeader("Content-Type", "application/x-www-form-urlencoded;charset=gb2312"); StringEntity entity=new StringEntity("bbbbbbbbb"); entity.setChunked(true); request.setEntity(entity); System.out.println(request.getRequestLine().toString()); for(Header h:request.getAllHeaders()) System.out.println(h.getName()+":"+h.getValue()); System.out.println(); HttpResponse response=httpclient.execute(request); //显示响应头 System.out.println(response.getStatusLine().toString()); for(Header h:response.getAllHeaders()) System.out.println(h.getName()+":"+h.getValue()); } private static void testPutMethod(String url) throws ClientProtocolException, IOException { HttpClient httpclient = HttpClients.createDefault(); HttpPut request=new HttpPut(url); //entity.setChunked(true); //request.setEntity(entity); System.out.println(request.getRequestLine().toString()); for(Header h:request.getAllHeaders()) System.out.println(h.getName()+":"+h.getValue()); System.out.println(); HttpResponse response=httpclient.execute(request); //显示响应头 System.out.println(response.getStatusLine().toString()); for(Header h:response.getAllHeaders()) System.out.println(h.getName()+":"+h.getValue()); } private static void testDeleteMethod(String url) throws ClientProtocolException, IOException { HttpClient httpclient = HttpClients.createDefault(); HttpDelete request=new HttpDelete(url); //entity.setChunked(true); //request.setEntity(entity); System.out.println(request.getRequestLine().toString()); for(Header h:request.getAllHeaders()) System.out.println(h.getName()+":"+h.getValue()); System.out.println(); HttpResponse response=httpclient.execute(request); //显示响应头 System.out.println(response.getStatusLine().toString()); for(Header h:response.getAllHeaders()) System.out.println(h.getName()+":"+h.getValue()); } public static void main(String[] args) throws InterruptedException, ClientProtocolException, IOException { String url_ext="http://releases.ubuntu.com/14.04.2/ubuntu-14.04.2-desktop-amd64.iso"; //创建abc文件 testPutMethod("http://127.0.0.1:8080/test/abc"); testGetMethod("http://127.0.0.1:8080/test/abc"); testPostMethod("http://127.0.0.1:8080/test/abc"); testDeleteMethod("http://127.0.0.1:8080/test/abc"); } }
相关文章推荐
- Tomcat7和mysql连接池的配置方法和测试(dbcp方式)
- 配置Tomcat,Apache的gzip压缩(HTTP压缩)来提高浏览网站速度
- Tomcat中更改网站根目录和默认页的配置方法
- Tomcat中更改网站根目录和默认页的配置方法
- 配置Tomcat,Apache的gzip压缩(HTTP压缩)来提高浏览网站速度
- Tomcat中更改网站根目录和默认页的配置方法
- vs2015启动网站调试提示 HTTP 错误 403.14 - Forbidden Web 服务器被配置为不列出此目录的内容。 解决方法
- Tomcat中更改网站根目录和默认页的配置方法
- 网站开发进阶(一)Tomcat域名或IP地址访问方式配置方法
- 配置tomcat支持http delete和put的方法
- Tomcat中更改网站根目录和默认页的配置方法
- Tomcat中更改网站根目录和默认页的配置方法
- 网站开发进阶(一)Tomcat域名或IP地址访问方式配置方法
- 用Jporbe测试tomcat的配置方法
- Tomcat7和mysql连接池的配置方法和测试
- 在Eclipse Galileo中配置Tomcat及代码显示HttpServlet相关错误信息处理方法
- 配置tomcat支持http delete和put的方法
- Tomcat中更改网站根目录和默认页的配置方法
- Tomcat测试“http://localhost:8080”失败解决方法
- Tomcat反向代理+负载均衡的四种方法配置