*. do 요청 처리
8383 단어 JAVA
public class ServiceServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void service(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html; charset=GB18030");
request.setCharacterEncoding("GB18030");
response.setCharacterEncoding("GB18030");
PrintWriter out = response.getWriter();
DofContext.setRequest(request);
DofContext.setResponse(response);
// url actionName
String actionName = getActionName(request);
String methodname = request.getParameter("action");
// action
DofInvocation invocation = new DofInvocation(actionName);
String resultString ="";
try {
resultString = invocation.execute(methodname);
} catch (Exception e) {
e.printStackTrace();
}
//
out.print(resultString);
}
// uri action name
private String getActionName(HttpServletRequest request){
String uri=request.getRequestURI();
String actionName=uri.substring(uri.lastIndexOf("/")+1 , uri.indexOf(".do"));
return actionName;
}
// map Context
public void init(ServletConfig config) throws ServletException {
ServletContext context=config.getServletContext();
Map configMap=new HashMap();
try {
configMap=ParseDofConfFile.convertPropertyFileToMap();
} catch (IOException e) {
e.printStackTrace();
}
DofContext.setServletContext(context);
DofContext.setConfigurationMap(configMap);
System.out.println("===DOF ====");
}
}
프로필 분석
public class ParseDofConfFile {
public static String ACTION_CONF_FILE = "DofConf.xml";
private static final String S_Action = "action";
private static final String S_Mapping = "class-mapping";
private static final String S_ClassName = "class-name";
/**
* DofConf.xml , configuration , map
*/
@SuppressWarnings("unchecked")
public static Map convertPropertyFileToMap()
throws IOException {
Map actionResultMap = new HashMap();
InputStream fs = null;
try {
fs = ParseDofConfFile.class.getClassLoader().getResourceAsStream(
ACTION_CONF_FILE);
if (fs != null) {
SAXReader saxReader = new SAXReader();
Document doc = saxReader.read(fs);
if (doc != null) {
Element root = doc.getRootElement();
if (root != null) {
// action ,
List actionlist = root.elements(S_Action);
if (actionlist != null)
{
Iterator it = actionlist.iterator();
while (it.hasNext())
{
Element actionele = (Element) it.next();
if (actionele != null)
{
// class-mapping class-name
Element mapele = actionele.element(S_Mapping);
Element nameele = actionele.element(S_ClassName);
// , Configuration map
if (mapele != null && nameele != null)
{
String actionName = mapele.getText();
String classname = nameele.getText();
Configuration conf = new Configuration();
conf.setActionName(actionName);
conf.setClassName(classname);
actionResultMap.put(actionName, conf);
}
}
}
}
}
}
}
}
catch (Exception e) {
throw new IOException(" DofConfig.xml !");
} finally {
if (fs != null) {
fs.close();
fs = null;
}
}
return actionResultMap;
}
}
구성 대상
public class Configuration {
// action
private String actionName;
//action
private String className;
public String getActionName() {
return actionName;
}
public void setActionName(String actionName) {
this.actionName = actionName;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
public String toString(){
return "actionName="+this.actionName+";classname="+this.className;
}
}
dofContext
public class DofContext {
//save actioncontext to a map
private static Map context=new HashMap();
private DofContext(){
}
public static Map getContext() {
return context;
}
public static HttpServletRequest getRequest(){
return (HttpServletRequest)getContext().get(ContextConstant.HTTP_REQUEST);
}
public static void setRequest(HttpServletRequest request){
getContext().put(ContextConstant.HTTP_REQUEST, request);
}
public static HttpServletResponse getResponse(){
return (HttpServletResponse)getContext().get(ContextConstant.HTTP_RESPONSE);
}
public static void setResponse(HttpServletResponse response){
getContext().put(ContextConstant.HTTP_RESPONSE, response);
}
public static void setServletContext(ServletContext servletContext){
getContext().put(ContextConstant.SERVLET_CONTEXT, servletContext);
}
public static ServletContext getServletContext(){
return (ServletContext)getContext().get(ContextConstant.SERVLET_CONTEXT);
}
public static void setConfigurationMap(Map configMap){
getContext().put(ContextConstant.ACTION_CONFIG_MAP, configMap);
}
@SuppressWarnings("unchecked")
public static Map getConfigurationMap(){
return (Map)getContext().get(ContextConstant.ACTION_CONFIG_MAP);
}
}
[code="java"]
/**
* action
* @author dengm
*
*/
public class DofInvocation {
private String actionName;
public DofInvocation(String actionName) {
this.actionName = actionName;
}
// action
@SuppressWarnings("unchecked")
public String execute(String methodName) throws Exception {
Configuration config = getActionConfiguration();
if (null != config) {
String className = config.getClassName();
if (null == methodName || "".equals(methodName)) {
methodName = "execute";
}
try {
Class actionClass = Class.forName(className);
Object action = actionClass.newInstance();
Method method = action.getClass().getMethod(methodName, null);
String result = (String) method.invoke(action, null);
return result;
} catch (ClassNotFoundException e) {
throw new Exception(" :" + className);
} catch (InstantiationException e) {
} catch (NoSuchMethodException e) {
throw new Exception(" :" + className + " >>>"
+ methodName);
} catch (Exception e) {
throw new Exception(e.getMessage());
}
}
return ContextConstant.ACTION_ERROR;
}
/*
* action configuration
*/
@SuppressWarnings("unchecked")
public Configuration getActionConfiguration() {
Configuration config = new Configuration();
Map configMap = (Map) DofContext
.getConfigurationMap();
if (null != configMap) {
config = configMap.get(actionName);
}
return config;
}
}
[/code]
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
JAVA 객체 작성 및 제거 방법정적 공장 방법 정적 공장 방법의 장점 를 반환할 수 있습니다. 정적 공장 방법의 단점 류 공유되거나 보호된 구조기를 포함하지 않으면 이불류화할 수 없음 여러 개의 구조기 파라미터를 만났을 때 구축기를 고려해야 한다...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.