会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132359个问题

老师,我的代码按照视频中的敲的,但是出了问题,反复检查了几遍无结果还请老师帮忙看一下!

book.xsd中的代码:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" >
	<xs:element name="books">
		<xs:complexType>   <!-- 复杂元素 -->
			<xs:sequence>
				<xs:element name="book" maxOccurs="unbounded"><!-- 第一个元素以及可以出现多个所以为unbounded -->
					<xs:complexType>
						<xs:sequence><!-- 子标签出现是有一定顺序的 -->
							<xs:element name="name" type="xs:string"></xs:element>
							<xs:element name="author" type="xs:string" ></xs:element>
							<xs:element name="price" type="xs:double"></xs:element>
						</xs:sequence>
						<xs:attribute name="id" type="xs:positiveInteger" use="required"></xs:attribute>
					</xs:complexType>
				</xs:element>
			</xs:sequence>
		</xs:complexType>
	</xs:element>
</xs:schema>

book.xml中的代码:

<?xml version="1.0" encoding="UTF-8"?>
<books xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:noNamespaceSchemaLocation="{book.xsd}">
	<book id="1001">
		<name>java开发实战</name>
		<author>江小欧</author>
		<price>99.9</price>
		
	</book>
	<book id="1002">
		<name>mysql从删库到跑路</name>
		<author>江小白</author>
		<price>89.7</price>
	</book>
</books>

Test类:

package cn.sxt.schema;

import java.io.File;
import java.io.IOException;

import javax.xml.bind.JAXBException;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;

import org.xml.sax.SAXException;

public class Test92 {
	public static void main(String[] args) throws SAXException {
		//1.创建SchemaFactory工厂
		SchemaFactory sch = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
		//2.建立验证文件对象
		File schemaFile = new File("book.xsd");
		//3.利用SchemaFactory工厂对象,接收验证的文件对象,生成Schema对象
		Schema schema = sch.newSchema(schemaFile);
		//4.生产对此schema的验证器
		Validator validator = schema.newValidator();
		//5.要验证的数据(准备数据源)
		Source source = new StreamSource();
		//6.开始验证
		try {
			validator.validate(source);
			System.out.println("成功");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("失败");
		}
	}
}


运行错误图:

blob.png



JAVA 全系列/第二阶段:JAVA 基础深化和提高/XML 技术(旧) 3753楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/网络编程(旧) 3754楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/容器(旧) 3755楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/容器(旧) 3756楼

老师,我的程序照着老师的敲的,但是我的程序有些问题,就是服务器端启动了以后,“收到了”这个数据没有发送过去,下面是我的程序的代码和运行结果


服务器端程序:

package cn.sxt.server85;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
/**
 * 服务器端的应用程序
 * @author Mr_xi
 *
 */
public class Test {
	public static void main(String[] args) throws IOException {
		System.out.println("--------------服务器端已启动---------------------");
		//1.创建ServerSocket对象
		ServerSocket server = new ServerSocket(9999);
		//2.监听是否有客户端来请求连接
		Socket client = server.accept();
		//3.获取输入流
		InputStream is = client.getInputStream();
		System.out.println((char)is.read());
		//4.获取输出流
		OutputStream os = client.getOutputStream();
		os.write("收到了!".getBytes());
		//5.关闭流,关闭Socket
		if(os!=null) {
			os.close();
		}
		
		if(is!=null) {
			is.close();
		}
		
		if(client!=null) {
			client.close();
		}
		
	}
}


客户端程序:

package cn.sxt.client85;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

/**
 * 客户端程序
 * @author Mr_xi
 *
 */
public class Test {
	public static void main(String[] args) throws IOException {
		System.out.println("----------------客户端启动了------------------");
		//1.创建Socket对象
		Socket client = new Socket("127.0.0.1",9999);
		//2.获取输出流
		OutputStream os = client.getOutputStream();
		os.write('a');
		//3.获取输入流
		InputStream is = client.getInputStream();
		byte[] buf = new byte[1024];//中转站
		int len=0;//读到的字节个数
		while((len=is.read())!=-1) {
			System.out.println(new String(buf,0,len));
		}
		//4.关闭流
		if(os!=null){
			os.close();
		}
		if(is!=null) {
			is.close();
		}
		if(client!=null) {
			client.close();
		}
		
	}
}


运行的结果是:

blob.pngblob.png


blob.png










JAVA 全系列/第二阶段:JAVA 基础深化和提高/网络编程(旧) 3758楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/常用类 3759楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/网络编程(旧) 3760楼
JAVA 全系列/第二阶段:JAVA 基础深化和提高/IO 流技术(旧) 3761楼

老师,您好,我在这里遇到了问题

问题描述:在进行Reseponse封装后,服务器能启动起来,但在html文档里面输入内容后登录服务器空指针异常。

相关代码

Response

package cn.bjsxt.server;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;

import com.bjsxt.util.IOCloseUtil;

public class Response {/*响应*/
	private StringBuilder headInfo;//响应头
	private StringBuilder content;//响应内容
	private int length;//响应内容的长度
	//流
	private BufferedWriter bw;
	//两个常量,换行和空格
	private static final String CRLF="\r\n";//换行
	private static final String BLANK=" ";//空格
	//构造方法
	public Response(){
		headInfo = new StringBuilder();
		content = new StringBuilder();
	}
	//带参构造方法
	public Response(OutputStream os){
		this();//调用本类的无参构造方法
		try {
			bw=new BufferedWriter(new OutputStreamWriter(os,"utf-8"));//初始化输出流
		} catch (UnsupportedEncodingException e) {
			headInfo=null;
		}
	}
	//构造正文部分
	public Response print(String info){
		content.append(info);
		try {
			length+=info.getBytes("uft-8").length;
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return this;
	}
	public Response println(String info){
		content.append(info).append(CRLF);
		try {
			length+=(info+CRLF).getBytes("utf-8").length;
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	return this;
	}
	//构造响应头
	private void createHeadInfo(int code){
		headInfo.append("HTTP/1.1").append(BLANK).append(code).append(BLANK);
		switch(code){
		case 200:
			headInfo.append("OK");
			break;
		case 500:
			headInfo.append("SERVER ERROR");
			break;
		default:
			headInfo.append("NOT FOUND");
			break;
		
		}
		headInfo.append(CRLF);
		headInfo.append("Content-Type:text/html;charset=utf-8").append(CRLF);
		headInfo.append("Content-Length:"+length).append(CRLF);
		headInfo.append(CRLF);
		
	}
	/**
	 * 推送到客户级的浏览器
	 * */
	public void pushToClient(int code){
		if(headInfo==null){
			code=500;
		}
		try{
			//调用笨了中的构造响应头
			this.createHeadInfo(code);
			bw.write(headInfo.toString());
			bw.write(content.toString());
			bw.flush();
			this.close();
		}catch(IOException e){
			e.printStackTrace();
		}
		
	}
	public void close(){
		IOCloseUtil.closeAll(bw);
	}

}

Server

package cn.bjsxt.server;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;

import com.bjsxt.servlet.Servlet;
import com.bjsxt.util.IOCloseUtil;

public class Server {//服务器,启动和停止服务
	private ServerSocket server;
	public static void main(String[] args){
		Server server = new Server();//创建服务器对象
		server.start();
	}
	public void start(int port){
		try {
			server = new ServerSocket(port);
			this.receive();//调用接收请求信息的方法
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void start(){
		this.start(8888);
	}
	private void receive() {
		
		try {
			//(1)监听
			Socket client = server.accept();
			//封装请求信息
			
			Request req = new Request(client.getInputStream());
			/*	StringBuilder sb = new StringBuilder();
			sb.append("HTTP/1.1").append(" ").append(200).append(" ").append("OK").append("\r\n");//响应头
			sb.append("Content-Type:text/html;charset=utf-8").append("\r\n");//内容文本,编码格式
			//内容
			
			String str="<html><head><title>响应结果</title></head><body>成功</body></html>";
			
			sb.append("Content-Length:"+str.getBytes("utf-8").length).append("\r\n");
			sb.append("\r\n");
			sb.append(str);
			//通过输出流发送出去
			BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(client.getOutputStream(),"utf-8"));
			bw.write(sb.toString());
			bw.flush();
			bw.close();*/
		/**
		 * 做出响应
		 * */
		Response rep=new Response(client.getOutputStream());
		Servlet servlet=WebApp.getServlet(req.getUrl());
		int code=200;
		if(servlet==null){
			code=404;
		}
		try {
			servlet.service(req,rep);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		rep.pushToClient(code);
		IOCloseUtil.closeAll(client);
		
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	}
	public void stop(){
		
	}

}

Request

package cn.bjsxt.server;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class Request {/*请求*/
	private InputStream is;//输入流
	private String requestInfo;//请求字符串,请求方式,请求路径,参数,协议,协议版本,请求正文。。。
	private String method;//请求的方式
	private String url;//请求的url
	public String getUrl() {
		return url;
	}	

	//输入框的name为key,值为value
	/*
	 * key: username            value:bjsxt
	 * key: pwd                 value:123
	 * key: hobby               value:read,ball
	 * */
	private Map<String,List<String>> parametermapValues;//参数
	private static final String CRLF="\r\n";//换行
	private static final String BLANK=" ";//空格
	//构造方法,初始化属性
	public Request(){
		parametermapValues=new HashMap<String,List<String>>();
		method="";
		url="";
		requestInfo="";
		
	}
	public Request(InputStream is){
		this();//调用本类无参的构造方法
		this.is=is;
		try {
			byte[] buf=new byte[20480];
			int len=this.is.read(buf);
			requestInfo=new String(buf,0,len);
		} catch (IOException e) {
			return;
		}
		//调用本类中的分解请求信息的方法
		this.parseRequestInfo();
	}
	//分解请求信息的方法
	/**
	 * 请求路径
	 * 请求方式
	 * 请求参数
	 * */
	private void parseRequestInfo(){
		String paraString="";//用于储存请求参数
		//获取请求参数的第一行
		String firstLine=requestInfo.substring(0,requestInfo.indexOf(CRLF)).trim();//从0,到第一行换行
		//分解出请求方式
		int index=firstLine.indexOf("/");
		this.method=firstLine.substring(0,index).trim();//获去0到第一个“/”的数据
		//分解url ,get可能包含参数,也有可能不包含参数post
		String urlString = firstLine.substring(index,firstLine.indexOf("HTTP/")).trim();
		//判断请求方式是GET还是POST
		if("get".equalsIgnoreCase(this.method)){//包含请求参数
			if(urlString.contains("?")){//?之前是路径,之后是参数
				String [] urlArray=urlString.split("\\?");//使用?分离路径与参数
				this.url=urlArray[1];
				paraString=urlArray[1];
			}else{ //post不包含请求参数
				this.url=urlString;				
			}			
		}else{//post不包含请求参数
			this.url=urlString;
			paraString=requestInfo.substring(requestInfo.lastIndexOf(CRLF)).trim();//获取最后一行
		}
		if(paraString.equals("")){
			return;
		}
		//请求参数
		//System.out.println(paraString);
		//调用本类中分解请求参数的方法
		this.parseParam(paraString);
	}
	//根据表单元素的name获取多个值
	public String [] getParamterValues(String name){
		//根据key获取value
		List<String> values=parametermapValues.get(name);
		if(values==null){
			return null;
		}else{
			return values.toArray(new String [0]);
		}
	}
	//根据表单元素获的name获取单个值
	public String  getParameter(String name){
		//调用本类中根据name获取多个值的方法
		String [] values=getParamterValues(name);
		if((values==null)){
			return null;
		}else{
			return values[0];
		}
    }
	/*public void show(){
		System.out.println(this.url);
		System.out.println(this.method);
	}*/
	//username=bjsxt&pwd=123&hobby=ball&hobby=paint
	/**
	 * username=bjsxt
	 * pwd123
	 * hobby=ball
	 * 
	 * username=
	 * */

	private void parseParam(String prarString){
		String [] token = prarString.split("&");
		for(int i=0;i<token.length;i++){
			String keyValues=token[i];
			String [] keyValue=keyValues.split("=");//username bjsxt pwd 123
			if(keyValue.length==1){ //username=
				keyValue=Arrays.copyOf(keyValue, 2);
				keyValue[1]=null;
				
			}
			//将表单元素的name与name对应的值储存到Map集合
			String key=keyValue[0].trim();
			String value=keyValue[1]==null?null:decode(keyValue[1].trim(),"utf-8");
			//放到集合中储存
			if(!parametermapValues.containsKey(key)){//如果键不存在,去创建
				parametermapValues.put(key, new ArrayList<String>());			
			}//如果键存在,将值添加进来
			List<String> values=parametermapValues.get(key);
			values.add(value);
		}
		
	}


		
	public static void main(String[] args){
		Request req = new Request();
		//调用分解参数的方法
		req.parseParam("username=%E5%8C%97%E4%BA%AC%E5%B0%9A%E5%AD%A6%E5%A0%82&pwd=123&hobby=ball&hobby=paint");
		Map<String,List<String>> map=req.parametermapValues;
		Set<String>key=map.keySet();
		for (String string : key) {
			System.out.println(string+"\t"+map.get(string));
		
		}
    }
	//处理中文,因为浏览器对中文进行了编码,进行解码
	private String decode(String value,String code){
		try {
			return URLDecoder.decode(value,code);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
}


运行图

image.png

代码包

http_server4.rar


JAVA 全系列/第二阶段:JAVA 基础深化和提高/手写服务器项目(旧) 3764楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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