处理*.do的请求


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<String ,Configuration> configMap=new HashMap<String, Configuration>();
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<String, Configuration> convertPropertyFileToMap()
throws IOException {
Map<String, Configuration> actionResultMap = new HashMap<String, Configuration>();
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;
}

}


configuration对象

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<String,Object> context=new HashMap<String, Object>();

private DofContext(){
}

public static Map<String, Object> 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<String,Configuration> configMap){
getContext().put(ContextConstant.ACTION_CONFIG_MAP, configMap);
}

@SuppressWarnings("unchecked")
public static Map<String,Configuration> getConfigurationMap(){
return (Map<String,Configuration>)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<String, Configuration> configMap = (Map<String, Configuration>) DofContext
.getConfigurationMap();
if (null != configMap) {
config = configMap.get(actionName);
}
return config;
}

}


[/code]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
VBA是一种用于自动化操作Microsoft Office软件的编程语言,可以通过编写VBA代码实现各种任务。在使用VBA爬虫下载*.xls文件时,可以按照以下步骤进行: 1. 打开Excel软件,按下Alt+F11键,打开VBA编辑器。 2. 在VBA编辑器中,插入一个新的模块,可以通过点击"插入"菜单,然后选择"模块"。 3. 在新创建的模块中,编写VBA代码实现爬虫功能。以下是一个简单示例: ``` Sub DownloadXlsFile() Dim url As String Dim savePath As String ' 设置要下载的*.xls文件的URL地址 url = "http://example.com/example.xls" ' 设置保存文件的路径和名称 savePath = "C:\example.xls" ' 创建一个新的InternetExplorer对象 Dim ie As Object Set ie = CreateObject("InternetExplorer.Application") ' 使IE窗口对用户可见 ie.Visible = True ' 打开指定的URL ie.Navigate url ' 等待IE窗口加载页面完成 Do While ie.Busy Or ie.readyState <> 4 DoEvents Loop ' 保存页面为指定路径的文件 ie.Document.SaveAs savePath ' 关闭IE窗口 ie.Quit ' 释放IE对象的内存 Set ie = Nothing MsgBox "下载完成!" End Sub ``` 4. 保存并关闭VBA编辑器。 5. 返回Excel界面,按下Alt+F8键,打开宏对话框。 6. 在宏对话框中,选中刚才创建的宏"DownloadXlsFile",然后点击"运行"按钮执行宏。 7. VBA代码将自动打开一个IE窗口,加载指定的URL,然后将页面保存为指定路径的*.xls文件。 请注意,这只是一个简单的示例,具体的代码和操作会根据不同的需求和网站而有所不同。在实际应用中,还需要考虑网络请求处理、页面元素的定位等问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值