Java 之 JSP

jsp原理

访问 jsp 页面时,jsp 页面会被翻译成 .java 文件
.java 文件会被编译成 .class 文件(字节码文件)
例如:在网页中显示当前时间
<%@ page import="java.util.Date"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <%
        // 正常写 Java 代码

        Date date = new Date();
        out.print(date.toLocaleString());
        // 被废弃的方法,使用仅为测试
    %>
</body>
</html>
纯JSP实现登录页面
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <form action="/Demo/dologin.jsp" method="post">
        账号:<input type="text" name="username">
        <br>
        密码:<input type="password" name="password">
        <br>
        <input type="submit" value="登录">
        <br>
        <%
            String msg = (String)request.getAttribute("msg");
            if(msg != null){
                out.print(msg);
            }
        %>
    </form>
</body>
</html>

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <%
        // 获取参数
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        // 处理逻辑
        if(username.equals("test") && password.equals("123")){
            // 登录成功
            // 跳转页面 -> 欢迎页面,并且显示名字
            // 传值时,如果是非表单提交的数据,可以保存在域对象中
            //      如果是表单提交过来的数据,直接使用请求转发就行了
            request.getRequestDispatcher("/success.jsp").forward(request, response);
        } else {
            // 登录失败 返回登录页面
            // 相对于服务器内,不用添加工程名,例如:请求转发
            // 相对于服务器外(相对于网址的8080后),需要添加工程名,例如:请求重定向
            // 失败后,在页面中显示错误信息
            // 使用请求转发,比较合适
            request.setAttribute("msg", "登录失败,请确认用户名和密码!");
            request.getRequestDispatcher("/login.jsp").forward(request, response);
        }
        // 跳转页面
    %>
</body>
</html>

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <%
        response.setContentType("text/html;charset=UTF-8");
        request.setCharacterEncoding("UTF-8");
    %>
    <h1>欢迎你  
        <% 
            // String username = (String)session.getAttribute("username");
            String username = request.getParameter("username"); 
            out.write(username);
        %>
    </h1>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!-- 默认是true,相当与一个开关 -->
<!-- false时,.Java文件编译时没有session这个对象 -->
<%@ page session="true" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <%-- jsp注释:安全效率高 --%>
    <!-- html注释:不安全,效率低 -->


    <!-- 测试声明 -->
    <%!
        // 加上叹号会被翻译到类的下面,即为全局变量
        // 还可以声明方法,静态块
        // 可以这么用,但是一般没人这么去写!
        int num2 = 10;
    %>
    <%
        // 测试session开关
        session.getAttribute("123123");

        int num1 = 10;
        num1++;
        num2++;
        // out.print(num1);
    %>
    <!-- 相当于 out.print(num1); -->
    <!-- 表达式 -->
    <%=num1+2 %>
    <%=num2 %>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page errorPage="/error.jsp" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <%
        int num = 10 / 0;
    %>
</body>
</html>

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!-- 默认是关闭状态的,打开捕获异常信息的对象的创建 Throwable -->
<%@ page isErrorPage="true" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <%
        String msg = exception.getMessage();
        out.print(msg);
    %>
    服务器正在紧张的建设当中...请稍后重试
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!-- include指令  file指包含哪个页面 
     静态包含:在翻译成.Java文件前就已经合成了一个页面
     动态包含:
    代码逐行执行,当执行到动态包含时
    才会去编译被包含的页面,这样会生成两套文件   -->
<%--@ include file="/4.jsp" --%>

<!-- 动态包含 -->
<%-- <jsp:include page="/4.jsp"></jsp:include> --%>

<!-- taglib标签,导入jstl核心库
    prefix="c" 表示给jstl标签库中的标签,起一个别名 -->
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <c:if test=""></c:if>
    bbbbbb
</body>
</html>
<!-- https://mvnrepository.com/ -->

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    aaaaaaaa
</body>
</html>
<%@ page import="Demo.User"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
    <%
        // 创建一个User对象
        User user = new User();
        user.setName("wanglong");
        user.setPwd("123");
        out.print(user.getName());
    %>
    <br>

    <jsp:useBean id="user1" class="Demo.User"></jsp:useBean>
    <%-- <jsp:useBean id="user1" class="Demo.User"/> --%>

    <jsp:setProperty property="name" name="user1" value="dingpeng" />
    <jsp:setProperty property="pwd" name="user1" value="456" />

    <%=user1 %>
    <br>
    <jsp:getProperty property="name" name="user1"/>
    <jsp:getProperty property="pwd" name="user1"/>
</body>
</html>

public class User {
    private String name;
    private String pwd;
    public User() {
        super();
    }
    public User(String name, String pwd) {
        super();
        this.name = name;
        this.pwd = pwd;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getPwd() {
        return pwd;
    }
    public void setPwd(String pwd) {
        this.pwd = pwd;
    }
    @Override
    public String toString() {
        return "User [name=" + name + ", pwd=" + pwd + "]";
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值