会员可以在此提问,尚新途老师有问必答

对大家有帮助的问答会被标记为“推荐”,看完课程过来浏览一下别人提的问题,会帮你学得更全面

截止目前,同学们一共提了128541个问题
二筒2018-12-27 15:33:11

请问有API文档吗?                    

相关课程:JAVA 全系列/第一阶段:JAVA 快速入门/飞机大战小项目训练
艾伦2018-06-28 16:55:14

醉了.png

老师,这个捕获异常的快捷键是什么?试了好几个不行,我的是win7系统。

相关课程:JAVA 全系列/第二阶段:JAVA 基础深化和提高/IO 流技术(旧)
微信用户2018-10-08 17:19:25
long 型不能赋给int 型,为什么写成int total =money *(long)(years)就可以呢?
相关课程:JAVA 全系列/第一阶段:JAVA 快速入门/变量、数据类型、运算符
gracebxp2019-01-27 13:03:56

有关无法解析的问题已解决,不需要加上{}符号

相关课程:JAVA 全系列/第二阶段:JAVA 基础深化和提高/XML 技术(旧)
wechatkU5KFJ2018-12-01 14:34:33

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

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

相关代码:

package com.like.server;

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

import com.like.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("utf-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.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;charse=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) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void close() {
		IOCloseUtil.closeAll(bw);
	}
}

Server:

public class Server {//服务器,用于启动和停止服务器
	private ServerSocket server;
	public static void main(String[] args) {
		Server server=new Server();
		server.start();
	}
	public void start() {
		this.start(8888);
	}
	public void start(int port) {
		try {
			server=new ServerSocket(port);
			this.receive();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void receive() {
		//监听
		try {
			Socket client=server.accept();
			/*
			//获取用户的请求
			InputStream is=client.getInputStream();
			byte[] buf=new byte[20480];
			int len=is.read(buf);
			System.out.println(new String(buf,0,len));*/
			//封装请求信息
			Request req=new Request(client.getInputStream());
			req.show();
			
			Response rep=new Response(client.getOutputStream());
			Servlet servlet=WebApp.getServlet(req.getUrl());
			int code=200;
			if(servlet==null) {
				code=404;
			}
			//调用Servlet中的服务方法
			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() {}
}

HTML

<html>
	<head>
		<title>我是第一个html</title>
	</head>
	<body>
		<h1>hello world!</h1>
		<form action="http://localhost:8888/log" method="get">
			<p>用户名:<input type="text" id="uname" name="username" /></p>
			<p>密&nbsp&nbsp码:<input type="password" id="pwd" name="password" /></p>
			<p>兴趣爱好:<input type="checkbox" name="hobby" value="ball"/>球
			<input type="checkbox" name="hobby" value="read"/>读书
			<input type="checkbox" name="hobby" value="paint"/>画画</p>
			<p><input type="submit" value="登录"/></p>
		</form>
	</body>
</html>

异常截图如下:

blob.png

在这之前的检测都是正常的。

源代码:

http_server6.zip


相关课程:JAVA 全系列/第二阶段:JAVA 基础深化和提高/手写服务器项目(旧)
Arsn2018-05-31 19:31:18

在设置PATH_HOME变量时,为什么前后都要加%

相关课程:JAVA 全系列/第一阶段:JAVA 快速入门/JAVA入门和背景知识
sdxcool2018-12-25 17:43:39

老师,请问一下,在navicat中将ldf导入到mysql,是不是得先在sql serve里将ldf文件改成csv格式之类的啊

相关课程:JAVA 全系列/第三阶段:数据库编程/MySQL数据库的使用
改个ID2018-09-02 09:33:49

捕获.PNG

HashMap是对键值对的存放,

图中老师讲的这个是不是应该是比较Key的hashcode 如果内容相同进行覆盖啊?

相关课程:JAVA 全系列/第二阶段:JAVA 基础深化和提高/容器(旧)
木土2021-08-24 17:06:59

老师为什么这个scanner显示出错啊,我看不懂它的报错。TestScanner.jpg

相关课程:JAVA 全系列/第一阶段:JAVA 快速入门/变量、数据类型、运算符

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