*. 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]

좋은 웹페이지 즐겨찾기