根据参数名称或者参数值
实现:html文件提交数据,Demo获取提交数据。
**HTML文件**
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>注册页面</title>
</head>
<body>
<form action="/Demo/demo7" method="post">
<input type="text" placeholder="请输入用户名" name="username"><br>
<input type="text" placeholder="请输入密码" name="password"><br>
<input type="checkbox" name="hobby" value="game">游戏
<input type="checkbox" name="hobby" value="study">java
<br>
<input type="submit" value="注册">
</form>
</body>
</html>
Demo7根据HTML提交到的参数进行获取
**Demo7**
//post 获取请求参数
//根据参数名称获取参数值
String username = request.getParameter("username");
/* System.out.println("post");
System.out.println(username);*/
//根据参数名称获取参数值的数组
String[] hobbies = request.getParameterValues("hobby");
/*for (String hobby : hobbies) {
System.out.println(hobby);
}*/
//获取所有请求的参数名称
Enumeration<String> parameterNames = request.getParameterNames();
while(parameterNames.hasMoreElements()){
String name = parameterNames.nextElement();
System.out.println(name);
String value = request.getParameter(name);
System.out.println(value);
System.out.println("----------------");
}*/
// 获取所有参数的map集合
Map<String, String[]> parameterMap = request.getParameterMap();
//遍历
Set<String> keyset = parameterMap.keySet();6
for (String name : keyset) {
//获取键获取值
String[] values = parameterMap.get(name);
System.out.println(name);
for (String value : values) {
System.out.println(value);
}
System.out.println("-----------------");
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//get 获取请求参数
/*
//根据参数名称获取参数值
String username = request.getParameter("username");
System.out.println("get");
System.out.println(username);*/
this.doPost(request,response);
}
}
中文乱码:
get方式:tomcat已经将get方式乱码问题解决。
post方式:会乱码
解决:设置request的编码:
req.setCharacterEncoding("utf-8");
请求转发 : 一种在服务器内部的资源跳转
案例一:
启动Demo8转发到了Demo9的数据
**Demo8**
package request;
import java.io.IOException;
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;
@WebServlet("/demo8")
public class Demo8 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("dome8被访问了 滴滴");
//转发到Demo9资源
req.getRequestDispatcher("/demo9").forward(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
}
}
Demo9
***Demo9***
package request;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/demo9")
public class Demo9 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("dome9被访问了 滴滴");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
}
}
这里插入代码片
特点:
1.浏览器地址栏路径没有发生变化,
2.只能转发到当前服务器内部资源中,
3.转发是一次请求,
共性数据: request作用范围一次请求
- void setAttribute(String name , Object obj) : 存储数据
- Object getAttribute(String name): 通过键获取值
- void removeAttribute( String name ): 通过键移除键值对
**Demo8**
//储存数据到req域中
req.setAttribute("java", "好有趣哇");
//转发到Demo9资源
req.getRequestDispatcher("/demo9").forward(req, resp);
System.out.println("dome9被访问了 滴滴");
Object java = req.getAttribute("java"); //注意getAttribute的返回值类型是Object,
//所以需要用Object类型接收
System.out.println(java);
笔记做的有点乱