会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 133501个问题
JAVA 全系列/第二阶段:JAVA 基础深化和提高/容器(旧) 34247楼
Python 全系列/第十六阶段:Python 爬虫开发/scrapy 框架高级 34248楼

照着代码打的,不知道哪错了

package cn.lsq.server;

import java.util.List;
import java.util.Map;

import cn.lsq.servlet.Servlet;

public class WebApp {
	private static ServletContext contxt;
	static {
		contxt = new ServletContext();
		//分别获取对应关系的Map集合
	    Map<String, String> servlet = contxt.getServlet();
	    Map<String, String> mapping = contxt.getMapping();
	    //创建解析XML文件对象
	    WebDom4j web = new WebDom4j();
	    web.parse(web.getDocument());//解析XML
	    //获取解析XML之后的List集合
	    List<Entity> entityList = web.getEntityList();
	    List<Mapping> mappingList = web.getMappingList();
	    //将List集合的数据存储到Map集合
	    for(Entity entity : entityList) {
	    	servlet.put(entity.getName(), entity.getClazz());
	    }
	    for(Mapping map:mappingList) {
	    	List<String> urlPattern=map.getUrlPattern();
	    	for(String s:urlPattern) {
	    		mapping.put(s,map.getName());
	    	}
	    }
	}
	public static Servlet getServlet(String url) {
		if ((url == null) || (url.trim().equals(""))) {
		      return null;
		}
	      try {
	    	  String servletName = contxt.getMapping().get(url);
			  String servletClass = contxt.getServlet().get(servletName);
			  Class<?> clazz=Class.forName(servletClass);
			  Servlet servlet = (Servlet)clazz.newInstance();
			  return servlet;
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InstantiationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	      return null;
	}
	    
	public static void main(String[] args) {
		System.out.println(getServlet("/log"));
	}
}

image.png

JAVA 全系列/第二阶段:JAVA 基础深化和提高/手写服务器项目(旧) 34250楼
Python 全系列/第二阶段:Python 深入与提高/异常机制 34251楼
Python 全系列/第二阶段:Python 深入与提高/异常机制 34252楼
JAVA 全系列/(旧的隐藏)第七阶段:JAVA 高级技术/MyCat 34253楼

第二十一行代码为什么会报异常?

我没按照视频那样写  直接输出ois.readObject();

package com.bjsxt.Object2;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

public class TestClasses {
	public static void main(String[] args) {
		//write();
		read();
		
	}
	public static void read() {
		ObjectInputStream ois = null;
		try {
			ois = new ObjectInputStream(new FileInputStream("E:\\object3.txt"));
			System.out.println(ois.readObject());
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			if(ois!=null) {
				try {
					ois.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		
	}
	public static void write() {
		//创建班级对象
		ArrayList<Student> al = new ArrayList<Student>();
		al.add(new Student("marry", 29, "888888"));
		al.add(new Student("lili", 23, "123456"));
		al.add(new Student("jack", 20, "654321"));
		
		Classes c1  =new Classes("jn101", al);
		
		//创建对象输出流 
		ObjectOutputStream oos = null;
		try {
			oos = new ObjectOutputStream(new FileOutputStream("E:\\object3.txt"));
			oos.writeObject(c1);
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			//关闭流
			if(oos!=null) {
				try {
					oos.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		
	
	}

}

image.png

JAVA 全系列/第二阶段:JAVA 基础深化和提高/IO 流技术(旧) 34254楼
Python 全系列/第一阶段:Python入门/控制语句 34256楼
JAVA 全系列/(旧的隐藏)第七阶段:JAVA 高级技术/Maven 34257楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/异常机制 34259楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637