-
enctype属性默认的值为“ application/x-www-form-urlencoded ”
-
使用"application/x-www-form-urlencoded“编码方式的请求正文,HttpServletRequest接口提供的获取方法如下:
| 方法 | 方法描述 |
| — | — |
| String getParameter(String name) | 返回由name指定的用户请求参数的值 |
| Enumeration getParameterNames() | 返回所有客户请求的参数名 |
| String[] getParameterValues(String name) | 返回由name指定的用户请求参数的值 |
| Map getParameterMap() | 返回一个请求参数的Map,Map中的键为参数的名称,值为参数名对应的参数值 |
演示:
创建register.jsp
<%@ page language=“java” contentType=“text/html; charset=UTF-8”
pageEncoding=“UTF-8”%>
用户名:
密码:<input
type=“password” name=“pwd” />
性别:<input type=“radio”
name=“gender” value=“1” checked=“checked” />男 <input type=“radio”
name=“gender” value=“2” />女
爱好:<input type=“checkbox”
name=“hobby” value=“lq” checked=“checked”>篮球 <input
type=“checkbox” name=“hobby” value=“code”>编程 <input
type=“checkbox” name=“hobby” value=“sing”>唱歌
所在城市: <select
name=“city”>
---请选择---- 北京 广州 上海
运行
后端,创建RegisterServlet
package com;
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;
/**
- Servlet implementation class RegisterServlet
*/
@WebServlet(“/RegisterServlet”)
public class RegisterServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
- @see HttpServlet#HttpServlet()
*/
public RegisterServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
-
@see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
-
response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding(“UTF-8”);
response.setContentType(“text/html;charset=utf-8”);
String name = request.getParameter(“userName”);
String pwd = request.getParameter(“pwd”);
String gender = request.getParameter(“gender”);
String[] hobbys = request.getParameterValues(“hobby”);
String city = request.getParameter(“city”);
System.out.println(“姓名为:” + name);
System.out.println(“密码为:” + pwd);
if (gender != null && gender.equals(“1”)) {
System.out.println(“姓名为:男”);
} else if (gender != null && gender.equals(“2”)) {
System.out.println(“姓名为:女”);
} else {
System.out.println(“请选择性别”);
}
System.out.print(“兴趣爱好:”);
for (String hobby : hobbys) {
System.out.print(hobby + “\t”);
}
System.out.println();
System.out.println(“所在城市:” + city);
}
/**
-
@see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
-
response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
运行
查看控制台获取数据
enctype属性只有在<form>
表单向服务器上传文件时才会设置值为“multipart/form-data”,并且此属性值只适用于POST请求方式。
<input name=”文件城名称”type=“file”>
<input type= submit" value=" 提交">
演示:
创建upLoad.jsp
<%@ page language=“java” contentType=“text/html; charset=UTF-8”
pageEncoding=“UTF-8”%>
文件名称:
上传文件:
运行
创建UploadServlet
package com;
import java.io.IOException;
import java.io.InputStream;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
- Servlet implementation class UploadServlet
*/
@WebServlet(“/UploadServlet”)
public class UploadServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
- @see HttpServlet#HttpServlet()
*/
public UploadServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
-
@see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
-
response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding(“utf-8”);
InputStream in = request.getInputStream();
byte[] b = new byte[1024];
in.read(b);
System.out.println(new String(b));
}
/**
-
@see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
-
response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
运行
查看控制台输出
3.存取请求域属性
-
存储在 HttpServletRequest 对象中的对象称之为请求域属性,属于同一个请求的多个处理组件之间可以通过请求域属性来传递对象数据 。
-
HttpServletRequest 接口提供了如下与请求域属性相关的方法:
| 方法 | 方法描述 |
| — | — |
| void setAtrribute(String name,Object value) | 设定name属性的值为value,保存在request范围内 |
| Object getAttribute(String name) | 从request范围内获取name属性的值 |
| void removeAttribute(String name) | 从request范围内移除name属性的值 |
| Enumeration getAttributeNames() | 获取所有request范围的属性 |
创建RequestScopeAttrServlet
package com;
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;
/**
- Servlet implementation class RequestScopeAttrServlet
*/
@WebServlet(“/RequestScopeAttrServlet”)
public class RequestScopeAttrServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
- @see HttpServlet#HttpServlet()
*/
public RequestScopeAttrServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
-
@see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
-
response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setAttribute(“msg1”, “request域中的value值1”);
request.setAttribute(“msg2”, “request域中的value值2”);
request.getRequestDispatcher(“/ResultServlet”).forward(request, response);
}
/**
-
@see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
-
response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
创建ResultServlet
package com;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
- Servlet implementation class ResultServlet
*/
@WebServlet(“/ResultServlet”)
public class ResultServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
- @see HttpServlet#HttpServlet()
*/
public ResultServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
-
@see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
-
response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding(“utf-8”);
response.setContentType(“text/html;charset=utf-8”);
String value01 = (String) request.getAttribute(“msg1”);
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数Java工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注Java获取)
![img](https://i-blog.csdnimg.cn/blog_migrate/b08a3bcbf9ff65d0046ab90f8c1e9b03.jpeg)
1200页Java架构面试专题及答案
小编整理不易,对这份1200页Java架构面试专题及答案感兴趣劳烦帮忙转发/点赞
百度、字节、美团等大厂常见面试题
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!
mg-yqIrH0IL-1713119705122)]
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
如果你觉得这些内容对你有帮助,可以扫码获取!!(备注Java获取)
![img](https://i-blog.csdnimg.cn/blog_migrate/b08a3bcbf9ff65d0046ab90f8c1e9b03.jpeg)
1200页Java架构面试专题及答案
小编整理不易,对这份1200页Java架构面试专题及答案感兴趣劳烦帮忙转发/点赞
[外链图片转存中…(img-6DfFJCrX-1713119705122)]
[外链图片转存中…(img-bqKZSelj-1713119705123)]
百度、字节、美团等大厂常见面试题
[外链图片转存中…(img-SJdrAV5f-1713119705123)]
《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!