导图社区 客户端服务器交互流程知识点笔记
“服务器端和客户端通信流程 1、服务器端通信流程 2、客户端通信流程 需要掌握的基础知识: 1、文件描述符:每一个文件描述符都在内核里面对应两个内存,一个内存是读缓冲区,用于接收数据;一个是写缓冲区,用于发送数据 1、创建用于监听的套接字,这个套接字是一个文件描述符,功能:检测是否有客户端发起的连接 下面介绍socket函数:
编辑于2022-11-07 18:00:57 广东客户端服务器交互流程知识点笔记
servlet清理成框架
在web站点创建servlet类
选择创建参数
给servlet命名/小写
servlet
清理无关参数代码
login.jsp
设置跳转链接和提交方式
编辑登陆主界面
success.jsp
post提交
fail.jsp
服务器servlet
doGet()
//将get请求转嫁给Post请求
this.doPost(request, response);
doPost()
response ()提前编码,加入charset=utf-8
判断提交方式
String method=request.getMethod();
if("GET".equals(method)){//当客户端采用get方式提交时得到提交方法名
}else if("POST".equals(method)){//当客户端采用post方式提交时得到提交方法名
09新建方法获得客户端参数
this.getParams(request);
方法获得具体的参数
10判断是否登陆成功
添加txt备注信息
11解决乱码
12post中文乱码
request.setCharacterEncoding("UTF-8");
13get中的乱码
get传递到服务器后,自定义转码方法
14将乱码转换为字节数组,再组装成utf.png
参数具体编码的实现
15登陆判断
A.服务器输出用户登录结果
添加内容request.setAttribute()
request.setAttribute("msg",userName+"登录成功!");
得到转发器对象并指定转发的目标页面/根目录
RequestDispatcher requestDispatcher= request.getRequestDispatcher("/success.jsp");
执行转发操作
requestDispatcher.forward(request, response);
2、post输出结果
//得到会话对象,session 30分钟死亡
HttpSession session=request.getSession();
添加内容request.setAttribute()
session.setAttribute("msg",userName+"登录成功!");
可选择重定向的错误页面,或转发
response.sendRedirect("error.jsp");
从客户端访问服务器
client/browser
get方式访问
1、将url的中文中文进行Unicode编码,否则出现乱码
URLEncoder.encode("张三","UTF-8");
String userName=URLEncoder.encode("张三","UTF-8");
2、获得完整网址和参数
String path="http://10.2.155.70:8080/LoginServerDemo01/loginServlet?userName="+userName+"&pwd=123";
3、getDataByGet(path)访问服务器并得到响应内容
1.根据网址得到URL对象
URL url=new URL(path);
2.通过调用url.openConnection()得到HttpURLConnection对象
HttpURLConnection httpURLConnection=(HttpURLConnection) url.openConnection();
3.设置请求方式和访问时间
httpURLConnection.setRequestMethod("GET");
httpURLConnection.setConnectTimeout(3000);
4.发送请求到服务器并得到响应码
int responseCode=httpURLConnection.getResponseCode();
5、如果响应码等于200表示本次访问成功
if(responseCode==200){
StringBuilder sb=new StringBuilder();
得到输入流对象读取服务器端发送回来的数据
InputStream inputStream=httpURLConnection.getInputStream();
byte[] buffer=new byte[1024];
int len=0;
while((len=inputStream.read(buffer))!=-1){
String content=new String(buffer,0,len,"UTF-8");//将字节流转成字符流
sb.append(content);
System.out.println("sb="+sb.toString());
post方式访问
1、将url的中文中文进行Unicode编码,否则出现乱码
URLEncoder.encode("张三","UTF-8");
String userName=URLEncoder.encode("张三","UTF-8");
2、获得网址没有参数
String path="http://10.2.155.70:8080/LoginServerDemo01/loginServlet";
3、建立map集合添加参数列表
Map<String,Object> map=new HashMap<String,Object>();
map.put("userName",userName);
map.put("pwd","123");
4、提交并获得服务器数据getDataByPost(path,map);
1.根据网址得到URL对象
URL url=new URL(path);
2.通过调用url.openConnection()得到HttpURLConnection对象
HttpURLConnection httpURLConnection=(HttpURLConnection) url.openConnection();
3.设置请求方式和访问时间
httpURLConnection.setRequestMethod("GET");
httpURLConnection.setConnectTimeout(3000);
4.允许向服务器端写入数据并得到输出流对象
httpURLConnection.setDoOutput(true);
OutputStream outputStream=httpURLConnection.getOutputStream();
5.使用输出流对象将数据写向服务器端
StringBuilder sb_param=new StringBuilder();
遍历结婚证entrySet
将缓冲字符转成字符串再转成字节型,并输出
outputStream.write(sb.toString().getBytes());
6.发送请求到服务器并得到响应码
int responseCode=httpURLConnection.getResponseCode();
7、如果响应码等于200表示本次访问成功
if(responseCode==200){
StringBuilder sb=new StringBuilder();
得到输入流对象读取服务器端发送回来的数据
InputStream inputStream=httpURLConnection.getInputStream();
byte[] buffer=new byte[1024];
int len=0;
while((len=inputStream.read(buffer))!=-1){
String content=new String(buffer,0,len,"UTF-8");//将字节流转成字符流
sb.append(content);
System.out.println("sb="+sb.toString());
System.Data.Common.NameValuePair
名称值对节点类
主要用途是在DBConnectionString类中,解析ConnectionString时存储并串联Name/Value对
tomcat中文乱码问题的原因及其解决方案图
客户端httpClient
1、将url的中文中文进行Unicode编码,否则出现乱码
URLEncoder.encode("张三","UTF-8");
String userName=URLEncoder.encode("张三","UTF-8");
2、获得网址没有参数
String path="http://10.2.155.70:8080/LoginServerDemo01/loginServlet";
3、建立map集合添加参数列表
Map<String,Object> map=new HashMap<String,Object>();
map.put("userName",userName);
map.put("pwd","123");
4、提交并获得服务器数据getDataByPost(path,map);
1.得到浏览器对象
httpClient=new DefaultHttpClient();
2.得到HttpPost对象
HttpPost httpPost=new HttpPost(path);
3.将相关参数设置到httpPost对象中
List<NameValuePair> parameters=new ArrayList<NameValuePair>();
for(Map.Entry<String, Object> item:map.entrySet()){ String name=item.getKey(); String value=item.getValue().toString(); parameters.add(new BasicNameValuePair(name, value)); }
HttpEntity entity=new UrlEncodedFormEntity(parameters, "UTF-8");
4.执行post请求
HttpResponse httpResponse=httpClient.execute(httpPost);
OutputStream outputStream=httpURLConnection.getOutputStream();
5.得到响应码
int responseCode=httpResponse.getStatusLine().getStatusCode();
String content=EntityUtils.toString(httpEntity);
6.得到浏览器返回的数据
HttpEntity httpEntity= httpResponse.getEntity();
7、如果响应码等于200表示本次访问成功
if(responseCode==200){
StringBuilder sb=new StringBuilder();
得到输入流对象读取服务器端发送回来的数据
InputStream inputStream=httpURLConnection.getInputStream();
byte[] buffer=new byte[1024];
int len=0;
while((len=inputStream.read(buffer))!=-1){
String content=new String(buffer,0,len,"UTF-8");//将字节流转成字符流
sb.append(content);
System.out.println("sb="+sb.toString());