Java Web添加HTTP服务
2015-10-06 14:06
627 查看
1.web.xml文件添加下面内容
<servlet>
<servlet-name>CommonHttpService</servlet-name>
<servlet-class>com.cName.pName.http.CommonHttpService</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CommonHttpService</servlet-name>
<url-pattern>/service/commonHttpService</url-pattern>
</servlet-mapping>
2.applicationContext.xml文件添加下面内容
<bean id="SpringContextUtil" class="com.cName.pName.common.SpringContextUtil"
scope="singleton"/>
3.添加SpringContextUtil.java
package com.cName.pName.common;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
public class SpringContextUtil implements ApplicationContextAware {
private static ApplicationContext applicationContext; // Spring应用上下文环境
/**
* 实现ApplicationContextAware接口的回调方法,设置上下文环境
*
* @param applicationContext
* @throws BeansException
*/
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
SpringContextUtil.applicationContext = applicationContext;
}
/**
* @return ApplicationContext
*/
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
/**
* 获取对象
*
* @param name
* @return Object 一个以所给名字注册的bean的实例
* @throws BeansException
*/
public static Object getBean(String name) throws BeansException {
return applicationContext.getBean(name);
}
/**
* 获取类型为requiredType的对象
* 如果bean不能被类型转换,相应的异常将会被抛出(BeanNotOfRequiredTypeException)
*
* @param name
* bean注册名
* @param requiredType
* 返回对象类型
* @return Object 返回requiredType类型对象
* @throws BeansException
*/
public static Object getBean(String name, Class requiredType)
throws BeansException {
return applicationContext.getBean(name, requiredType);
}
/**
* 如果BeanFactory包含一个与所给名称匹配的bean定义,则返回true
*
* @param name
* @return boolean
*/
public static boolean containsBean(String name) {
return applicationContext.containsBean(name);
}
/**
* 判断以给定名字注册的bean定义是一个singleton还是一个prototype。
* 如果与给定名字相应的bean定义没有被找到,将会抛出一个异常(NoSuchBeanDefinitionException)
*
* @param name
* @return boolean
* @throws NoSuchBeanDefinitionException
*/
public static boolean isSingleton(String name)
throws NoSuchBeanDefinitionException {
return applicationContext.isSingleton(name);
}
/**
* @param name
* @return Class 注册对象的类型
* @throws NoSuchBeanDefinitionException
*/
public static Class getType(String name)
throws NoSuchBeanDefinitionException {
return applicationContext.getType(name);
}
/**
* 如果给定的bean名字在bean定义中有别名,则返回这些别名
*
* @param name
* @return
* @throws NoSuchBeanDefinitionException
*/
public static String[] getAliases(String name)
throws NoSuchBeanDefinitionException {
return applicationContext.getAliases(name);
}
/**
* 取得前缀为name 且实现了clss接口的bean.如果有多个 只返回找到的第一个。 必须有标识为name的bean 否则会报错
*
* @param name
* @param clss
* @return
* @throws NoSuchBeanDefinitionException
*/
public static Object getBeanByVague(String name, Class clss)
throws NoSuchBeanDefinitionException {
String[] names = applicationContext.getBeanDefinitionNames();
Object reObject = applicationContext.getBean(name);
if (names.length > 0) {
for (int i = 0; i < names.length; i++) {
if (names[i].startsWith(name) && !names[i].equals(name)) {
Object temp = applicationContext.getBean(names[i]);
if (clss.isInstance(temp)) {
reObject = temp;
break;
}
}
}
}
return reObject;
}
public static HttpServletRequest getServletRequest() {
return ((ServletRequestAttributes) RequestContextHolder
.getRequestAttributes()).getRequest();
}
}
4.添加服务接口实现类CommonHttpService.java
package com.cName.pName.http;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.cName.pName.common.SpringContextUtil;
import com.cName.pName.service.UserService;
public class CommonHttpService extends HttpServlet {
private static final long serialVersionUID = -2147143215062730629L;
UserService userService;
public CommonHttpService() {
this.userService = (UserService) SpringContextUtil.getBeanByVague(
"userServiceImpl", UserService.class);
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
out.print("doGet方法");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String xmlmsg = request.getParameter("$xmlmsg");
System.out.println(xmlmsg);
try {
Map<String, Object> Response = new HashMap<String, Object>();
Response.put("xml", "test");
response.setContentType("text/xml;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
response.setHeader("Cache-Control", "no-cache");
PrintWriter out = response.getWriter();
System.out.println("commonhttp success");
out.print(Response);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
到此HTTP服务添加OK了,启动Web服务
请求http://localhost:8080/pName/service/commonHttpService?$xmlmsg=test汉字进行测试。
附HTTP请求测试工具http://download.csdn.net/detail/jclpc/9158625
<servlet>
<servlet-name>CommonHttpService</servlet-name>
<servlet-class>com.cName.pName.http.CommonHttpService</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>CommonHttpService</servlet-name>
<url-pattern>/service/commonHttpService</url-pattern>
</servlet-mapping>
2.applicationContext.xml文件添加下面内容
<bean id="SpringContextUtil" class="com.cName.pName.common.SpringContextUtil"
scope="singleton"/>
3.添加SpringContextUtil.java
package com.cName.pName.common;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
public class SpringContextUtil implements ApplicationContextAware {
private static ApplicationContext applicationContext; // Spring应用上下文环境
/**
* 实现ApplicationContextAware接口的回调方法,设置上下文环境
*
* @param applicationContext
* @throws BeansException
*/
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
SpringContextUtil.applicationContext = applicationContext;
}
/**
* @return ApplicationContext
*/
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
/**
* 获取对象
*
* @param name
* @return Object 一个以所给名字注册的bean的实例
* @throws BeansException
*/
public static Object getBean(String name) throws BeansException {
return applicationContext.getBean(name);
}
/**
* 获取类型为requiredType的对象
* 如果bean不能被类型转换,相应的异常将会被抛出(BeanNotOfRequiredTypeException)
*
* @param name
* bean注册名
* @param requiredType
* 返回对象类型
* @return Object 返回requiredType类型对象
* @throws BeansException
*/
public static Object getBean(String name, Class requiredType)
throws BeansException {
return applicationContext.getBean(name, requiredType);
}
/**
* 如果BeanFactory包含一个与所给名称匹配的bean定义,则返回true
*
* @param name
* @return boolean
*/
public static boolean containsBean(String name) {
return applicationContext.containsBean(name);
}
/**
* 判断以给定名字注册的bean定义是一个singleton还是一个prototype。
* 如果与给定名字相应的bean定义没有被找到,将会抛出一个异常(NoSuchBeanDefinitionException)
*
* @param name
* @return boolean
* @throws NoSuchBeanDefinitionException
*/
public static boolean isSingleton(String name)
throws NoSuchBeanDefinitionException {
return applicationContext.isSingleton(name);
}
/**
* @param name
* @return Class 注册对象的类型
* @throws NoSuchBeanDefinitionException
*/
public static Class getType(String name)
throws NoSuchBeanDefinitionException {
return applicationContext.getType(name);
}
/**
* 如果给定的bean名字在bean定义中有别名,则返回这些别名
*
* @param name
* @return
* @throws NoSuchBeanDefinitionException
*/
public static String[] getAliases(String name)
throws NoSuchBeanDefinitionException {
return applicationContext.getAliases(name);
}
/**
* 取得前缀为name 且实现了clss接口的bean.如果有多个 只返回找到的第一个。 必须有标识为name的bean 否则会报错
*
* @param name
* @param clss
* @return
* @throws NoSuchBeanDefinitionException
*/
public static Object getBeanByVague(String name, Class clss)
throws NoSuchBeanDefinitionException {
String[] names = applicationContext.getBeanDefinitionNames();
Object reObject = applicationContext.getBean(name);
if (names.length > 0) {
for (int i = 0; i < names.length; i++) {
if (names[i].startsWith(name) && !names[i].equals(name)) {
Object temp = applicationContext.getBean(names[i]);
if (clss.isInstance(temp)) {
reObject = temp;
break;
}
}
}
}
return reObject;
}
public static HttpServletRequest getServletRequest() {
return ((ServletRequestAttributes) RequestContextHolder
.getRequestAttributes()).getRequest();
}
}
4.添加服务接口实现类CommonHttpService.java
package com.cName.pName.http;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.cName.pName.common.SpringContextUtil;
import com.cName.pName.service.UserService;
public class CommonHttpService extends HttpServlet {
private static final long serialVersionUID = -2147143215062730629L;
UserService userService;
public CommonHttpService() {
this.userService = (UserService) SpringContextUtil.getBeanByVague(
"userServiceImpl", UserService.class);
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
response.setCharacterEncoding("UTF-8");
PrintWriter out = response.getWriter();
out.print("doGet方法");
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String xmlmsg = request.getParameter("$xmlmsg");
System.out.println(xmlmsg);
try {
Map<String, Object> Response = new HashMap<String, Object>();
Response.put("xml", "test");
response.setContentType("text/xml;charset=UTF-8");
response.setCharacterEncoding("UTF-8");
response.setHeader("Cache-Control", "no-cache");
PrintWriter out = response.getWriter();
System.out.println("commonhttp success");
out.print(Response);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
到此HTTP服务添加OK了,启动Web服务
请求http://localhost:8080/pName/service/commonHttpService?$xmlmsg=test汉字进行测试。
附HTTP请求测试工具http://download.csdn.net/detail/jclpc/9158625
相关文章推荐
- TCP与UDP的区别
- TCP的三次握手和四次挥手
- netperf 而网络性能测量
- TCP的三次握手和四次挥手
- ZOJ 2532 Internship 网络流求关键边
- VMware网络连接方式(Host-only、NAT、Bridged)介绍及NAT环境下静态IP配置
- 手动编译httpd
- Linux网络编程入门 (转载)
- Xcode7中HTTP不能正常请求的解决办法
- http://jinnianshilongnian.iteye.com/blog/2019547
- .NET/MVC-发布到IIS6.1提示未能加载程序集System.Web.Http.WebHost
- linux常用网络命令
- 深度学习(DL)与卷积神经网络(CNN)学习笔记随笔-04-基于Python的LeNet之MLP
- http编程系列2:http协议GET方式获取网络图片
- 自己对网络请求进行封装,block作参数
- TCP定时器
- 神经网络(Neural Network)概述
- Linux网络配置和网络诊断命令介绍
- 和大家分享几本seo和营销的书籍吧!
- ioctl函数详细说明(网络)