JSP/Servlet
JSP/Servlet:
C/S:客户端与服务器
B/S:浏览器与客户端
- sun公司主推的B/S架构
- 基于java语言的(所有的大公司,或者一些开源的组件都是用java写的)
- 可以承载三高(高并发,高可用,高性能)带来的影响
tomcat默认端口号为8080
mysql:3306
http:80
https:443
网站是如何访问的?
-
输入一个域名然后回车;
-
检查本机的C:\Windows\System32\drivers\etc\hosts配置文件下有没有这个域名映射;
1.有:直接返回对应的ip地址,这个地址中有我们需要访问的web程序,可以直接访问
2.没有:去DNS服务器找,找到的话就返回,找不到就返回找不到
- 可以配置一下环境变量(可选)
网站应该有的结构
--webspps:Tomcat服务器的web目录
-ROOT
-study:网站的目录名
-WEB-INF
-classes:java程序
-libs:web应用所依赖的jar包
-web.xml:网站配置文件
-index.html默认的首页
-static
-css
-style.css
-js
-img
-...
http
http(超文本传输协议)是一个简单的请求-响应协议,它通常运行在TCP之上。
- 文本:html,字符串,~...
- 超文本:图片,音乐,视频
- 80
https:安全的
- 443
两个时代
http1.0
- HTTP/1.0:客户端与服务器连接后,只能获取一个web资源,断开连接。
http2.0
- HTTP/1.1:客户端可以与web服务器连接后,可以获得多个web资源。
http请求
- 客户端---发送请求(request)---服务器
百度:
Request URL: https://www.baidu.com/ 请求地址
Request Method: GET get方法/post方法
Status Code: 200 OK 状态码:200
Remote(远程) Address: 14.215.177.39:443
Accept: text/html
Accept-Encoding: gzip, deflate, br
Accept-Language: zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6 语言
Cache-Control: max-age=0
Connection: keep-alive
1、请求行
- 请求行中的请求方式:GET
- 请求方式:Get,Post,HEAD,DELETE,TRACT...
- get:请求可以携带的参数比较少,大小有限制,会在浏览器的URL地址栏显示数据内容,不安全但是高效
- post:请求可以携带的参数没有限制,大小没有限制,不会在浏览器的URL地址栏显示数据内容,安全但是不高效
2、消息头
Accept: 告诉浏览器,他所支持的数据类型
Accept-Encoding: 支持哪种编码格式
Accept-Language: 告诉浏览器它的语言环境
Cache-Control: 缓存控制
Connection: 告诉浏览器,请求完成是断开还是保持连接
HOST:主机
http响应
- 服务器---响应(response)---客户端
Cache-Control: private 缓存控制
Connection: keep-alive 连接
Content-Encoding: gzip 编码
Content-Type: text/html;charset=utf-8 类型
1、响应体
Accept: 告诉浏览器,他所支持的数据类型
Accept-Encoding: 支持哪种编码格式
Accept-Language: 告诉浏览器它的语言环境
Cache-Control: 缓存控制
Connection: 告诉浏览器,请求完成是断开还是保持连接
HOST:主机
Reflesh:告诉浏览器,多久刷新一次
location:让网页重新定位
2、响应状态码
200:请求响应成功
3xx:请求重定向
4xx:找不到资源· 404
- 资源不存在
5xx:服务器代码错误 500 502:网关错误
在浏览器地址栏输入地址并回车的一瞬间到页面展示回来,经历了什么?
package:项目的打包方式
jar:java应用 war:javaweb应用
ctrl+o:实现要重写的方法,或者alt+ins
在继承HttpServlet时,为什么只重写doGet和doPost?
对于service方法,一般来说这个方法是不需要重写的,因为在HttpServlet中已经有了很好的实现,它会根据请求的方式,调用doGet,doPos以及其他的doXxx方法,也就是说service是用来转向的,所以我们一般写一个servlet,只需要重写doGet或者doPost就可以了。
ServletContext:
web容器在启动的时候,它会为每个web程序都创建一个对应的ServletContext对象,它代表了当前的web应用
一、可以共享数据
public class HelloServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// this.getInitParameter(); //初始化参数
// this.getServletConfig(); //Servlet 配置
// this.getServletContext(); //Servlet上下文
ServletContext context = this.getServletContext();
String username = "study";
context.setAttribute("username",username);
//将一个数据保存在了ServletContext中,名字为username,值为username
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
public class GetServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//设置编码(防止浏览器页面乱码)
resp.setContentType("text/html");
resp.setCharacterEncoding("utf-8");
ServletContext context = this.getServletContext();
String username = (String) context.getAttribute("username");
PrintWriter writer = resp.getWriter();
writer.print("名字为:"+username);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
web.xml配置文件:
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>com.xx.servelt.GetServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
二、获取初始化参数
<!-- 配置一些web应用初始化参数-->
<context-param>
<param-name>url</param-name>
<param-value>jdbc:mysql://localhost:3306/mybatis</param-value>
</context-param>
ServletContext context = this.getServletContext();
String url = context.getInitParameter("url");
resp.getWriter().print(url);
三、请求转发
context.getRequestDispatcher("/hello").forward(req,resp);
四、读取资源文件
InputStream resource = this.getServletContext().getResourceAsStream("/web/aa.properties");
Properties prop = new Properties();
prop.load(resource);
String username = prop.getProperty("username");
String password = prop.getProperty("password");
resp.getWriter().print(username+"---"+password);
response下载文件
//获取文件路径
//String realPath = this.getServletContext().getRealPath("/1.png");
String realPath = "F:\\develop\\idea\\idea_workspace\\javaweb_test2\\demo1\\web\\1.png";
//获取文件名
String fileName = realPath.substring(realPath.lastIndexOf("\\") + 1);//常用
//system.out.println(fileName);
//让浏览器支持我们下载的东西
resp.setHeader("Content-Disposition" ,"attachment;filename="+ URLEncoder.encode(fileName,"utf-8"));
//获取输入流
FileInputStream in = new FileInputStream(realPath);
//创建缓冲区
byte[] buffer = new byte[1024];
//获取OutputStream对象
ServletOutputStream out = resp.getOutputStream();
//将fileOutputStream流写入到buffer缓冲区
int len = 0;
while ((len=in.read(buffer))!=-1){
//使用OutputStream将缓冲区中的数据输出到客户端
out.write(len);
}
重定向
/*重定向
resp.setHeader("Location","/demo1/hi");
resp.setStatus(302);*/
resp.sendRedirect("/demo1/hi");
请求转发
String username = req.getParameter("username");//获取单个属性
String[] habbys = req.getParameterValues("habbys");//获取复选框等
req.getRequestDispatcher("success.jsp").forward(req,resp);
重定向和请求转发区别?
相同点:都可以实现页面跳转
不同点:
-
重定向时,url地址栏会发生变化;302
-
请求转发,地址栏不会发生变化。307

浙公网安备 33010602011771号