概述

该技术已过时

jsp 已经过时,是因为代码杂糅在一起,没有做到完美的分离.不利于后期的维护

java bean 过时的原因在于面条代码的存在,即,各种代码杂乱的糅合在一起.

特点

组件式编程
步骤封装成为组件,需要那个的时候,直接调用组件即可.增加复用性

标准目录结构

WEB-INF

该目录为web目录中最安全的文件夹.保存各种类,第三方jar包,配置文件.

WEB-XML

web部署的描述符

classes

保存所有hacabean

lib

第上方jar包

tags

保存所有标签文件

jsp

保存jsp文件

css

保存样式表文件

images

保存图片文件

page导入JavaBean

package demo;

public class demo {
    public String name;
    public String age;
    public demo(){

    }
    public void setName(String name){
        this.name = name;
    }
    public void setAge(String age){
        this.age = age;
    }
    public String getName(){
        return this.name;
    }
    public String getAge(){
        return this.age;
    }
}

<%--
  Created by IntelliJ IDEA.
  User: ming
  Date: 19-3-14
  Time: 下午9:19
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="demo.demo" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
    demo demo = new demo();
    demo.setAge("12");
    demo.setName("ming");
%>
<%=demo.getName()%>
<%=demo.getAge()%>
</body>
</html>

JavaBean与表单

同jsp
直接传参即可

jsp:useBean指令

使用jsp:useBean指令可以直接调用类

<%--
  Created by IntelliJ IDEA.
  User: ming
  Date: 19-3-14
  Time: 下午9:19
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<jsp:useBean id="demo" scope="page" class="demo.demo"/>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
    demo.setName("ming");
    demo.setAge("12");
%>
<%=demo.getAge()%>
<%=demo.getName()%>
</body>
</html>

JavaBean范围

使用scope属性标记范围
当为post的时候,表示为只在本页生效.
当为request的时候,表示在一次页面跳转,不会重新实例化对象
当为session,表示对象的生命周期为整个会话
当为application的时候,表示为所有用户共同拥有这个对象.即为单例模式

栗子

注册验证

<%--
  Created by IntelliJ IDEA.
  User: ming
  Date: 19-3-15
  Time: 上午12:53
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>用户注册页</title>
</head>
<body>
<jsp:useBean id="reg" scope="request" class="com.ming.Register"/>
<form action="./check.jsp" method="post">
    用户名<input type="text" name="name" value="<jsp:getProperty name="reg" property="name"/>"/>
    <%=reg.getErrorMsg("errname")%>
    年龄<input type="text" name="age" value="<jsp:getProperty name="reg" property="age"/>"/>
    <%=reg.getErrorMsg("errage")%>
    E-Mail<input type="text" name="email" value="<jsp:getProperty name="reg" property="email"/>"/>
    <%=reg.getErrorMsg("erremail")%>
    <input type="submit" value="注册"/>
    <input type="reset" value="重置"/>
</form>
</body>
</html>

<%--
  Created by IntelliJ IDEA.
  User: ming
  Date: 19-3-15
  Time: 上午2:04
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<jsp:useBean id="reg" scope="request" class="com.ming.Register"/>
用户名 <jsp:getProperty name="reg" property="name"/>
年龄  <jsp:getProperty name="reg" property="age"/>
E-Mail <jsp:getProperty name="reg" property="email"/>
</body>
</html>

<%--
  Created by IntelliJ IDEA.
  User: ming
  Date: 19-3-15
  Time: 上午12:55
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>信息验证中</title>
</head>
<body>
<jsp:useBean id="reg" scope="request" class="com.ming.Register"/>
<!-- 属性自动赋值 -->
<jsp:setProperty name="reg" property="*"/>
<%
    // 进行验证
    if(reg.isVaildate()){
%>
        <jsp:forward page="sucess.jsp"/>
<%
    }else{
%>
        <jsp:forward page="index.jsp"/>
<%
    }
%>
</body>
</html>

javaBean如下

package com.ming;

import java.util.HashMap;
import java.util.Map;

public class Register {
    private String name;
    private String age;
    private String email;
    private Map<String, String> errors = null;
    public Register(){
        this.name = "";
        this.age = "";
        this.email = "";
        this.errors = new HashMap<String, String>();
    }
    public boolean isVaildate(){
        boolean flag = true;
        // 验证name合法
        if(!this.name.matches("\\w{6,15}")){
            flag = false;
            this.name = "";
            errors.put("errname", "用户名需要为6-15位字母或数字");
        }
        if(!this.email.matches("\\w+@\\w+\\.\\w+\\.?\\w*")){
            flag = false;
            this.email = "";
            errors.put("erremail", "输入email地址错误");
        }
        if(!this.age.matches("\\d+")){
            flag = false;
            this.age = "";
            errors.put("errage", "年龄只能是数字");
        }
        return flag;
    }
    // 取出错误信息
    public String getErrorMsg(String key){
        String value = this.errors.get(key);
        return value == null ? "" : value;
    }

    public String getName() {
        return name;
    }

    public String getAge() {
        return age;
    }

    public Map<String, String> getErrors() {
        return errors;
    }

    public void setName(String name) {
        this.name = name;
    }

    public void setErrors(Map<String, String> errors) {
        this.errors = errors;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public void setAge(String age) {
        this.age = age;
    }

    public String getEmail() {
        return email;
    }
}