Java. web
web开发:
web:网页的意思 www.baidu.com
静态web
html. Css
提供给所有人看的数据始终不会发生变化!
动态web
淘宝等,几乎是所有的网站!
提供给所有人看的数据始终会发生变化,每个人在不同的时间,不同的地点看到的信息各不相同!
技术栈:Servlet/JSP,ASP,PHP
在java中,动态web资源开发的技术统称为JavaWeb
web应用程序:可以提供浏览器访问的程序;
a.html,b.html.....多个web资源,这些web资源可以被外界访问,对外界提供服务;
你们能访问到的任何一个页面或者资源,都存在于这个世界的某一个角落的计算机上;
URL
这个统一的web资源会被放在同一个文件夹下,web应用程序,依赖于Tomcat:服务器;
一个web应用由多部分组成:静态web,动态web;
Html, css,js
Jsp,servlet
java程序
jar包
配置文件(properties)
web应用程序编写完毕后,若想提供给外界访问,需要一个服务器来统一管理;
*.htm , *.html 这些都是网页的后缀,如果服务器上一直存在这些东西,我们就可以直接进行读取,(网络);
静态web存在的缺点:
web页面无法动态更新,所有用户看到的都是同一个页面
轮播图,点击特效:伪动态
javaScript(实际开发中用的多)
VBScript
它无法和数据库交互(数据无法持久化,用户无法交互)
页面会动态展示:“web展示的效果因人而异!”
缺点:
假如服务器的动态web资源出现了错误,我们需要重新编写我们的 后台程序 ,重新发布;
重新发布=停机维护
优点:
web页面可以动态更新,所有用户看到的都不是同一个页面
它可以和数据库交互(数据可以持久化:注册,商品信息可以存在数据库...,用户可以交互)
ASP:
微软:国内最早流行的就是ASP
在html中嵌入了VB的脚本:ASP+DOM;
在ASP开发中,基本一个页面都有几千行的业务代码,页面极其混乱(html和java写在一起)
维护成本高!
C#
IIS
PHP
PHP开发速度很快,功能很强大,跨平台,代码很简单(70%,WP)
无法承载大访问量的情况(局限性)
JSP/Servlet:
B/S:浏览器和服务器
C/S:客户端和服务器
sun公司主推的B/S架构
基于java语言的(所有的大公司,或者一些开源的组件,都是用Java写的)
可以承载三高问题带来的影响;
语法像ASP,方便ASP转JSP,加强市场强度;
服务器是一种被动的操作,用来处理用户的请求和给用户一些响应信息;
iis:
微软的;ASP,,windows中自带
Tomcat:
Tomcat是Apache 软件基金会(Apache Software Foundation)的Jakarta 项目中的一个核心项目,最新的Servlet 和JSP 规范总是能在Tomcat 中得到体现因为Tomcat 技术先进、性能稳定,而且 免费 ,因而深受Java 爱好者的喜爱并得到了部分软件开发商的认可,成为目前比较流行的Web 应用服务器。
Tomcat 服务器是一个免费的开放源代码的Web 应用服务器,属于轻量级应用 服务器 ,在中小型系统和并发访问用户不是很多的场合下被普遍使用,是开发和调试JSP 程序的首选。对于一个初学者来说,他是最佳的选择。
而Tomcat 实际上运行JSP 页面和Servlet。目前Tomcat最新版本为 9.0.30。
https://tomcat.apache.org
启动Tomcat: /Users/mac/Library/tomcat/bin/startup.sh
关闭Tomcat: /Users/mac/Library/tomcat/bin/shutdown.sh
可能遇到的问题:
java环境变量没有配置
闪退问题:
乱码问题:
可以配置启动的端口号;
tomcat的默认端口号:8080
mysql:3306
http:80
https:443
可以配置主机的名称:
默认的主机名为:localhost-->127.0.0.1
默认网站应用存放的位置为:webapps
请你谈谈网站是如何进行访问的:
--webapps : Tomcat服务器的web目录
-ROOT
-kuangstudy : 网站的目录名
-WEB-INF
-classes:java程序
-lib : web应用所依赖的jar包
-web.xml:网站的配置文件
-index.html :默认的首页
-static
-css
-style.css
-js
-img
-......
Request URL:https://ss1.bdstatic.com/5eN1bjq8AAUYm2zgoY3K/r/www/cache/static/protocol/https/home/js/nu_instant_search_baaa58d.js //请求地址
Request Method: GET //get方法/post方法
Status Code: 200 (from memory cache) //状态码:200
Remote(远程) Address: 183.201.233.32:443 //
<strong>:authority: dss3.baidu.com</strong>
<strong>:method: GET</strong>
:path: /-rVXeDTa2gU2pMbgoY3K/it/u=701675230,3439008724&fm=202&mola=new&crop=v1
<strong>:scheme: https</strong>
accept: image/webp,image/apng,image/*,*/*;q=0.8
accept-encoding: gzip, deflate, br
accept-language: zh-CN,zh;q=0.9
cookie: BIDUPSID=25FC099492B03BC6D724B99139BE7C00; PSTM=1574690755; BAIDUID=25FC099492B03BC6B769B03C4B7F8EF7:FG=1; delPer=0; H_PS_PSSID=1454_21108_30211_30283_26350_22157; PSINO=1
referer: https://www.baidu.com/s?ie=utf-8&f=8&rsv_bp=1&rsv_idx=1&tn=baidu&wd=java&rsv_pq=a7b71db0000a86e7&rsv_t=d954LlVjnwQ%2F%2BDdfHsZNPH%2FZwioIqTxTYlQFPza0gJLxtdOChA9trFVS%2BbA&rqlang=cn&rsv_enter=1&rsv_dl=tb&rsv_sug3=5&rsv_sug1=3&rsv_sug7=101&rsv_sug2=0&inputT=1681&rsv_sug4=2717
sec-fetch-mode: no-cors
sec-fetch-site: same-site
user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36
get:请求能够携带的参数比较小,大小有限制,会在浏览器的URL地址栏显示数据内容,不安全,但高效。
post:请求能够携带的参数没有限制,大小有没有限制,不会在浏览器的URL地址栏显示数据内容,安全,但不高效。
<strong>Accept:告诉浏览器,他所支持的数据类型</strong>
Accept-Encoding:支持哪种编码格式 :GBK UTGF-8 GB2312 ISO8859-1
Accept-language:告诉浏览器,他的语言环境
Cache-Control:缓存控制
<strong>Connection:告诉浏览器,请求完成是断开还是保持连接</strong>
<strong>HOST:主机</strong>
..........
<strong></strong>
cache-control: max-age=315360000 //缓存控制
Connection:keep-Alive //连接
content-encoding: gzip //编码
Content-Type:text/html //类型
<strong>Accept:告诉浏览器,他所支持的数据类型</strong>
Accept-Encoding:支持哪种编码格式 :GBK UTGF-8 GB2312 ISO8859-1
Accept-language:告诉浏览器,他的语言环境
Cache-Control:缓存控制
<strong>Connection:告诉浏览器,请求完成是断开还是保持连接</strong>
<strong>HOST:主机</strong>
..........
<strong>reFresh:告诉客户端,多久刷新一次</strong>
<strong>Location:让网页重新定位</strong>
官网: https://maven.apache.org
//在build中配置resource,来防止资源导出失败的问题
<build>
<finalName>test</finalName>
<!--
这样也可以把所有的xml文件,打包到相应位置。
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
<include>**/*.tld</include>
</includes>
<filtering>false</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.properties</include>
<include>**/*.xml</include>
<include>**/*.tld</include>
</includes>
<filtering>false</filtering>
</resource>
</resources>
</build>
sun公司在这些API中提供了一个接口叫做:Servlet,如果你想开发一个Servlet程序,只需要完成两个小步骤:
构建一个普通的maven项目,删掉里面的src目录,以后我们的学习就在这个项目里面建立Model;这个空的工程就叫做Maven的主工程
<modules>
<module>Servlet-02</module>
</modules>
<strong> <parent>
<artifactId>javaweb-02-Servlet</artifactId>
<groupId>com.yuan</groupId>
<version>1.0-SNAPSHOT</version>
</parent></strong>
<strong>son extends father//类似于java的多态</strong>
<strong>public class HelloServlet extends HttpServlet {
//由于get或者post只是请求实现的不同方式,可以相互调用,业务逻辑都一样
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//ServletOutputStream outputStream = resp.getOutputStream();
PrintWriter writer = resp.getWriter();//响应流
writer.println("Hello Servlet");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
super.doPost(req, resp);
}
}
</strong>
为什么需要映射:我们写的是java程序,但是需要通过浏览器访问,而浏览器需要连接web服务器,所以我们需要在web服务中注册我们写的Servlet,还需要给他一个浏览器能够访问的路径;
<strong><servlet-mapping> <servlet-name>hello</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping> </strong>
<strong><servlet-mapping> <servlet-name>hello</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>hello</servlet-name> <url-pattern>/hello1</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>hello</servlet-name> <url-pattern>/hello2</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>hello</servlet-name> <url-pattern>/hello3</url-pattern> </servlet-mapping> </strong>
<strong><servlet-mapping> <servlet-name>hello</servlet-name> <url-pattern>/hello/*</url-pattern> </servlet-mapping> </strong>
<strong><servlet-mapping> <servlet-name>hello</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> </strong>
<strong><servlet-mapping> <servlet-name>hello</servlet-name> <url-pattern>*.do</url-pattern> //*前面不能加映射的路径"/" </servlet-mapping> </strong>
<strong>//404 <servlet> <servlet-name>error</servlet-name> <url-pattern>com.yuan.servlet.ErrorServlet</url-pattern> //*前面不能加映射的路径"/" </servlet> <servlet-mapping> <servlet-name>error</servlet-name> <url-pattern>/*</url-pattern> //*前面不能加映射的路径"/" </servlet-mapping> </strong>
<strong> 域对象是服务器在内存上创建的存储空间,用于在不同动态资源(servlet)之间传递与共享数据。</strong>
<strong>setAttribute(name,value);//name是String类型,value是Object类型; 往域对象里面添加数据,添加时以key-value形式添加 getAttribute(name); //根据指定的key读取域对象里面的数据 removeAttribute(name); //根据指定的key从域对象里面删除数据</strong>
<strong>/**
* doGet
*/
publicvoid doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//往serlvetContext里面存数据
//1.获取ServletContext对象
//getServletContext()
//2.往对象里面设置数据
getServletContext().setAttribute("username", "admin");
response.getOutputStream().write("用户名写入到servletContext成功".getBytes());
}
————————————————
原文链接:https://blog.csdn.net/qq_36371449/article/details/80314024</strong>
<strong>/**
* doGet
*/
publicvoid doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//获取ServletContext里面的用户名数据
Object valueObject = getServletContext().getAttribute("username");
if(valueObject!=null){
response.getOutputStream().write(("从servletContext读取到的用户名数据:"+valueObject.toString()).getBytes());
}
}
————————————————
原文链接:https://blog.csdn.net/qq_36371449/article/details/80314024</strong>
<strong> 服务器启动的时候创建,服务器关闭的时候销毁,因为这是全局应用程序对象,全局共享对象。</strong>
<strong> getServletContext().getInitParameter(name);//根据指定的参数名获取参数值 getServletContext().getInitParameterNames();//获取所有参数名称列表</strong>
<strong><!-- 全局配置参数,因为不属于任何一个servlet,但是所有的servlet都可以通过servletContext读取这个数据 -->
<context-param>
<param-name>param1</param-name>
<param-value>value1</param-value>
</context-param>
<context-param>
<param-name>param2</param-name>
<param-value>value2</param-value>
</context-param>
————————————————
原文链接:https://blog.csdn.net/qq_36371449/article/details/80314024</strong>
<strong> public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//使用servletContext读取全局配置参数数据
//核心方法
/*getServletContext().getInitParameter(name);//根据指定的参数名获取参数值
getServletContext().getInitParameterNames();//获取所有参数名称列表*/
//打印所有参数
//1.先获取所有全局配置参数名称
Enumeration<String> enumeration = getServletContext().getInitParameterNames();
//2.遍历迭代器
while(enumeration.hasMoreElements()){
//获取每个元素的参数名字
String parameName = enumeration.nextElement();
//根据参数名字获取参数值
String parameValue = getServletContext().getInitParameter(parameName);
//打印
System.out.println(parameName+"="+parameValue);
}
}
————————————————
原文链接:https://blog.csdn.net/qq_36371449/article/details/80314024</strong>
<strong> getServletContext().getRealPath(path)//,根据相对路径获取服务器上资源的绝对路径
getServletContext().getResourceAsStream(path)//,根据相对路径获取服务器上资源的输入字节流</strong>
<strong> getServletContext().getContextPath();
//作用:获取当前工程名字</strong>
<strong>publicvoid doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//获取工程名字,getServletContext().getContextPath()
response.getOutputStream().write(("工程名字:"+getServletContext().getContextPath()).getBytes());
}
原文链接:https://blog.csdn.net/qq_36371449/article/details/80314024</strong>
参考博客: https://www.cnblogs.com/xdp-gacl/p/3789624.html
参考博客: https://www.jianshu.com/p/8bc6b82403c5
<strong>1 package gacl.response.study;
2 import java.io.FileInputStream;
3 import java.io.FileNotFoundException;
4 import java.io.FileReader;
5 import java.io.IOException;
6 import java.io.InputStream;
7 import java.io.OutputStream;
8 import java.io.PrintWriter;
9 import java.net.URLEncoder;
10 import javax.servlet.ServletException;
11 import javax.servlet.http.HttpServlet;
12 import javax.servlet.http.HttpServletRequest;
13 import javax.servlet.http.HttpServletResponse;
14 /**
15 * @author gacl
16 * 文件下载
17 */
18 public class ResponseDemo02 extends HttpServlet {
19
20 public void doGet(HttpServletRequest request, HttpServletResponse response)
21 throws ServletException, IOException {
22 downloadFileByOutputStream(response);//下载文件,通过OutputStream流
23 }
24
25 /**
26 * 下载文件,通过OutputStream流
27 * @param response
28 * @throws FileNotFoundException
29 * @throws IOException
30 */
31 private void downloadFileByOutputStream(HttpServletResponse response)
32 throws FileNotFoundException, IOException {
33 //1.获取要下载的文件的绝对路径
34 String realPath = this.getServletContext().getRealPath("/download/1.JPG");
35 //2.获取要下载的文件名
36 String fileName = realPath.substring(realPath.lastIndexOf("//")+1);
37 //3.设置content-disposition响应头控制浏览器以下载的形式打开文件
38 response.setHeader("content-disposition", "attachment;filename="+fileName);
39 //4.获取要下载的文件输入流
40 InputStream in = new FileInputStream(realPath);
41 int len = 0;
42 //5.创建数据缓冲区
43 byte[] buffer = new byte[1024];
44 //6.通过response对象获取OutputStream流
45 OutputStream out = response.getOutputStream();
46 //7.将FileInputStream流写入到buffer缓冲区
47 while ((len = in.read(buffer)) > 0) {
48 //8.使用OutputStream将缓冲区的数据输出到客户端浏览器
49 out.write(buffer,0,len);
50 }
51 in.close();
52 }
53
54 public void doPost(HttpServletRequest request, HttpServletResponse response)
55 throws ServletException, IOException {
56 doGet(request, response);
57 }
58 }</strong>
<strong>1 package gacl.response.study;
2 import java.io.FileInputStream;
3 import java.io.FileNotFoundException;
4 import java.io.FileReader;
5 import java.io.IOException;
6 import java.io.InputStream;
7 import java.io.OutputStream;
8 import java.io.PrintWriter;
9 import java.net.URLEncoder;
10 import javax.servlet.ServletException;
11 import javax.servlet.http.HttpServlet;
12 import javax.servlet.http.HttpServletRequest;
13 import javax.servlet.http.HttpServletResponse;
14 /**
15 * @author gacl
16 * 文件下载
17 */
18 public class ResponseDemo02 extends HttpServlet {
19
20 public void doGet(HttpServletRequest request, HttpServletResponse response)
21 throws ServletException, IOException {
22 downloadChineseFileByOutputStream(response);//下载中文文件
23 }
24
25 /**
26 * 下载中文文件,中文文件下载时,文件名要经过URL编码,否则会出现文件名乱码
27 * @param response
28 * @throws FileNotFoundException
29 * @throws IOException
30 */
31 private void downloadChineseFileByOutputStream(HttpServletResponse response)
32 throws FileNotFoundException, IOException {
33 String realPath = this.getServletContext().getRealPath("/download/张家界国家森林公园.JPG");//获取要下载的文件的绝对路径
34 String fileName = realPath.substring(realPath.lastIndexOf("//")+1);//获取要下载的文件名
35 //设置content-disposition响应头控制浏览器以下载的形式打开文件,中文文件名要使用URLEncoder.encode方法进行编码,否则会出现文件名乱码
36 response.setHeader("content-disposition", "attachment;filename="+URLEncoder.encode(fileName, "UTF-8"));
37 InputStream in = new FileInputStream(realPath);//获取文件输入流
38 int len = 0;
39 byte[] buffer = new byte[1024];
40 OutputStream out = response.getOutputStream();
41 while ((len = in.read(buffer)) > 0) {
42 out.write(buffer,0,len);//将缓冲区的数据输出到客户端浏览器
43 }
44 in.close();
45 }
46
47 public void doPost(HttpServletRequest request, HttpServletResponse response)
48 throws ServletException, IOException {
49 doGet(request, response);
50 }
51 }
</strong>
<strong>public class ResponseDemo4 extends HttpServlet {
public static final int WIDTH = 120; // 生成的图片的宽度
public static final int HEIGHT = 35; // 生成的图片的高度
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 在内存中创建一张图片
BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
// 得到图片
Graphics g = image.getGraphics();
// 1.设置图片的背景色
setBackGround(g);
// 2.设置图片的边框
setBorder(g);
// 3.在图片上画干扰线
drawRandomLine(g);
// 4.在图片上写随机数
String random = drawRandomNum((Graphics2D) g);
request.getSession().setAttribute("checkcode", random); // 将随机数存在session中
/*
* 5.图形写给浏览器
*/
response.setContentType("image/jpeg")
// 发响应头控制浏览器不要缓存图片
response.setDateHeader("expries", -1);
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Pragma", "no-cache");
ImageIO.write(image, "jpg", response.getOutputStream());
}
/*
* 设置图片的背景色
*/
private void setBackGround(Graphics g) {
// 设置颜色
g.setColor(Color.WHITE);
// 填充区域
g.fillRect(0, 0, WIDTH, HEIGHT);
}
/*
* 设置图片的边框
*/
private void setBorder(Graphics g) {
// 设置边框颜色
g.setColor(Color.BLUE);
// 边框区域
g.drawRect(1, 1, WIDTH-2, HEIGHT-2);
}
/*
* 在图片上画干扰线
*/
private void drawRandomLine(Graphics g) {
// 设置颜色
g.setColor(Color.GREEN);
// 设置线条个数并画线
for(int i=0;i<5;i++) {
// 生成干扰线随机的起始坐标
int x1 = new Random().nextInt(WIDTH); // 生成0~WIDTH(不包括WIDTH)的随机数
int y1 = new Random().nextInt(HEIGHT);
// 生成干扰线随机的结束坐标
int x2 = new Random().nextInt(WIDTH);
int y2 = new Random().nextInt(HEIGHT);
g.drawLine(x1, y1, x2, y2);
}
}
/*
* 在图片上写随机数
*/
private String drawRandomNum(Graphics2D g) {
// 设置颜色
g.setColor(Color.RED);
// 设置字体
g.setFont(new Font("宋体", Font.BOLD, 20));
// 常用的中国汉字(汉字区间:[/u4e00-/u9fa5])
String base = "/u7684/u4e00/u4e86/u662f/u6211/u4e0d/u5728/u4eba/u4eec/u6709/u6765/u4ed6/u8fd9/u4e0a/u7740/u4e2a/u5730/u5230/u5927/u91cc/u8bf4/u5c31/u53bb/u5b50/u5f97/u4e5f/u548c/u90a3/u8981/u4e0b/u770b/u5929/u65f6/u8fc7/u51fa/u5c0f/u4e48/u8d77/u4f60/u90fd/u628a/u597d/u8fd8/u591a/u6ca1/u4e3a/u53c8/u53ef/u5bb6/u5b66/u53ea/u4ee5/u4e3b/u4f1a/u6837/u5e74/u60f3/u751f/u540c/u8001/u4e2d/u5341/u4ece/u81ea/u9762/u524d/u5934/u9053/u5b83/u540e/u7136/u8d70/u5f88/u50cf/u89c1/u4e24/u7528/u5979/u56fd/u52a8/u8fdb/u6210/u56de/u4ec0/u8fb9/u4f5c/u5bf9/u5f00/u800c/u5df1/u4e9b/u73b0/u5c71/u6c11/u5019/u7ecf/u53d1/u5de5/u5411/u4e8b/u547d/u7ed9/u957f/u6c34/u51e0/u4e49/u4e09/u58f0/u4e8e/u9ad8/u624b/u77e5/u7406/u773c/u5fd7/u70b9/u5fc3/u6218/u4e8c/u95ee/u4f46/u8eab/u65b9/u5b9e/u5403/u505a/u53eb/u5f53/u4f4f/u542c/u9769/u6253/u5462/u771f/u5168/u624d/u56db/u5df2/u6240/u654c/u4e4b/u6700/u5149/u4ea7/u60c5/u8def/u5206/u603b/u6761/u767d/u8bdd/u4e1c/u5e2d/u6b21/u4eb2/u5982/u88ab/u82b1/u53e3/u653e/u513f/u5e38/u6c14/u4e94/u7b2c/u4f7f/u5199/u519b/u5427/u6587/u8fd0/u518d/u679c/u600e/u5b9a/u8bb8/u5feb/u660e/u884c/u56e0/u522b/u98de/u5916/u6811/u7269/u6d3b/u90e8/u95e8/u65e0/u5f80/u8239/u671b/u65b0/u5e26/u961f/u5148/u529b/u5b8c/u5374/u7ad9/u4ee3/u5458/u673a/u66f4/u4e5d/u60a8/u6bcf/u98ce/u7ea7/u8ddf/u7b11/u554a/u5b69/u4e07/u5c11/u76f4/u610f/u591c/u6bd4/u9636/u8fde/u8f66/u91cd/u4fbf/u6597/u9a6c/u54ea/u5316/u592a/u6307/u53d8/u793e/u4f3c/u58eb/u8005/u5e72/u77f3/u6ee1/u65e5/u51b3/u767e/u539f/u62ff/u7fa4/u7a76/u5404/u516d/u672c/u601d/u89e3/u7acb/u6cb3/u6751/u516b/u96be/u65e9/u8bba/u5417/u6839/u5171/u8ba9/u76f8/u7814/u4eca/u5176/u4e66/u5750/u63a5/u5e94/u5173/u4fe1/u89c9/u6b65/u53cd/u5904/u8bb0/u5c06/u5343/u627e/u4e89/u9886/u6216/u5e08/u7ed3/u5757/u8dd1/u8c01/u8349/u8d8a/u5b57/u52a0/u811a/u7d27/u7231/u7b49/u4e60/u9635/u6015/u6708/u9752/u534a/u706b/u6cd5/u9898/u5efa/u8d76/u4f4d/u5531/u6d77/u4e03/u5973/u4efb/u4ef6/u611f/u51c6/u5f20/u56e2/u5c4b/u79bb/u8272/u8138/u7247/u79d1/u5012/u775b/u5229/u4e16/u521a/u4e14/u7531/u9001/u5207/u661f/u5bfc/u665a/u8868/u591f/u6574/u8ba4/u54cd/u96ea/u6d41/u672a/u573a/u8be5/u5e76/u5e95/u6df1/u523b/u5e73/u4f1f/u5fd9/u63d0/u786e/u8fd1/u4eae/u8f7b/u8bb2/u519c/u53e4/u9ed1/u544a/u754c/u62c9/u540d/u5440/u571f/u6e05/u9633/u7167/u529e/u53f2/u6539/u5386/u8f6c/u753b/u9020/u5634/u6b64/u6cbb/u5317/u5fc5/u670d/u96e8/u7a7f/u5185/u8bc6/u9a8c/u4f20/u4e1a/u83dc/u722c/u7761/u5174/u5f62/u91cf/u54b1/u89c2/u82e6/u4f53/u4f17/u901a/u51b2/u5408/u7834/u53cb/u5ea6/u672f/u996d/u516c/u65c1/u623f/u6781/u5357/u67aa/u8bfb/u6c99/u5c81/u7ebf/u91ce/u575a/u7a7a/u6536/u7b97/u81f3/u653f/u57ce/u52b3/u843d/u94b1/u7279/u56f4/u5f1f/u80dc/u6559/u70ed/u5c55/u5305/u6b4c/u7c7b/u6e10/u5f3a/u6570/u4e61/u547c/u6027/u97f3/u7b54/u54e5/u9645/u65e7/u795e/u5ea7/u7ae0/u5e2e/u5566/u53d7/u7cfb/u4ee4/u8df3/u975e/u4f55/u725b/u53d6/u5165/u5cb8/u6562/u6389/u5ffd/u79cd/u88c5/u9876/u6025/u6797/u505c/u606f/u53e5/u533a/u8863/u822c/u62a5/u53f6/u538b/u6162/u53d4/u80cc/u7ec6";
StringBuffer sb = new StringBuffer();
int x = 5;
// 控制字数
for(int i=0;i<4;i++) {
int degree = new Random().nextInt()%30; //生成-30-30范围的随机数
String ch = base.charAt(new Random().nextInt(base.length()))+"";
sb.append(ch);
// 写入字之前,设置好旋转
g.rotate(degree*Math.PI/180, x, 20); // 设置字体旋转角度
g.drawString(ch, x, 20);
// 这次旋转不能影响下一次的旋转,所以要将上一次的旋转清掉,转回去
g.rotate(-degree*Math.PI/180, x, 20);
x+=30;
}
return sb.toString();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}</strong>
<strong>response.setHeader("refresh", "5");//设置refresh响应头控制浏览器每隔5秒钟刷新一次</strong>
<strong>response.setDateHeader("expries", -1);
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Pragma", "no-cache");</strong>
参考博客: https://www.iteye.com/blog/sihailoveyan-2430052
<strong>Cookie[] cookies = req.getCookies();//获得cookie
cookie.getName()//获得cookie的key
cookie.getValue()//获得cookie中的值
new Cookie("lastLoginTime",System.currentTimeMillis()+"");//新建一个cookie
cookie.setMaxAge(24*60*60);//设置cookie的有效期
resp.addCookie(cookie1);//响应给客户端一个cookie
</strong>
Session是把用户的数据写到用户独占Session中,服务端保存(保存重要的信息,减少服务器资源的浪费)
<strong>SessionId: <%= session.getId() %>
<br/><br/>
isCreateNew:<%= session.isNew() %>
<br/><br/>
maxInActiveInterval:<%= session.getMaxInactiveInterval() %>
<br/><br/>
CreatedTime:<%= session.getCreationTime() %>
<br/><br/>
lastAccessedTime: <%= session.getLastAccessedTime() %>
<br/><br/>
<%
Object username = session.getAttribute("username");
if(username == null){
username = "";
}
%>
<form action="<%= response.encodeURL("loginSuccess.jsp") %>" method="post">
username: <input type="text" name="username" value="<%= username%>"/><br/>
<input type="submit" value="登录"/>
</form></strong>
<strong>SessionId: <%= session.getId() %>
<br/><br/>
isCreateNew:<%= session.isNew() %>
<br/><br/>
maxInActiveInterval:<%= session.getMaxInactiveInterval() %>
<br/><br/>
CreatedTime:<%= session.getCreationTime() %>
<br/><br/>
lastAccessedTime: <%= session.getLastAccessedTime() %>
<br/><br/>
Hello: <%= request.getParameter("username") %>
<br/><br/>
<%
//将username存储于session之中,便于在整个会话过程中记住当前user;
// 当然服务器端可以找到session还是通过cookie(URL重写)在客户端和服务器端传递JSESSIONID
session.setAttribute("username", request.getParameter("username"));
%>
<!--
cookie被禁用的情况下使用url重写的方式传递JSSESSIONID;
重写使用:response.encodeURL或response.encodeRedirectURL()都行
-->
<a href="<%= response.encodeURL("login.jsp") %>">重新登录</a>
<a href="<%= response.encodeURL("loginOut.jsp") %>">注销</a>
</strong>
<strong>SessionId: <%= session.getId() %>
<br/><br/>
isCreateNew:<%= session.isNew() %>
<br/><br/>
maxInActiveInterval:<%= session.getMaxInactiveInterval() %>
<br/><br/>
CreatedTime:<%= session.getCreationTime() %>
<br/><br/>
lastAccessedTime: <%= session.getLastAccessedTime() %>
<br/><br/>
ByeBye: <%= session.getAttribute("username") %>
<br/><br/>
<%
//注销session
session.invalidate();
%>
<a href="login.jsp">重新登录</a>
</strong>
<strong> 2、web容器将jsp转化为servlet代码(.java)
3、web容器将转化为servlet代码编译(.class)
4、web容器加载编译后的代码并执行
5、将执行结果响应给客户端</strong>
<strong>out.write("<html>/r/n");</strong>
<strong><%-- JSP表达式
作用:用来将程序的输出到客户端
<%= 变量或者表达式%>--%>
<%= new java.util.Date()%>
</strong>
<strong><%-- jsp脚本片段--%>
<%
int sum = 0;
for (int i = 0; i <= 100; i++) {
sum+=i;
}
out.println("<h1>"+"Sum="+sum+"<h1>");
%>
</strong>
<strong><%
int x = 10;
out.println(x);
%>
<p>这是一个jsp文档</p>
<%
int y = 20;
out.println(y);
%>
<hr>
<%-- 在代码中嵌入html元素 --%>
<%
for (int i = 0; i < 5; i++) {
%>
<h1>Hello,World<%=i%></h1>
<%
}
%></strong>
<strong><%!
static {
System.out.println("Loading Servlet!");
}
private int globalVar = 0;
public void yuan(){
System.out.println("进入了方法yuan!");
}
%></strong>
<strong><% %> <%= %> <%! %> <%----%></strong>
| <%@ page ... %> | 定义网页依赖属性,比如脚本语言、error页面、缓存需求等等 |
|---|---|
| <%@ include ... %> | 包含其他文件 |
| <%@ taglib ... %> | 引入标签库的定义 |
参考博客: https://www.runoob.com/jsp/jsp-directives.html
<strong><%
pageContext.setAttribute("name1","智能1号");//保存的数据只在一个页面中有效
request.setAttribute("name2","智能2号");//保存的数据只在一次请求中有效,请求转发会携带这个参数
session.setAttribute("name3","智能3号");//保存的数据只在一次回话中有效,从打开浏览器到关闭浏览器
application.setAttribute("name4","智能4号");//保存的数据只在服务器中有效,从打开服务器到关闭服务器
%></strong>
<strong> <!-- https://mvnrepository.com/artifact/javax.servlet.jsp.jstl/jstl-api -->
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>jstl-api</artifactId>
<version>1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/taglibs/standard -->
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency></strong>
<strong><%--http://localhost:8080/jsptag.jsp?name=yuan&age=12--%>
<h1>1</h1>
<jsp:forward page="/jsptag2.jsp">
<jsp:param name="name" value="yuan"/>
<jsp:param name="age" value="12"/>
</jsp:forward></strong>
参考博客: https://www.runoob.com/jsp/jsp-jstl.html
参考博客: https://www.jianshu.com/p/731d027b2d91
<strong>登录--->接收用户的登录请求---->处理用户的请求(获取用户登录的参数;username,password)---->交给业务层处理登录业务(判断用户名密码是否正确:事务)---->Dao层查询用户名和密码是否正确---->数据库</strong>
<strong>servlet---->CRUB(增删改查)---->数据库 弊端:程序十分臃肿,不利于维护 servlet的代码中:处理请求、响应、视图跳转、处理JDBC、处理业务代码、处理逻辑代码 架构:没有什么是加一层解决不了的!如果不行,再加一层! 程序员调用: JDBC 操作: MySQL、Oracle、SQLServer....</strong>
<strong>package com.yuan.filter;
import javax.servlet.*;
import java.io.IOException;
public class CharacterEncodingFilter implements Filter {
//初始化
public void init(FilterConfig filterConfig) throws ServletException {
System.out.println("CharacterEncodingFilter已经初始化了");
}
// 1.过滤器中的所有代码,在过滤特定请求的时候都会执行
// 2.必须要让过滤器继续执行
// filterChain.doFilter(servletRequest,servletResponse);
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
servletRequest.setCharacterEncoding("utf-8");
servletResponse.setCharacterEncoding("utf-8");
servletResponse.setContentType("text/html;charset=UTF-8");
System.out.println("CharacterEncodingFilter执行前.....");
filterChain.doFilter(servletRequest,servletResponse);//让我们的请求继续走,如果不写,程序到这里就被拦截停止
System.out.println("CharacterEncodingFilter执行后.....");
}
//web服务器关闭的时候,过滤器才会销毁
public void destroy() {
System.out.println("CharacterEncodingFilter已经销毁了");
}
}
</strong>
<strong> <filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>com.yuan.filter.CharacterEncodingFilter</filter-class>
</filter>
<!-- 只要是/Servlet的任何请求,都会经过这个过滤器-->
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/Servlet/*</url-pattern>
</filter-mapping></strong>
<strong> <dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency></strong>
<strong>开启事务 事务提交 commit() 事务回滚 rollback() 关闭事务 转账: A:1000 B:1000 A(900)---100--->B(1100)</strong>
<strong><!--单元测试-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency></strong>
<strong> @Test
public void test(){
System.out.println("Hello World");
}</strong>