javaWeb部分知识点

1.B/S,C/S
2.HTTP
3.浏览器请求,服务器响应
4.静态 ,动态资源
5.WEB服务器

​ tomcat、weblogic、JBoss

6.框架*(framework):

就是某种应用的半成品,就是一组组件,供你选用完成你自己的系统**.java框架就是一些类和接口的集合,通过这些类和接口协调来完成一系列的程序实现。框架又叫做开发中的半成品,它不能提供整个WEB应用程序的所有东西,但是有了框架,我们就可以集中精力进行业务逻辑的开发而不用去关心它的技术实现以及一些辅助的业务逻辑。我们熟知的StructsSpring就是表示层和业务层框架的代表。

7.url

​ 协议://域名:端口/资源位置?参数=值

8.tomcat及目录结构、端口号(3个)

​ bin、conf、lib、logs、temp、webapps、work

​ webapps:下面5个目录

9.web项目目录结构

​ 项目名:静态资源、WEB-INF

​ WEB-INF:web.xml、lib、classes

10.WEB-INF不能直接被外部访问
11.lib:第三方jar的存放位置
12.classes :java源码编译后生成class文件存放的位置
13.idea创建web项目
new java-勾选web Application-选择SDK-勾选create web.xml-定义项目名称
edit configurations- +tomcatServer(local)-deployment- +Artifact	
在web-inf下-创建lib、classes
将需要的jar放入lib-右键add as library
14.协议、有哪些
通信规则:由谁发起,使用什么语言、怎么结束,这种规则就叫协议
http udp tcp/ip dns ftp
https
http协议特点: 基于请求、响应,默认端口80
15.报文
16.应用层-传输层-网络层-链路层
17.http1.0和1.1版本的区别
1.0:发送请求,创建一次链接,获得一个web资源,链接断开
1.1:发送请求,创建一次链接,获得多个web资源,链接断开
1.1比1.0优化很大,延迟低,速度快
18.表单提交action=“#”的意义:提交到当前
19.get、post区别
get方式:
	请求参数追加在URL后面,不安全
	URL长度限制GET请求方式的数据大小
	没有请求体
	常见get请求:地址栏直接访问、<a href> 、<img src>
post方式:
	请求参数在请求体,安全
	请求数据相对较大
	想要post方式请求必须设置method=post,其他都是get
    
20.常见状态码
200:请求成功
302:请求重定向
304:请求资源没有改变,访问本地缓存
404:请求资源不存在,路径错误或者服务器资源删除
500:服务器内部错误,程序抛异常
21.响应类型设置:content-Type
响应正文的类型(MIME类型)
取值:text/html;charset=UTF-8
22.响应定时刷新Refresh
定时刷新,格式:秒数;url=路径。url可省略,默认值为当前页。
取值:3;url=www.itcast.cn    //三秒刷新页面到www.itcast.cn
23.响应码分析口诀
响应码小口诀
2开头的都ok 
3开头的都是一半
4开头的浏览器端问题
5开头的服务器端问题
24.请求和响应对象
httpservletrequest httpservletresponse
25.通过请求对象获取数据
String name/age/sex=request.getParameter("name/age/sex");单个值
String [] hobbies=request.getPatameterValues("hobby");多个值
Map<String, String[]> parameterMap = request.getParameterMap();直接获取表单所有数据封装到map
26.通过响应对象输出数据
response.getWrite().write("");
27.post提交表单
action=“虚拟目录/url-pattern/?method=fn”
method=“post”
表单内部设置name属性 name=“”
28.get提交表单
action=“虚拟目录/url-pattern/?method=fn&param1=value1&param2=value2”
29.BeanUtils给对象赋值
Beanutils.populate(user,map);
user---->user对象
map---->键值对数据
30.xml存储数据
存:
	XmlUtils.write("这是一个path",user)
取:
	List<User>list=XmlUtils.readAll("这是一个path",User.class)
31.解决请求乱码
  在获取参数之前设置编码:request.setCharacterEncoding("UTF-8");
32.访问项目资源
提示:当前1.html放在web目录下面
浏览器访问:/虚拟路径/1.html
项目中访问:/1.html
33.响应对象实现3秒跳转
response.setHeader(“Refrash”,“3;/虚拟路径/资源文件”)
34.简单的登录案例逻辑
表单输入用户名、密码并点击登录
web.xml中的servlet-mapping中的url-pattern拦截请求(和前台交互)
通过servlet 中的servlet-class的全路径找到类(和后台交互)
进入登录方法,实现逻辑
	获取所有用户xmlUtils.readAll
	判断当前用户是否存在:用户名、密码都相等
	存在:跳转首页:return “/index.html”;
	不存在:跳转注册页面
35.javaweb三大组件
servlet filter(否喽特) listener()
36.servlet的任务
获取请求,处理请求,完成响应
37.作用域
class servletDemo{
    int j=2;
    void fn(){
        int i=1;
        getServletContext().setAttribute("k",3);
    }
}
//i---->调用 fn方法是可以访问
//j---->今天当前servlet类,可以访问
//k---->所有的servlet均可以访问
38.验证码
1.创建验证码对象
	verify=new VerifyCode();
2.获取图片--->用于展示到页面
	BufferedImage image=verify.getImage();
3.将图片写到页面--->输出流
	verify.output(image,getResponse().getOutputStream())
4.获取文本--->用于和用户输入的进行校验,放入域中
	String text=verify.getText();
5.文本放入
	getServletContext.setAttribute("textcode",text);//在用户登录servlet类中校验

39.获取登录人数案例
在登录校验成功之后
1.获取全局域对象context
2.判断是否为空if(context.getAttribute("count")==null);
3.不为空则重新赋值context.setAttribute("count",++context.getAttribute("count"));
4.为空则赋值context.setAttribute("count",1);
40.判断验证码 -忽略大小写
A.equalsIgnoreCase(B)
41.用户登录与验证码逻辑
用户进入登录界面
页面打开同时发送请求--->获取验证码
	获取验证码对象
	通过验证码对象获取图片,通过流响应到页面
	通过验证码对象获取图片文本,放入全局域
用户输入用户名、密码、验证码,点击登录--->发送请求
请求通过url-pattern 、servlet-class、进入登录方法
验证码校验
	不通过:响应提示并返回登录页面
	通过:校验用户
用户名和密码和库中用户名、密码校验
	不存在:响应提示并跳转注册或者登录界面
	存在:进入首页
42.文件下载
 public void download() throws IOException {
        //1.修改响应头,让浏览器识别成需要使用下载的方式
        getResponse().setHeader("Content-Disposition", "attachment;filename = 1.jsp");
        //2.将图片转成字节数组
        byte[] bytes = FileUtils.readFileToByteArray(new File(getServletContext().getRealPath("/WEB-INF/san.png")));
        //3.使用输出流输出字节
        getResponse().getOutputStream().write(bytes);
        return;
    }
43.随机生成36位字符串作为id
UUID.randomUUID().toString();
44.servlet生命周期
过程描述:
1.第一次访问servlet,创建一个单例servlet,执行service方法,执行init方法(仅执行一次)
2.然后每一次请求,都会创建创建一个新的线程访问service的方法
3.servlet被移除或者关闭服务器,servlet实例被销毁,执行destroy方法
45.js创建数组的方式
第一种:new Array(),new Array(size),new Array
第二种: new Array("","");
第三种:var arr=["",""]
46.js字符串
“”  ’‘ 都是字符串
我想字符串里包含一个子字符串需要用到引号,这种情况:  单双连用
var str ="<a href='www.baidu.com'>"
47.启动被加载
 <load-on-startup>2</load-on-startup>
 添加了这个配置 该servlet在服务器开启的时候就会启动
48.url-pattern 路径的编写

49.全局初始化参数
 <context-param>
    <param-name>username</param-name>
    <param-value>root</param-value>
  </context-param>
  <context-param>
    <param-name>password</param-name>
    <param-value>pwd</param-value>
  </context-param>

//使用ServletContext读取全局初始化参数(在web.xml目录下配置参数)
String username = this.getServletContext().getInitParameter("username");
String password = this.getServletContext().getInitParameter("password");
System.out.println(username+"   "+password);
		
		
//获得所有字段,返回一个泛型集合
Enumeration<String> e = this.getServletContext().getInitParameterNames();
    while(e.hasMoreElements()) {
        String name = e.nextElement();
			String value = this.getServletContext().getInitParameter(name);
			System.out.println(name+"   "+value);
		}
50.当前servlet初始化参数配置
    <init-param>
        <param-name>username</param-name>
        <param-value>moonlit</param-value>
    </init-param>
    
    String username = this.getInitParameter("username");
51.获取资源文件
//获取相对路径中的输入流对象
 		InputStream in = context.getResourceAsStream("/WEB-INF/classes/itcast.properties");
//获取文件绝对路径
 		String path = context.getRealPath("/WEB-INF/classes/itcast.properties");
// 类的加载器用来加载class文件,将class文件加载到内存.
		InputStream is = 类名.class.getClassLoader().getResourceAsStream("db.properties");

52.cookie技术
什么是cookie:将用户的信息记录到客户端浏览器的技术,用户下次访问,浏览器会自动携带cookie的信息来到服务器
api使用:
	创建:new cookie(name ,value);
	发送浏览器:HttpServletResponse.addCookie(cookie)
	接受浏览器cookie:HttpServletRequest.getCookies();
	获取cookie的名字:cookie.getName();
	获取cookie的值:cookie.getValue();
	setMaxAge(int expiry);  以秒为单位的时间,超过了该时间后Cookie会自动销毁. 
	setMaxAge(0),手动删除持久性的Cookie。(前提:path和name必须一致)
判断指定cookie是否存在
	String cookieName="aa";//指定名称的cookie
	Cookie[]cookies=getRequest().getCookies();
	for(Cookie cookie Cookies){
    	if(cookie.getName().equals(aa)){//所有cookie中是否存在
            return cookie;
    	}    
	}
	return null;
记录访问时间案例:
	1.判断是否是第一次登录
		//获取所有cookie,判断指定cookie是否存在
		Cookie[] cookies = this.getRequest().getCookies();
		//2.找到记录上次访问时间的cookie
		Cookie lvtc = CookieUtils.findCookie(cookies, "lastVisitTime");
    2.如果是
    	if(null==lvtc){
    3.响应欢迎访问
    		this.getResponse().getWriter().println("欢迎访问");
    	}else{
    4.获取上次访问时间
    		long time = Long.parseLong(lvtc.getValue());
			Date date = new Date(time);
			this.getResponse().getWriter().println("欢迎访问 您上次访问的时间是:"+date.toLocaleString());
    	}
  	5.更新访问时间
  		getResponse().addCookie(new Cookie("lastVisitTime",""+System.currentTimeMillis))

53.session案例:一次性验证码

session的API使用
1.获取session对象
	this.getRequest().getSession()
2.向session对象中添加数据
	session对象.setAttribute(String name,Object value)
3.从session对象中获取数据
 	session对象.getAttribute(String name)
4.移除session对象中的数据
	session对象.removeAttribute(String name)
一次性验证码案例代码实现:
1.生成验证码
 	public void getImg() throws IOException {

        //创建验证码图像
        VerifyCode verifyCode = new VerifyCode();
        //1.将验证码图片响应给img标签
        //1.1获取验证码图片
        BufferedImage image = verifyCode.getImage();
        //1.2获取输出流字节流
        ServletOutputStream outputStream = getResponse().getOutputStream();
        //1.3响应写出图片
        VerifyCode.output(image,outputStream);
        //2.将验证码文本放到xxx位置,用于VerifyResultServlet做对比
        //2.1获取验证码字母文本
        String verifyCodeText = verifyCode.getText();
        //2.2放入到xx位置
        getSession().setAttribute("verifyCode",verifyCodeText);
        System.out.println("sessionID:"+getSession().getId());
        //在生成验证码之后,10s之间后,验证码需要失效
        //设置session的生存时间只有10s
        //getSession().setMaxInactiveInterval(10);
    }
    
2.用户登录校验

	public void result() throws IOException {
        //获取表单提交的验证码
        String verifyCode1 = getRequest().getParameter("verifyCode");
        //获取xx位置的验证码
        String verifyCode=(String)getSession().getAttribute("verifyCode");
        //在获取了session中的验证码后,需要将验证码从session中删除,完成失效动作
        getSession().removeAttribute("verifyCode");
        //如果获取到的验证码是null,则说明之前的验证码已经被移除,失效了!
        if (verifyCode==null){
            getResponse().getWriter().write("验证码已经失效,请刷新验证码");
            return;
        }
        //3.对比返回结果
        if(verifyCode.equalsIgnoreCase(verifyCode1)){
            //3.1比较成功,返回验证码正确
            getResponse().getWriter().write("验证码正确");
            return;
        }
        //3.2比较失败,返回验证码错误
        getResponse().getWriter().write("验证码错误");
        return;
    }
54.请求转发和重定向区别
1、转发是在服务器端完成的,重定向是在客户端发生的;
2、转发的速度快,重定向速度慢;
3、转发是同一次请求,重定向是两次请求;
4、转发地址栏没有变化,重定向地址栏有变化;
5、转发必须是在同一台服务器下完成,重定向可以在不同的服务器下完成。
55.cookie和session用法和区别
相同点:http协议是无状态的,cookie和session绑定用户(无状态:关闭网页-断开连接,打开网页,重新链接)
cookie:
	存储在浏览器端
	不安全,容易被拦截识别
	浏览器可能会禁用cookie(url参数能解决sid=XXX)
	存储限量4kb
	我们可以轻松访问cookie
session:
	存储在服务器端
	安全,但对服务器存储压力大
	会话方式,不能永久保存(会话:打开多个链接,然后关闭浏览器,,这个过程称为一个会话)
	存储无限量
	不能轻松访问
	通常以来cookie(不安全,所以需要先加密)
	
	再次打开浏览器识别之前用户:
	client  --http Request            --> server
	client  <--http Response+set-cookie-- server
	client  --http Request+cookie	  --> server
	client  <--http Response		  -- server
 	
	
56.URL编码解码
1. 编码 URLEncoder
2. 解码 URLDecoder
		String encode = URLEncoder.encode("男", "utf-8");
		Cookie cookie3 = new Cookie("mysex", encode);

56.表单提交形式
方式一:

<form action="/项目资源路径/servlet映射路径" method="post">
	<input type="hidden" name="method" value="login">
	输入验证码:<input type="text" name="checkCode"><br>
	<input type="submit" value="提交">
</form>

方式二:

<form action="/项目资源路径/servlet映射路径?method=login" method="post">
	输入验证码:<input type="text" name="checkCode"><br>
	<input type="submit" value="提交">
</form>

57.思考:生成验证码案例为什么用session,而不用context,request作用域
将数据存放到ServletContext,多个用户共享一个验证码。
将数据存放到request作用域,多次请求不能共享数据
将数据存放到session作用域,一次会话共享数据
58.三个作用域对比
servletContext:
	一个WEB应用只有一个servletContext对象
	创建:服务器启动
	销毁:服务器关闭
httpSession:
	一次会话,多个请求
	创建:第一次调用getSession()
	销毁:
		1.服务器非正常关闭
		2.session过期,默认30分
		3.调用session的invalidate方法
httpServletRequest:
	一次请求(多次请求:请求转发)
	创建:客户端向服务端发送请求
	销毁:服务器为这次请求做出响应,销毁request
三个作用域的API相同:
	存放数据:setAttribute(name,value)
    获得数据:getAttribute(name)
	删除数据:removeAttribute(name)

59.查询商品案例

代码:

1.数据储备:
    /**
     * 用于初始化xml数据,方便后续操作
     */
    @Test
    public void initData() {

        //创建存储所有商品数据
        List<Product> list = new ArrayList<Product>();
        //向集合中存储每个商品
        list.add(new Product(CommonUtils.uuid(), "小米 4c 标准版", "1299", "products/1/cs10001.jpg", "小米 4c 标准版 全网通 白色 移动联通电信4G手机 双卡双待"));
        list.add(new Product(CommonUtils.uuid(), "华硕(ASUS)X450J", "4399", "products/1/cs10002.jpg", "14英寸笔记本电脑 (i5-4200H 4G 1TB GT940M 2G独显 蓝牙4.0 D刻 Win8.1 黑色)"));
        list.add(new Product(CommonUtils.uuid(), "三星 Galaxy S5 (G9008W) 闪耀白", "1999", "products/1/cs10003.jpg", "5.1英寸全高清炫丽屏,2.5GHz四核处理器,1600万像素"));
        list.add(new Product(CommonUtils.uuid(), "NUU NU5", "1190", "products/1/cs10004.jpg", "NUU NU5 16GB 移动联通双4G智能手机 双卡双待 晒单有礼 晨光金香港品牌 2.5D弧度前后钢化玻璃 随机附赠手机套+钢化贴膜 晒单送移动电源+蓝牙耳机"));
        //将所有数据写入到XML文件中
        XmlUtils.write(PRODUCT_FILE_PATH,list);
    }
    
2.实体类创建

	private String id;
    private String name;
    private String price;
    private String path;
    private String des;
    
3.dao层
	   
     private static final String PRODUCT_FILE_PATH = "D://product.xml";
    /**
     * 获取所有商品信息
     * @return 所有商品的list集合
     */
    public List<Product> findAll() {
        List<Product> products = XmlUtils.readAll(PRODUCT_FILE_PATH, Product.class);
        return products;
    }
    
4.service层

    /**
     * 返回所有商品
     * @return 所有商品对象的集合
     */
    public List<Product> getProducts() {
        //需要操作数据,创建dao对象操作数据
        ProductDao dao = new ProductDao();
        List<Product> all = dao.findAll();
        return all;
    }
    
5.servlet层

    /**
     * 接收访问,查询所有的对象数据,将数据放置到request中,供展示页面使用
     * @return 转发的jsp路径
     */
    public String findAll() {
        //1.创建Service对象,查询所有商品
        ProductService service = new ProductService();
        List<Product> products = service.getProducts();
        //2.将数据放置到request中
        getRequest().setAttribute("products",products);
        //3.转发到list.jsp
        return "forward:list.jsp";
    }
    
6.web.xml

	<servlet>
        <servlet-name>ProductServlet</servlet-name>
        <servlet-class>com.czxy.servlet.ProductServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>ProductServlet</servlet-name>
        <url-pattern>/ProductServlet</url-pattern>
    </servlet-mapping>
  
7.jsp页面

	<table border="1" width="100%">
        <tr>
            <th>商品序号</th>
            <th>商品名称</th>
            <th>商品图片</th>
            <th>商品价格</th>
            <th>商品描述</th>
        </tr>
    <%
        //从request中获取所有商品数据
        List<Product> products = (List<Product>) request.getAttribute("products");
        //遍历所有商品数据,依次获取每一个商品
        for (int i=0; i<products.size(); i++) {
            //每一个商品
            Product thisPro = products.get(i);
            //将每一个商品拼写出一个tr,输出到网页中
    %>
    <tr>
        <td><%=i+1%></td>
        <td hidden="hidden"><%=thisPro.getId()%></td>
        <td><%=thisPro.getName()%></td>
        <td><img src="<%=thisPro.getPath()%>" alt=""></td>
        <td><%=thisPro.getPrice()%></td>
        <td><%=thisPro.getDes()%></td>
    </tr>
    <%
        }
    %>
60.jsp语法
1.“<%!”和“%>”是用来定义成员变量属性和方法的
2.“<%” 和“%>”主要是用来输出内容的
3.“<%=”和“%>”标记之间插入的是表达式,不能插入语句。
	“<%=”是一个完整的符号,“<%”和“=”之间不能有空格。
	 JSP表达式中的变量或表达式后面不能有分号(;)。

注释:
	<%-- 注释信息 --%>
	注意:jsp编译后,注释部分消失
include:
	<%@ include file="被包含的文件地址"%>
	包含,引入被包含文件的元素,转成响应java源代码
9个隐式内置对象
	out			javax.servlet.jsp.JspWriter				用于页面输出
    request		javax.servlet.http.HttpServletRequest	得到用户请求信息,
    response	javax.servlet.http.HttpServletResponse	服务器向客户端的回应信息
    config		javax.servlet.ServletConfig				服务器配置,可以取得初始化参数
    session		javax.servlet.http.HttpSession			用来保存用户的信息
    application	javax.servlet.ServletContext			所有用户的共享信息
    page		java.lang.Object						指当前页面转换后的Servlet类的实例
    pageContext	javax.servlet.jsp.PageContext			JSP的页面容器
    exception	java.lang.Throwable						表示JSP页面所发生的异常,在错误页中才起作用
out  与 httpServletResponse.getWriter() 区别:
	相同点:都是用来向客户端发送文本形式的实体内容
	不同点:
			1. out 对象为jspWriter
			2. 直到整个JSP页面结束,out对象中输入缓冲区的数据才真正写入到Serlvet引擎提供的缓冲区中,
			   而response.getWriter().println语句则是直接把内容写入Servlet引擎提供的缓冲区中,
			   Servlet引擎按照缓冲区中的数据存放顺序输出内容。
pageContext
	特点:
		1.jsp中,pageContext可以获取其他8个隐式对象,代表当前jsp的运行环境
		2.获取其他对象的方法: getAbc()
		3.操作属性:
			setAttribute(name,value,scope) 
        	getAttribute(name,scope)
        	removeAttribute(name)
        	findAttribute(name) 
        4.对象的作用范围
            PageContext.PAGE_SCOPE:表示页面范围
            PageContext.REQUEST_SCOPE:表示请求范围
            PageContext.SESSION_SCOPE:表示会话范围
            PageContext.APPLICATION_SCOPE:表示Web应用程序范围

版权声明:本文为作者原创教程,转载请附上教程出处!

猜你喜欢

转载自blog.csdn.net/qq_43266294/article/details/89070088