《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》,点击传送门,即可获取!
String password = req.getParameter(“password”);
resp.setContentType(“text/html; charset=utf-8”);
Writer writer = resp.getWriter();
writer.write(“”);
writer.write("
firstName: " + firstName + “</h1”);
writer.write(“
”);
writer.write("
secondName: " + secondName + “
”);writer.write(“
”);
writer.write("
password: " + password + “
”);writer.write(“”);
}
}
- 获取header部分
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Writer;
import java.util.Enumeration;
public class HttpServletTest3 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//resp.setContentType(“text/html; charset=utf-8”);
resp.setContentType(“text/html”);
resp.setCharacterEncoding(“utf-8”);
//返回的是一个枚举
Enumeration enumeration = req.getHeaderNames();
Writer writer = resp.getWriter();
writer.write(“”);
while (enumeration.hasMoreElements()) {
String header = enumeration.nextElement();
writer.write(“
” + header + ": " + req.getHeader(header) + “
”);}
writer.write(“</html”);
}
}
==================================================================================
- 返回一个404
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class HttpServletTest5 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 得到一个 Tomcat 内置的错误页面.
resp.sendError(404);
}
}
- 返回时间并设置一秒刷新一次
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Writer;
import java.util.Date;
public class HttpServletTest4 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 每秒自动刷新
// 返回的页面中, 填写上当前时间.
// 获取当前时间
// System.currentTimeMillis();
// new Date();
// new Calender();
resp.setContentType(“text/html; charset=utf-8”);
//设置header部分 1 表示每间隔一秒
resp.setIntHeader(“Refresh”, 1);
Date date = new Date();
Writer writer = resp.getWriter();
writer.write(“”);
writer.write(“
” + date.toString() + “
”);writer.write(“”);
}
}
- 返回重定向
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class HttpServletTest6 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//重定向到一个网页 这个方法自动帮我们设置了respond的状态码和状态描述等相关信息
resp.sendRedirect(“https://blog.csdn.net/Shangxingya/article/details/107295800”);
}
}
==============================================================================
=================================================================================
- 设置cookie
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class HttpServletTest7 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//构建一个Cookie
Cookie name = new Cookie(“userName”, “listen”);
Cookie password = new Cookie(“password”, “listenY”);
//把cookie写到respond中
resp.addCookie(name);
resp.addCookie(password);
resp.setContentType(“text/html; charset=utf-8”);
resp.getWriter().write(“
设置Cookie成功
”);}
}
- 或得cookie
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Writer;
public class HttpServletTest8 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获得Cookie字段
Cookie[] cookies = req.getCookies();
//将所有的cookie输出到响应报文中
resp.setContentType(“text/html; charset=utf-8”);
Writer writer = resp.getWriter();
writer.write(“”);
for (Cookie c : cookies
) {
writer.write(“
” + c.getName() + ": " + c.getValue() + “
”);}
writer.write(“”);
}
}
- 删除cookie
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.Writer;
public class HttpServletTest10 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//删除现有的cookie
//获取所有cookie 然后将这些cookie的生命时间设置为0
//将这些cookie保存在响应报文中
Cookie[] cookies = req.getCookies();
// 设置响应内容类型
resp.setContentType(“text/html; charset=utf-8”);
if (cookies != null) {
for (Cookie c : cookies
) {
c.setMaxAge(0);
resp.addCookie©;
}
Writer writer = resp.getWriter();
writer.write(“”);
writer.write(“
删除cookie成功
”);} else {
Writer writer = resp.getWriter();
writer.write(“”);
writer.write(“
当前没有cookie
”);}
}
}
==================================================================================
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.Writer;
public class HttpServletTest9 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// 1. 先获取 Session. 如果用户曾经没有访问过, 此时将创建新的 session
// 如果用户已经访问过了, 就获取到曾经的 Session
// 新用户访问:
// 这个操作就会自动生成一个 sessionId, 同时创建一个 httpSession 对象
// 把这个键值对放到内置的 hash 表中. 同时把 sessionId 写回到浏览器的
// Cookie 中
// 老用户访问:
// 根据请求中 Cookie 里的 sessionId, 在 hash 表中查, 找到对应的 session 对象
HttpSession session = req.getSession(true);
// 2.判断是否为新用户
Integer count = 1;
if (session.isNew()) {
//新用户
// 把 count 值写入到 session 对象中.
// httpSession 也可以当成是一个 hash 表.
session.setAttribute(“count”, count);
} else {
// 老用户
// 从 httpSession 中读取到 count 值
count = (Integer) session.getAttribute(“count”);
count++;
session.setAttribute(“count”, count);
}
resp.setContentType(“text/html; charset=utf-8”);
Writer writer = resp.getWriter();
writer.write(“”);
writer.write(“
” + session.getAttribute(“count”).toString() + “
”);writer.write(“”);
}
}
============================================================================
- 前端表单
选择上传的文件
- 文件传输代码
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.http.HttpServlet;
总结
三个工作日收到了offer,头条面试体验还是很棒的,这次的头条面试好像每面技术都问了我算法,然后就是中间件、MySQL、Redis、Kafka、网络等等。
- 第一个是算法
关于算法,我觉得最好的是刷题,作死的刷的,多做多练习,加上自己的理解,还是比较容易拿下的。
而且,我貌似是将《算法刷题LeetCode中文版》、《算法的乐趣》大概都过了一遍,尤其是这本
《算法刷题LeetCode中文版》总共有15个章节:编程技巧、线性表、字符串、栈和队列、树、排序、查找、暴力枚举法、广度优先搜索、深度优先搜索、分治法、贪心法、动态规划、图、细节实现题
《算法的乐趣》共有23个章节:
- 第二个是Redis、MySQL、kafka(给大家看下我都有哪些复习笔记)
基本上都是面试真题解析、笔记和学习大纲图,感觉复习也就需要这些吧(个人意见)
- 第三个是网络(给大家看一本我之前得到的《JAVA核心知识整理》包括30个章节分类,这本283页的JAVA核心知识整理还是很不错的,一次性总结了30个分享的大知识点)
《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》,点击传送门,即可获取!
加上自己的理解,还是比较容易拿下的。
而且,我貌似是将《算法刷题LeetCode中文版》、《算法的乐趣》大概都过了一遍,尤其是这本
《算法刷题LeetCode中文版》总共有15个章节:编程技巧、线性表、字符串、栈和队列、树、排序、查找、暴力枚举法、广度优先搜索、深度优先搜索、分治法、贪心法、动态规划、图、细节实现题
[外链图片转存中…(img-07eeU1oI-1714674700034)]
《算法的乐趣》共有23个章节:
[外链图片转存中…(img-e16b79Gp-1714674700035)]
[外链图片转存中…(img-C5AZwmZR-1714674700035)]
- 第二个是Redis、MySQL、kafka(给大家看下我都有哪些复习笔记)
基本上都是面试真题解析、笔记和学习大纲图,感觉复习也就需要这些吧(个人意见)
[外链图片转存中…(img-yDtSk6JL-1714674700035)]
- 第三个是网络(给大家看一本我之前得到的《JAVA核心知识整理》包括30个章节分类,这本283页的JAVA核心知识整理还是很不错的,一次性总结了30个分享的大知识点)
[外链图片转存中…(img-NPSsw8ej-1714674700036)]
《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》,点击传送门,即可获取!