当前位置:网站首页>2021.8.9 note request
2021.8.9 note request
2022-07-27 18:33:00 【It's food, not shellfish】
request
principle


Inheritance system

Get request line data _ Method
One of the most important is 2. Get virtual directories and 5. Get request URI
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String method = request.getMethod();
System.out.println(method);
String contextPath = request.getContextPath();
System.out.println(contextPath);
String servletPath = request.getServletPath();
System.out.println(servletPath);
String queryString = request.getQueryString();
System.out.println(queryString);
String requestURI = request.getRequestURI();
StringBuffer requestURL = request.getRequestURL();
System.out.println(requestURI);
System.out.println(requestURL);
String remoteAddr = request.getRemoteAddr();
System.out.println(remoteAddr);
String protocol = request.getProtocol();
System.out.println(protocol);
}
Get request header data
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Enumeration<String> headerNames = request.getHeaderNames();
while(headerNames.hasMoreElements()) {
String name = headerNames.nextElement();
String value = request.getHeader(name);
System.out.println(name + "-----" + value);
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String agent = request.getHeader("user-agent");
if(agent.contains("Firefox")) {
System.out.println(" Here comes Firefox ...");
}else if(agent.contains("Chrome")) {
System.out.println(" Here comes Google ");
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String referer = request.getHeader("referer");
if(referer != null) {
if(referer.contains("/day14")) {
response.setContentType("text/html;charset=utf-8");
response.getWriter().write(" Play ");
}else {
response.setContentType("text/html;charset=utf-8");
response.getWriter().write(" Do not give ");
}
}
}
By creating two tomcat And modify the virtual directory , A no /day14, One can verify
Get request body data

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="/demo4" method="post">
<input name="username">
<input type="submit" value=" Submit ">
</form>
</body>
</html>
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.BufferedReader;
import java.io.IOException;
@WebServlet("/requestDemo5")
public class RequestDemo5 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
BufferedReader reader = request.getReader();
String str = null;
while((str = reader.readLine()) != null) {
System.out.println(str);
}
}
}
Get request parameters in general

get and post You can use
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Map;
import java.util.Set;
@WebServlet("/requestDemo6")
public class RequestDemo6 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request.getParameter("username");
System.out.println("get");
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();
String value = request.getParameter(name);
System.out.println(value);
}
System.out.println("----------");
Map<String, String[]> parameterMap = request.getParameterMap();
Set<String> strings = parameterMap.keySet();
for (String name : strings) {
System.out.println(name);
String[] value = parameterMap.get(name);
for (String va : value) {
System.out.println(va);
}
System.out.println("------------");
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
}
Chinese garbled
post stay input When inputting Chinese , When outputting the corresponding input data, it will be garbled , The solution is request.setCharacterEncoding(“utf-8”);
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
String username = request.getParameter("username");
System.out.println(username);
}
Request forwarding

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("demo8 Was interviewed ...");
request.getRequestDispatcher("/requestDemo9").forward(request,response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println("demo9 Was interviewed ...");
}
Shared data ( Domain object )


@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setAttribute("msg","hello");
request.getRequestDispatcher("/requestDemo9").forward(request,response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Object name = request.getAttribute("msg");
System.out.println(name);
System.out.println("demo9 Was interviewed ...");
}
getServletContext
I didn't say that
边栏推荐
- Deep learning: GCN (graph convolution neural network) theory learning summary
- 动态链表3队列的链式存储结构(LinkedQueue实现)
- [MIT 6.S081] Lec 4: Page tables 笔记
- [mit 6.s081] LEC 5: calling conventions and stack frames risc-v notes
- [MIT 6.S081] Lab 7: Multithreading
- 3. Opencv geometric transformation
- Technology sharing | quick intercom integrated dispatching system
- [MIT 6.S081] Lab 7: Multithreading
- Deep learning: installation package records
- 2021.7.13笔记 子查询
猜你喜欢
![[mit 6.s081] LEC 8: page faults notes](/img/e2/0f5332dd9d2b439bcf29e87a9fa27f.png)
[mit 6.s081] LEC 8: page faults notes

2021.8.9笔记 request

Navicat 导出表生成PDM文件

Three consecutive high-frequency interview questions of redis online celebrity: cache penetration? Cache breakdown? Cache avalanche?
![[MIT 6.S081] Lab 7: Multithreading](/img/f4/26e513fb8678a88cfba29c1a636b37.png)
[MIT 6.S081] Lab 7: Multithreading
![[mit 6.s081] LEC 1: introduction and examples notes](/img/5d/2fc4bde8eebbb22605d314b5292e05.png)
[mit 6.s081] LEC 1: introduction and examples notes

2021.7.13 note sub query

发布自己的npm组件库

Common commands of database 2

深度学习:STGCN学习笔记
随机推荐
2021.8.6笔记 jsoup
Common commands of database 1
Deep learning: Gan case exercise -minst handwritten digits
The first PCIe 5.0 SSD master of Jiangsu Huacun: TSMC 12NM process, mass production in 2020
[MIT 6.S081] Lab 9: file system
[mit 6.s081] LEC 4: page tables notes
2021.8.9笔记 request
[MIT 6.S081] Lab 7: Multithreading
微信小程序微信支付概述
Wechat applet wxacode.getunlimited generates applet code
[MIT 6.S081] Lab8: locks
江苏华存首发PCIe 5.0 SSD主控:台积电12nm工艺,2020年量产
An analysis of CPU explosion of a smart logistics WCS system in.Net
Announcing the acquisition of 30% shares of Wenye, what is the general intention of Dalian United?
[MIT 6.S081] Lab 10: mmap
数据库的常用命令2
超实用!阿里P9私藏的Kubernetes学习笔记,看完直呼NB
JDBC学习 Day1:JDBC
After being "expelled" from bitland, the Jank group said for the first time: it will return as soon as possible through legal channels!
请教大神一个问题 flinkcdc,同步mysql中的datetime字段会变为时间戳 有人遇到过吗