Java34http&request:请求转发,登陆案例
Posted 码农编程录
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java34http&request:请求转发,登陆案例相关的知识,希望对你有一定的参考价值。
文章目录
1.http协议和抓包:StringBuilder线程不安全,request.getHeaderNames()
request对象:由tomcat创建,并且里面数据由tomcat set进去,我们只需要get出来。
File-New-Project-Java Enterprise
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<!--
http协议两种主要请求协议
1. get (默认, 只要看到可以发起请求,但是没看到请求方式设置)
2. post (需要收到设置)
-->
<h1>get请求方式</h1>
<form action="http://localhost:8080/MyServlet" method="get">
<input type="text" placeholder="请输入用户名" name="name"> <br>
<input type="text" placeholder="请输入密码" name="pwd"> <br>
<input type="submit">
</form>
<h1>post请求方式</h1>
<form action="/MyServlet" method="post"> <!-- 本服务器前面可以省略 -->
<input type="text" placeholder="请输入用户名" name="name"> <br>
<input type="text" placeholder="请输入密码" name="pwd"> <br>
<input type="submit">
</form>
</body>
</html>
如上html文件建在web文件夹路径下,如下创建的是servlet的模板。
package com.itheima01.http;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;
/**
* http://192.168.139.236:8080/MyServlet 或 MyServlet换成..请求报文.html
*
* #request对象核心功能: 获取前端的请求数据如下:
* 1. 请求行
* 请求方式 / 请求url / 协议
* 方法:
1. String getMethod() : 获取请求方式的类型
2. StringBuffer getRequestURL() : 获取客户端发出请求完整URL
3. String getProtocol(): 获取当前协议的名称和版本
4. String getRemoteAddr() : 获取IP地址
*
* 2. 请求头
* 1. 获取指定请求头的信息: value = request.getHeader("name");
2. 获取所有的请求头的name值:request.getHeaderNames();
*
* 3. 请求参数
* tomcat: 会根据不同的请求方式(自动get请求从url获取参数,post请求从请求体里获取参数),从不同的地方获取参数并设置到request对象,这样不需要知道过程,只有结果。
**/
@WebServlet(urlPatterns = "/MyServlet")
public class MyServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
//doGet方法是get请求方式下的service方法
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //doGet方法相当于get请求方式下的service方法
// System.out.println("服务器被访问了"); //idea下面显示
// response.getWriter().print("hello http"); //在网页上输出
// line(request); //请求行
String agent = request.getHeader("user-agent"); //出bug执行的
//用户访问我的网站突然崩溃,实际已获取了用户环境,在服务器所在地方可以模拟用户
String referer = request.getHeader("referer"); //防盗链
System.out.println(agent);
System.out.println(referer);
//11111111111111111111111111111111111111111111111111111111111111111111111111111111
HashMap<String, String> map = new HashMap<>();
Iterator<String> it = map.keySet().iterator();
while(it.hasNext()){
String key = it.next();
String value = map.get(key);
}
//如下枚举Enumeration相当于上面迭代器
Enumeration<String> it2 = request.getHeaderNames(); //=号右边等价于map.keySet().iterator()
while(it2.hasMoreElements()){
String name = it2.nextElement();
String value = request.getHeader(name);
System.out.println(name + "->" + value);
}
}
//11111111111111111111111111111111111111111111111111111111111111111111111111111111
private void line(HttpServletRequest request) { //请求行,抓包
String method = request.getMethod();
/*
StringBuffer : 线程安全(每个方法上都加synchronized),效率低,抛弃
StringBuilder : 也叫字符串缓冲区 (线程不安全【自己加锁,锁范围越小效率越高】,效率高)
**/
StringBuffer url = request.getRequestURL();
String protocol = request.getProtocol();
String remoteAddr = request.getRemoteAddr();
System.out.println(method);
System.out.println(url.toString());
System.out.println(protocol);
System.out.println("访问者的ip:" + remoteAddr);
}
}
如上浏览器网址中…报文.html是Referer中的上一次访问页面,因为已跳转到如下下个页面。
如下请求行中的请求url
省略了如上的http://localhost:8080。cookie是浏览器缓存的一种。get请求中最后一行不是请求体,是谷歌浏览器工具抓包进行的参数强调渲染。
2.request对象:.getParameterMap(),post请求中文参数乱码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>get请求</h1>
<form action="/ParamServlet" method="get">
用户名: <input type="text" name="name"> <br>
密码: <input type="password" name="pwd"> <br>
性别: <input type="radio" name="gender" value="boy">男
<input type="radio" name="gender" value="girl"> 女<br>
爱好:
<input type="checkbox" name="hobby" value="smoke"> 抽烟
<input type="checkbox" name="hobby" value="drink"> 喝酒
<input type="checkbox" name="hobby" value="firehead"> 烫头 <br>
<input type="submit">
</form>
<h1>post请求</h1>
<form action="/ParamServlet" method="post">
用户名: <input type="text" name="name"> <br>
密码: <input type="password" name="pwd"> <br>
性别: <input type="radio" name="gender" value="boy">
<input type="radio" name="gender" value="girl"> <br>
爱好:
<input type="checkbox" name="hobby" value="smoke"> 抽烟
<input type="checkbox" name="hobby" value="drink"> 喝酒
<input type="checkbox" name="hobby" value="firehead"> 烫头 <br>
<input type="submit">
</form>
</body>
</html>
package com.itheima02.param;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Arrays;
import java.util.Map;
import java.util.Set;
/**
请求参数: parameter ?n1=v1&n2=v2...
方法:
1. 根据name值获取value值: String value = request.getParameter(name);
2. 根据name值获取多个value值:String[] value = request.getParameterValues(name);
3. 获取所有的请求参数,封装到map中:Map<String, String[]> map = request.getParameterMap();
name为String,value为String[]
问题: post请求中文参数乱码
原因: 编解码使用的字符集不一样
编码: 浏览器 ( utf-8 )
解码: 服务器 (tomcat + servlet)
ISO-8859-1 utf-8
解决: tomcat的编码表改成utf-8
1. 永久
2. 临时 (选用) request.setCharacterEncoding("utf-8"); 注意: 必须放在获取参数之前
【get请求 tomcat8以上被优化 不乱码(post请求参数在请求体中,走io流)】
**/
@WebServlet(urlPatterns = "/ParamServlet")
public class ParamServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
doGet(request, response);
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// request.setCharacterEncoding("utf-8");
String name = request.getParameter("name");
String pwd = request.getParameter("pwd");
String gender = request.getParameter("gender");
String[] hobbies = request.getParameterValues("hobby"); //复选框可重复
System.out.println(name+"," + pwd + "," + gender + "," + Arrays.toString(hobbies));
//11111111111111111111111111111111111111111111111111111111111111111111111111111111111
Map<String, String[]> map = request.getParameterMap();
// System.out.println(map); //这个map没有重写tostring方法
Set<String> set = map.keySet();
for (String key : set) {
String[] value = map.get(key); //value数组没有重写tostring方法,不能
// System.out.println(key + "=" + Arrays.toString(value));
}
}
}
如下选中的是map遍历出来的结果和下面第一行一样。
3.请求转发:jrss,request.setAttribute()
request表面上获取请求数据,还有快递员身份。
如下forward方法中request是邮件及内容,response是回应权限。A模块指MyServlet,B模块指ZhouServlet。一次请求链如下只有一去一回。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
人事
<!--<a href="http://localhost:8080/ShiServlet?msg=有人来面试">发送邮件</a>-->
<a href="/ShiServlet?msg=有人来面试">发送邮件</a> <!--点发送邮件后,地址栏修改为/Shi..,没写就是get请求-->
</body>
</html>
package com.itheima03.transfer;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet(urlPatterns = "/ShiServlet")
public class ShiServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//1. 接收请求
String msg = request.getParameter("msg");
System.out.println("shi:" + msg);
System.out.println("shi:我现在没空,叫周楠同学帮我面试");
request.setAttribute("extra","比我帅的不要~~"); //添加备注,这行注掉不写,ZhouServlet写request.getAttribute会得到null
//2. 请求转发
// RequestDispatcher dispatcher = request.getRequestDispatcher("/ZhouServlet"); //比作邮件服务器或快递员
// dispatcher.forward(request,response); //快递员发货
request.getRequestDispatcher("/ZhouServlet").forward(request,response); //这行等价上面两行
}
}
package com.itheima03.transfer;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet(urlPatterns = "/ZhouServlet")
public class ZhouServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
以上是关于Java34http&request:请求转发,登陆案例的主要内容,如果未能解决你的问题,请参考以下文章