会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132368个问题
Python 全系列/第十九阶段:数据分析-数据管理/数据分析案例_案例2_电影数据分析 37547楼
JAVA 全系列/第一阶段:JAVA 快速入门/数组和数据存储 37549楼
Python 全系列/第一阶段:Python入门/编程基本概念 37550楼
JAVA 全系列/第八阶段:Linux入门到实战/Linux(旧) 37551楼
Python 全系列/第十五阶段:Python 爬虫开发/爬虫基础 37552楼
Python 全系列/第一阶段:Python入门/函数和内存分析 37554楼

你好,老师,我是按照视频中的代码敲得,但是最后在浏览器没有显示登陆成功的字样

package com.hy.test;
/***
 * 在html中使用post方法,通过程序然后获取输入的信息,并且对web浏览器做出回应
 */

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.ServerSocket;
import java.net.Socket;



public class Server3 {
	public static void main(String[] args) {
		String BLANK="\n";
		String CRLF="\r\n";//换行
		//1创建ServerSocket对象
		ServerSocket server=null;
		//2监听是否有客户端发来请求
		Socket client=null;
		//3创建Socket对象	
				//4创建输入流
		InputStream is=null;
		try {
			server = new ServerSocket(8080);	
			client = server.accept();
			is=client.getInputStream();
			byte[] buf=new byte[10240];
			int len=is.read(buf);
			System.out.println(new String(buf,0,len));
			/***
			 * 以下代码是对浏览器的请求做出响应
			 */
			StringBuilder sb=new StringBuilder();
			StringBuilder sbContent=new StringBuilder();//响应的文本
			sbContent.append("<html><head><title>响应结果</title></head>");
			sbContent.append("<body>登陆成功</body></html>");
			//1拼接像迎头
			sb.append("HTTP/1.1").append(BLANK).append(200).append(BLANK).append("ok");
			sb.append(CRLF);//换行
			sb.append("Content-Type: text/html;charset=utf-8");
			sb.append(CRLF);
			sb.append("Content-Length:").append(sbContent.toString().getBytes().length).append(CRLF);//换行,代表响应头和相应正文之间的空行
			sb.append(sbContent);
			
			//通过流输出
			BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(client.getOutputStream(),"utf-8"));
			bw.write(sb.toString());
			bw.flush();
			bw.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
		//关闭流
		try {
			IOClose.closeAll(is,client,server);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		}
	}

	private static InputStream InputStream(InputStream inputStream) {
		// TODO Auto-generated method stub
		return null;
	}
}

html代码

<html>
<head>
<title>我的第一个html</title>
</head>
<body>
<h1>hello world</h1>
<form action="http://localhost:8080/index.html" method="post">
<p>用户名:<input type="text" id="uname" name="username" /></p>
<p>密码:<input type="password" id="pwd" name="password" /></p>
<p><input type="submit" value="登陆"/></p>
</form>
</body>
</html>


image.png


 浏览器没有显示,但是eclipse控制台输出的正常的,所以想问一下是什么原因,感谢

image.png

JAVA 全系列/第二阶段:JAVA 基础深化和提高/手写服务器项目(旧) 37555楼
大数据全系列/第十三阶段:Elasticsearch 分布式搜索/ElasticSearch 分布式搜索(旧) 37556楼
Python 全系列/第二阶段:Python 深入与提高/GUI编程(隐藏) 37559楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

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