5.添加tomcat插件
/jt-manage/pom.xml
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.mavengroupId>
<artifactId>tomcat7-maven-pluginartifactId>
<version>2.2version>
<configuration>
<port>8092port>
<path>/path>
configuration>
plugin>
plugins>
build>
/jt-web/src/main/webapp/WEB-INF/web.xml
<filter>
<filter-name>encodingFilterfilter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilterfilter-class>
<init-param>
<param-name>encodingparam-name>
<param-value>UTF8param-value>
init-param>
filter>
<filter-mapping>
<filter-name>encodingFilterfilter-name>
<url-pattern>/*url-pattern>
filter-mapping>
<servlet>
<servlet-name>springmvc-webservlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
<init-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:spring/applicationContext*.xmlparam-value>
init-param>
<load-on-startup>1load-on-startup>
servlet>
<servlet-mapping>
<servlet-name>springmvc-webservlet-name>
<url-pattern>*.htmlurl-pattern>
servlet-mapping>
<servlet-mapping>
<servlet-name>springmvc-webservlet-name>
<url-pattern>/service/*url-pattern>
servlet-mapping>
<welcome-file-list>
<welcome-file>index.htmlwelcome-file>
<welcome-file>index.jspwelcome-file>
welcome-file-list>
/jt-web/src/main/resources/spring/applicationContext.xml
需求:当用户访问 www.jt.com/index.html 时,自动访问京淘首页。
1.编辑controller
/jt-web/src/main/java/com/jt/web/controller/IndexController.java
2.修改Nginx实现反向代理
修改自己计算机上的nginx.conf文件,在配置文件结尾处,再添加一个server
server {
listen 80;
server_name www.jt.com;
location / {
proxy_pass http://localhost:8092;
proxy_connect_timeout 3;
proxy_read_timeout 3;
proxy_send_timeout 3;
}
}
3.修改host文件
找到自己计算机上的host文件,修改其内容,添加相应内容。
# 京淘电商环境
127.0.0.1 image.jt.com
127.0.0.1 manage.jt.com
127.0.0.1 www.jt.com
127.0.0.1 sso.jt.com
127.0.0.1 cart.jt.com
127.0.0.1 order.jt.com
127.0.0.1 solr.jt.com
192.168.126.148 www.easymall.com
1.浏览器解析静态页面速度更快。
2.对于搜索引擎,静态页面更加友好,更容易被搜索引擎收录。
3.交互性差,扩展性弱。
1.交互性强,扩展性好。
2.浏览器加载速度慢。
3.搜索引擎不会收录动态页面数据。
伪静态是相对真实静态来讲的,通常我们为了增强搜索引擎的友好面,都将文章内容生成静态页面,但是有的朋友为了实时的显示一些信息。或者还想运用动态脚本解决一些问题。不能用静态的方式来展示网站内容。但是这就损失了对搜索引擎的友好面。怎么样在两者之间找个中间方法呢,这就产生了伪静态技术。就是展示出来的是以html一类的静态页面形式,但其实是用ASP一类的动态脚本来处理的。
总结:以.html展现动态页面的技术。
HttpClient 是Apache Jakarta Common 下的子项目,可以用来提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的版本和建议。
HTTP 协议可能是现在 Internet 上使用得最多、最重要的协议了,越来越多的 Java 应用程序需要直接通过 HTTP 协议来访问网络资源。虽然在 JDK 的 java net包中已经提供了访问 HTTP 协议的基本功能,但是对于大部分应用程序来说,JDK 库本身提供的功能还不够丰富和灵活。
HttpClient 是 Apache Jakarta Common 下的子项目,用来提供高效的、最新的、功能丰富的支持 HTTP 协议的客户端编程工具包,并且它支持 HTTP 协议最新的版本和建议。HttpClient 已经应用在很多的项目中,比如 Apache Jakarta 上很著名的另外两个开源项目 Cactus 和 HTMLUnit 都使用了 HttpClient。
现在HttpClient最新版本为 HttpClient 4.5 (GA) (2015-09-11)。
总结:在java代码中发起Http请求,之后解析返回值结果即可。
特点:Http请求协议中,参数类型:String,结果类型:html/json
/jt-parent/pom.xml
<dependency>
<groupId>org.apache.httpcomponentsgroupId>
<artifactId>httpclientartifactId>
<version>${httpclient.version}version>
dependency>
<dependency>
<groupId>org.apache.httpcomponentsgroupId>
<artifactId>httpmimeartifactId>
<version>4.3.1version>
dependency>
/jt-web/src/test/java/com/jt/test/httpclient/TestHttpClient.java
@Test
public void doGet() throws ClientProtocolException, IOException{
//1.实例化HttpClient对象
CloseableHttpClient client
= HttpClients.createDefault();
//定义url
String url = "https://item.jd.com/10528983700.html";
//定义请求类型
HttpGet get = new HttpGet(url);
String method =
get.getRequestLine().getMethod();
System.out.println("获取请求类型:"+method);
String http =
get.getRequestLine().getProtocolVersion().toString();
System.out.println("获取请求协议:"+http);
//发起http请求
CloseableHttpResponse response =
client.execute(get);
//判断状态信息是否正确 200
if(response.getStatusLine().getStatusCode() == 200){
String result =
EntityUtils.toString(response.getEntity());
System.out.println(result);
}
}
/jt-web/src/main/resources/property/httpclient.properties
#从连接池中获取到连接的最长时间
http.request.connectionRequestTimeout=500
#5000
http.request.connectTimeout=5000
#数据传输的最长时间
http.request.socketTimeout=30000
#提交请求前测试连接是否可用
http.request.staleConnectionCheckEnabled=true
#设置连接总数
http.pool.maxTotal=200
#设置每个地址的并发数
http.pool.defaultMaxPerRoute=100
2.Spring管理配置文件
/jt-web/src/main/resources/spring/applicationContext.xml
<list>
<value>classpath:/property/jdbc.propertiesvalue>
<value>classpath:/property/redis.propertiesvalue>
<value>classpath:/property/httpclient.propertiesvalue>
list>
3.编辑Spring配置文件
/jt-web/src/main/resources/spring/applicationContext-httpclient.xml
<bean id="httpClientConnectionManager" class="org.apache.http.impl.conn.PoolingHttpClientConnectionManager" destroy-method="close">
<property name="maxTotal" value="${http.pool.maxTotal}">property>
<property name="defaultMaxPerRoute" value="${http.pool.defaultMaxPerRoute}">property>
bean>
<bean id="httpClientBuilder" class="org.apache.http.impl.client.HttpClientBuilder" factory-method="create">
<property name="connectionManager" ref="httpClientConnectionManager">property>
bean>
<bean id="httpClient" factory-bean="httpClientBuilder" factory-method="build"/>
<bean class="com.jt.common.util.IdleConnectionEvictor" destroy-method="shutdown">
<constructor-arg index="0" ref="httpClientConnectionManager" />
<constructor-arg index="1" value="60000" />
bean>
<bean id="requestConfigBuilder" class="org.apache.http.client.config.RequestConfig.Builder">
<property name="connectionRequestTimeout" value="${http.request.connectionRequestTimeout}"/>
<property name="connectTimeout" value="${http.request.connectTimeout}"/>
<property name="socketTimeout" value="${http.request.socketTimeout}"/>
<property name="staleConnectionCheckEnabled" value="${http.request.staleConnectionCheckEnabled}"/>
bean>
<bean id="requestConfig" factory-bean="requestConfigBuilder" factory-method="build" />
/jt-common/src/main/java/com/jt/common/service/HttpClientService.java
@Service
public class HttpClientService {
private static final Logger LOGGER = LoggerFactory.getLogger(HttpClientService.class);
@Autowired(required=false)
private CloseableHttpClient httpClient;
@Autowired(required=false)
private RequestConfig requestConfig;
/**
* url:addUser?id=1&name=tom
* httpClient get请求方式
* 参数: 1.String url 2.Map,3.字符编码
* /* //addUser?id=1&name=tom
String paramUrl = url + "?";
for (Map.Entry entry : params.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();
paramUrl = paramUrl + key + "=" + value + "&";
}
paramUrl = paramUrl.substring(0, paramUrl.length()-1);*/
public String doGet
(String url,Map<String,String> params,String charset){
String result = null;
//1.判断字符集编码是否为null
if(StringUtils.isEmpty(charset)){
charset = "UTF-8";
}
try {
//2.判断是否有参数
if(params != null){
URIBuilder builder = new URIBuilder(url);
for (Map.Entry<String,String> entry : params.entrySet()) {
builder.addParameter(entry.getKey(), entry.getValue());
}
//利用工具类实现url拼接
url = builder.build().toString();
}
//发起http请求
HttpGet httpGet = new HttpGet(url);
httpGet.setConfig(requestConfig);
CloseableHttpResponse httpResponse =
httpClient.execute(httpGet);
if(httpResponse.getStatusLine()
.getStatusCode() == 200){
//获取返回值结果
result = EntityUtils.
toString(httpResponse.getEntity(),charset);
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
//重载方法
public String doGet(String url){
return doGet(url,null,null);
}
public String doGet(String url,Map<String,String> params){
return doGet(url, params, null);
}
}
/jt-web/src/main/java/com/jt/web/controller/ItemController.java
@Controller
@RequestMapping("/items")
public class ItemController {
@Autowired
private ItemService itemService;
@RequestMapping("/{itemId}")
public String findItemById(@PathVariable Long itemId,Model model){
Item item = itemService.findItemById(itemId);
model.addAttribute("item", item);
ItemDesc itemDesc = itemService.findItemDescById(itemId);
model.addAttribute("itemDesc", itemDesc);
//跳转到商品展现页面
return "item";
}
}
/jt-web/src/main/java/com/jt/web/service/ItemService.java
public interface ItemService {
Item findItemById(Long itemId);
ItemDesc findItemDescById(Long itemId);
}
/jt-web/src/main/java/com/jt/web/service/ItemServiceImpl.java
@Service
public class ItemServiceImpl implements ItemService {
@Autowired
private HttpClientService httpClient;
private static ObjectMapper objectMapper = new ObjectMapper();
//急需发送http请求
@Override
public Item findItemById(Long itemId) {
String url = "http://manage.jt.com/web/item/"+itemId;
//ItemJSON串
String result = httpClient.doGet(url);
Item item = null;
try {
item = objectMapper.readValue(result,Item.class);
} catch (Exception e) {
e.printStackTrace();
}
return item;
}
@Override
public ItemDesc findItemDescById(Long itemId) {
String url = "http://manage.jt.com/web/item/desc/"+itemId;
//JSON串
String result = httpClient.doGet(url);
ItemDesc itemDesc = null;
try {
itemDesc = objectMapper.readValue(result,ItemDesc.class);
} catch (Exception e) {
e.printStackTrace();
}
return itemDesc;
}
}
/jt-manage/src/main/java/com/jt/manage/controller/web/WebItemController.java
@Controller
@RequestMapping("/web")
public class WebItemController {
@Autowired
private ItemService itemService;
@RequestMapping("/item/{itemId}")
@ResponseBody
public Item findItemById(@PathVariable Long itemId){
return itemService.finditemById(itemId);
}
@RequestMapping("/item/desc/{itemId}")
@ResponseBody
public ItemDesc findItemDescById(@PathVariable Long itemId){
return itemService.findItemDescById(itemId);
}
}
/jt-manage/src/main/java/com/jt/manage/service/ItemService.java
Item finditemById(Long itemId);
ItemDesc findItemDescById(Long itemId);
/jt-manage/src/main/java/com/jt/manage/service/ItemServiceImpl.java
/**
* 1.手写sql 手动返回po.item
* 2.编辑vo.Mapper 操作item表
* 3.直接返回manage.中的pojo对象 json返回
* 4.不用common中po 在web层添加pojo对象
*/
@Override
public Item finditemById(Long itemId) {
return itemMapper.selectByPrimaryKey(itemId);
}
//之前写过
@Override
public ItemDesc findItemDescById(Long itemId) {
return itemDescMapper.selectByPrimaryKey(itemId);
}