您的位置:首页 > 理论基础 > 计算机网络

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: