《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》,点击传送门,即可获取!
HttpServletRequest代表客户端的请求,用户通过Http协议访问服务器,HTTP请求中的所有信息会被封装到HttpServletRequest,通过这个HttpServletRequest的方法,获得客户端的所有信息
1)概述
类似于HttpServletResponse
HttpServletRequest是接口,继承自ServletRequest接口
public interface HttpServletRequest extends ServletRequest {}
- 相同点:都会实现页面的跳转
| 区别 | 请求转发 | 重定向 |
| — | — | — |
| 操作对象 | ServletContext 和 ServletRequest | HttpServletResponse |
| 方法 | getRequestDispatcher().forward() | sendRedirect() |
| url | 地址栏url不会发生变化
作用域的数据会被携带转发,包含有 getContextPath()
,不用再显示添加 | 地址栏url会发生变化
应该在 url 中加上 getContextPath()
|
| WEB-INF | 可以指向WEB-INF
下的页面 | 不可以指向WEB-INF
下的页面,否则会报404 |
==========================================================================
-
会话:用户打开一个浏览器,点击了很多超链接,访问多个web资源,关闭浏览器,这个过程可以称之为会话;
-
有状态会话:一个同学来过教室,下次再来教室,我们会知道这个同学,曾经来过,称之为有状态会话;
- Cookie
客户端技术 (响应,请求)
- Session
服务器技术,利用这个技术,可以保存用户的会话信息;可以把信息或者数据放在Session中!
Cookie是一个类,在包package javax.servlet.http中
可复制、可序列化
package javax.servlet.http;
public class Cookie implements Cloneable, Serializable{}
1)创建Cookie
// 构造器
Cookie(String name, String value)
2)设置有效期
// 设置有效期:单位秒
cookie.setMaxAge(24 * 60 * 60);
Cookie不设有效期,或者有效期为0时,关闭浏览器后不保存
3)获取键name
cookie.getName();
4)获取值value
cookie.getValue();
5)添加Cookie
// 把Cookie添加到响应对象中,输出给浏览器
response.addCookie(cookie);
6)获取Cookie
// 获取客户端的cookies数组
Cookie[] cookies = request.getCookies();
7)存取中文
// 编码
Cookie cookie1 = new Cookie(“username”, URLEncoder.encode(“abc测试”,“utf8”));
// 解码
String name = URLDecoder.decode(cookie.getValue(),“utf8”);
cookie:一般会保存在本地的用户目录下 appdata;
-
一个Cookie只能保存一个信息
-
一个web站点可以给浏览器发送多个cookie,最多存放20个cookie
-
Cookie大小有限制4kb
-
浏览器上限300个cookie
1)概述
-
服务器会给每一个用户(浏览器)创建一个Seesion对象;
-
一个Seesion独占一个浏览器,只要浏览器没有关闭,这个Session就存在;
-
用户登录之后,整个网站它都可以访问!–> 保存用户的信息;保存购物车的信息…
HttpSession是接口,在包package javax.servlet.http中
package javax.servlet.http;
public interface HttpSession {}
2)Session和cookie的区别
-
Cookie是把用户的数据写给用户的浏览器,浏览器保存 (可以保存多个)
-
Session把用户的数据写到用户独占Session中,服务器端保存 (保存重要的信息,减少服务器资源的浪费)
-
Session对象由服务器创建
3)使用场景
-
保存一个登录用户的信息
-
购物车信息
4)获取Session
HttpSession session = request.getSession();
5)属性Attribute
public Object getAttribute(String name);
public void setAttribute(String name, Object value);
public void removeAttribute(String name);
public Enumeration getAttributeNames();
6)ID
public String getId();
7)isNew
// 判断Session是否是新创建的
public boolean isNew();
8)ServletContext
public ServletContext getServletContext();
9)失效
(1)直接失效
public void invalidate();
(2)设置有效期
- 调用方法
//(单位:秒)
setMaxInactiveInterval(int interval);
- web.xml中配置
5
===============================================================
Java Server Pages : Java服务器端页面,也和Servlet一样,用于动态Web技术!
JSP本质上是一个Servlet
每个JSP文件都对应一个java文件(Servlet),保存在Tomcat服务器的work目录下
如:index.jsp —> index_jsp.java
1)JSP表达式
<%=
开头、%>
结尾
中间是可以直接输出的值、对象…
用来将程序的输出,输出到客户端
<%= new java.util.Date()%>
2)JSP脚本片段
<%
开头、%>
结尾
中间是java代码片段,会被添加到JSP对应的java类的
_jspService()
方法中
<%
int sum = 0;
for (int i = 1; i <=100 ; i++) {
sum+=i;
}
out.println(“
Sum=”+sum+“
”);%>
3)JSP声明
<%!
开头、%>
结尾
中间可以写java代码段,定义属性、方法,会被添加到 JSP 对应的java类中,不是
_jspService()
方法中
<%!
static {
System.out.println(“Loading Servlet!”);
}
private int globalVar = 0;
public void kuang(){
System.out.println(“进入了方法Kuang!”);
}
%>
4)JSP注释
<%--
开头、--%>
结尾
中间写注释内容
JSP的注释,不会在客户端显示,HTML就会!
<%–
JSP注释
不会在客户端显示,HTML就会!
–%>
<%@page args… %>
<%@include file=“” %>
<%@taglib prefix=“” %>
- head.jsp
不用
<%@ page …%>
- foot.jsp
不用
<%@ page …%>
- index.jsp
不用
<%@ page …%>
<%@include file=“head.jsp”%>
…
<%@include file=“foot.jsp” %>
<jsp:include page=“”/>
<jsp:xxx />
使用标签、表达式之前要导入依赖包
Tomcat运行环境中也要有相关jar包
javax.servlet.jsp.jstl
jstl-api
1.2
taglibs
standard
1.1.2
JSTL标签库的使用就是为了弥补HTML标签的不足;自定义有许多标签,功能和Java代码一样!
-
格式化标签
-
SQL标签
-
XML 标签
-
核心标签
<c:out value=“${}”/>
<c:if test=“${}” var=“”>
<c:out value=“”/>
</c:if>
<c:choose>
<c:when test=“${}”>
</c:when>
<c:when test=“${}”>
</c:when>
<c:otherwise>
</c:when>
</c:choose>
<c:forEach var=" " items=“${}”>
</c:forEach>
// 页面上下文
PageContext pageContext;
// 请求
HttpServletRequest request;
// 响应
HttpServletResponse response;
// session
HttpSession session;
// 项目上下文
SerlvetContext application;
// 配置
SerlvetConfigconfig;
// 输出流
JspWriter out;
// 当前页
Objext page = this;
// 异常
Exception exception;
-
作用域Scope
-
page
保存的数据只在当前页面中有效
- request
保存的数据只在一次请求中有效,请求转发会携带这个数据
- session
保存的数据只在一次会话中有效,从打开浏览器到关闭浏览器
- application
保存的数据在服务运行时有效,从打开服务器到关闭服务器
1)概述
JavaBean一般就是指一个普通的实体类(pojo)
-
必须要有一个无参构造器
-
属性必须私有化 private
-
必须有对应的get/set方法
一般用来和数据库的字段做映射 ORM
ORM :对象关系映射
-
表 —> 类
-
字段 —> 属性
-
行记录 —> 对象
2)JSP中用法
// 调用类;当前页有效
<jsp:useBean id=“student” class=“com.tuwer.pojo.Student” scope=“page”/>
// 设置属性值
<jsp:setProperty name=“student” property=“name” value=“小张”/>
// 获取值
<jsp:getProperty name=“student” property=“name”/>
404
/error/404.jsp
500
/error/500.jsp
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns=“http://xmlns.jcp.org/xml/ns/javaee”
xmlns:xsi=“http://www.w3.org/2001/XMLSchema-instance”
xsi:schemaLocation=“http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd”
version=“4.0”
metadata-complete=“true”>
===============================================================
Model View Controller 模型、视图、控制器
======================================================================
Filter接口
package javax.servlet;
public interface Filter {}
功能:数据过滤
-
权限检查
-
中文乱码
-
不良文字、图片…
解决中文乱码
1)导包
导入相关的依赖包
2)定义过滤器类
实现 javax.servlet.Filter 接口
package com.tuwer;
import javax.servlet.*;
import java.io.IOException;
/**
-
@author 土味儿
-
Date 2021/12/13
-
@version 1.0
-
字符过滤器
*/
public class CharacterFilter implements Filter {
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println(“初始化过滤器!”);
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
request.setCharacterEncoding(“utf8”);
response.setCharacterEncoding(“utf8”);
System.out.println(“过滤前…”);
// 传递给下一链
chain.doFilter(request,response);
System.out.println(“过滤后…”);
}
public void destroy() {
System.out.println(“销毁过滤器!”);
}
}
3)在web.xml中配置 Filter
characterFilter
com.tuwer.CharacterFilter
characterFilter
/h/*
登录案例
- 用户访问个人中心页面
* 如果已成功登录,可以访问
* 如果没有登录,不可以访问,跳转到登录页面
- jsp页
index.jsp
<%@ page contentType=“text/html;charset=UTF-8” language=“java” %>
Hello World!
login.jsp
<%@ page contentType=“text/html;charset=UTF-8” language=“java” %>
账号:
登录
myhome.jsp
<%@ page contentType=“text/html;charset=UTF-8” language=“java” %>
欢迎你,<%=session.getAttribute(“username”)%>!
- Servlet
public class LoginServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String username = req.getParameter(“username”);
if(username!=null){
// 添加用户名
req.getSession().setAttribute(“username”,username);
// 重定向至个人中心
resp.sendRedirect(req.getContextPath()+“/user/myhome.jsp”);
}else{
// 重定向至登录页面
resp.sendRedirect(req.getContextPath()+“/login.jsp”);
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
public class LogoutServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
Object username = req.getSession().getAttribute(“username”);
if(username!=null){
// 移除用户名
req.getSession().removeAttribute(“username”);
// 重定向至登录页面
resp.sendRedirect(req.getContextPath()+“/login.jsp”);
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
- Filter
package com.tuwer;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
-
@author 土味儿
-
Date 2021/12/14
-
@version 1.0
*/
public class loginFilter implements Filter {
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println(“启动权限过滤器!”);
}
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
// 强转:HttpServletRequest 可以得到session,ServletRequest 不能得到session
HttpServletRequest req = (HttpServletRequest) request;
HttpServletResponse resp = (HttpServletResponse) response;
Object username = req.getSession().getAttribute(“username”);
// 未登录
if (username == null) {
System.out.println(req.getContextPath());
// 重定向至登录页面
resp.sendRedirect(req.getContextPath()+“/login.jsp”);
}
chain.doFilter(req,resp);
}
public void destroy() {
System.out.println(“销毁权限过滤器!”);
}
}
- web.xml
login
com.tuwer.LoginServlet
login
/login
logout
com.tuwer.LogoutServlet
logout
/logout
loginFilter
com.tuwer.loginFilter
loginFilter
/user/myhome.jsp
- 获取项目根路径
所有的请求链接都建立在根路径基础之上
<%=application.getContextPath()%>
<%=request.getContextPath()%>
<%=session.getServletContext().getContextPath()%>
// java代码
request.getContextPath();
========================================================================
监听某个事件/动作的发生
- 案例:统计在线人员;session的个数就是在线人数(会话);通过监听session的创建和销毁,把数据存入项目上下文application(ServletContext)对象中
实现相应的监听器接口:HttpSessionLintener接口可以监听session的创建和销毁
监听器接口有很多种
package com.tuwer;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
/**
-
@author 土味儿
-
Date 2021/12/14
-
@version 1.0
-
统计在线人数
-
session数量就是在线人数
*/
public class OnlineCountListener implements HttpSessionListener {
/**
-
监听session创建
-
@param se
*/
public void sessionCreated(HttpSessionEvent se) {
// 获取项目上下文对象
ServletContext context = se.getSession().getServletContext();
// 得到在线人数
Integer onlineCount = (Integer) context.getAttribute(“onlineCount”);
if (onlineCount == null) {
// 第一个用户
onlineCount = new Integer(1);
} else {
// 用户数量加1
onlineCount = new Integer(onlineCount.intValue() + 1);
}
// 存入项目上下文对象ServletContext中
context.setAttribute(“onlineCount”, onlineCount);
}
/**
-
监听session销毁
-
@param se
*/
public void sessionDestroyed(HttpSessionEvent se) {
// 获取项目上下文对象
ServletContext context = se.getSession().getServletContext();
// 得到在线人数
Integer onlineCount = (Integer) context.getAttribute(“onlineCount”);
if (onlineCount == null) {
// 第一个用户
onlineCount = new Integer(0);
} else {
// 用户数量减1
onlineCount = new Integer(onlineCount.intValue() - 1);
}
// 存入项目上下文对象ServletContext中
context.setAttribute(“onlineCount”, onlineCount);
}
}
写在最后
作为一名即将求职的程序员,面对一个可能跟近些年非常不同的 2019 年,你的就业机会和风口会出现在哪里?在这种新环境下,工作应该选择大厂还是小公司?已有几年工作经验的老兵,又应该如何保持和提升自身竞争力,转被动为主动?
就目前大环境来看,跳槽成功的难度比往年高很多。一个明显的感受:今年的面试,无论一面还是二面,都很考验Java程序员的技术功底。
最近我整理了一份复习用的面试题及面试高频的考点题及技术点梳理成一份“Java经典面试问题(含答案解析).pdf和一份网上搜集的“Java程序员面试笔试真题库.pdf”(实际上比预期多花了不少精力),包含分布式架构、高可扩展、高性能、高并发、Jvm性能调优、Spring,MyBatis,Nginx源码分析,Redis,ActiveMQ、Mycat、Netty、Kafka、Mysql、Zookeeper、Tomcat、Docker、Dubbo、Nginx等多个知识点高级进阶干货!
由于篇幅有限,为了方便大家观看,这里以图片的形式给大家展示部分的目录和答案截图!
Java经典面试问题(含答案解析)
阿里巴巴技术笔试心得
《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》,点击传送门,即可获取!
// 获取项目上下文对象
ServletContext context = se.getSession().getServletContext();
// 得到在线人数
Integer onlineCount = (Integer) context.getAttribute(“onlineCount”);
if (onlineCount == null) {
// 第一个用户
onlineCount = new Integer(1);
} else {
// 用户数量加1
onlineCount = new Integer(onlineCount.intValue() + 1);
}
// 存入项目上下文对象ServletContext中
context.setAttribute(“onlineCount”, onlineCount);
}
/**
-
监听session销毁
-
@param se
*/
public void sessionDestroyed(HttpSessionEvent se) {
// 获取项目上下文对象
ServletContext context = se.getSession().getServletContext();
// 得到在线人数
Integer onlineCount = (Integer) context.getAttribute(“onlineCount”);
if (onlineCount == null) {
// 第一个用户
onlineCount = new Integer(0);
} else {
// 用户数量减1
onlineCount = new Integer(onlineCount.intValue() - 1);
}
// 存入项目上下文对象ServletContext中
context.setAttribute(“onlineCount”, onlineCount);
}
}
写在最后
作为一名即将求职的程序员,面对一个可能跟近些年非常不同的 2019 年,你的就业机会和风口会出现在哪里?在这种新环境下,工作应该选择大厂还是小公司?已有几年工作经验的老兵,又应该如何保持和提升自身竞争力,转被动为主动?
就目前大环境来看,跳槽成功的难度比往年高很多。一个明显的感受:今年的面试,无论一面还是二面,都很考验Java程序员的技术功底。
最近我整理了一份复习用的面试题及面试高频的考点题及技术点梳理成一份“Java经典面试问题(含答案解析).pdf和一份网上搜集的“Java程序员面试笔试真题库.pdf”(实际上比预期多花了不少精力),包含分布式架构、高可扩展、高性能、高并发、Jvm性能调优、Spring,MyBatis,Nginx源码分析,Redis,ActiveMQ、Mycat、Netty、Kafka、Mysql、Zookeeper、Tomcat、Docker、Dubbo、Nginx等多个知识点高级进阶干货!
由于篇幅有限,为了方便大家观看,这里以图片的形式给大家展示部分的目录和答案截图!
[外链图片转存中…(img-NvsQdoyt-1714675381061)]
Java经典面试问题(含答案解析)
[外链图片转存中…(img-esOhGzKA-1714675381062)]
阿里巴巴技术笔试心得
[外链图片转存中…(img-c59XgMFq-1714675381062)]
《一线大厂Java面试题解析+核心总结学习笔记+最新讲解视频+实战项目源码》,点击传送门,即可获取!