JSP基础1

来源:互联网 发布:tv看电视直播软件 编辑:程序博客网 时间:2024/05/21 10:58

JSP 指令

JSP中的三种指令标签:

<%@ page ... %> 定义网页依赖属性,比如脚本语言、error页面、缓存需求等等<%@ include ... %>  包含其他文件<%@ taglib ... %>   引入标签库的定义

Page指令的语法格式:(添加在头部)

<%@ page language="java" import="import java.util.*" contentType="text/html;charset=utf-8" %>

Include指令的语法格式如下:

<%@ include file="文件相对 url 地址" %>

Taglib指令的语法:

<%@ taglib uri="uri" prefix="prefixOfTag" %>

JSP注释

三种注释方法:
(1)<–html注释–>                客户端通过源代码可见
(2)<%–JSP的注释–%>         不可见
(3)//单行注释,/*多行注释*/  不可见

JSP语法

脚本程序的语法格式:

<% 代码片段 %>

例如

<html><head><title>Hello World</title></head><body>Hello World!<br/><%out.println("Your IP address is " + request.getRemoteAddr());%></body></html>

JSP声明(变量或者方法)

变量或者语法
在<%! %>中声明的变量,是全局变量

在<% %>中声明的变量,是局部变量
示例:

<%! int i = 0; %> <%! int a, b, c; %> <%! Circle a = new Circle(2.0); %> <%! int add(int a,int b){        return x + y;        }%>

JSP表达式(输出)

JSP表达式的语法格式:

<%= 表达式 %>

示例

<p>   今天的日期是: <%= (new java.util.Date()).toLocaleString()%> <br/>   x + y <%= add(1,2) %></p>

JSP 生命周期

JSP编译,初始化,执行,销毁.

这里写图片描述

九大内置对象

5个常用对象: out(输出) request(请求) response(响应) session(会话) application(应用)

4个不常用: page pageContext exception config

GET/POST提交方式的区别

具体参考w3school
http://www.w3school.com.cn/tags/html_ref_httpmethods.asp
测试代码
index.jsp

<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <base href="<%=basePath%>">    <title>My JSP 'index.jsp' starting page</title>    <meta http-equiv="pragma" content="no-cache">    <meta http-equiv="cache-control" content="no-cache">    <meta http-equiv="expires" content="0">        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">    <meta http-equiv="description" content="This is my page">    <!--    <link rel="stylesheet" type="text/css" href="styles.css">    -->  </head>  <body>    <h1>用户登录</h1>    <form action="dologin.jsp" name="loginForm" method = "post">        <table>            <tr>            <td>用户名</td>            <td><input type="text" name="username"/></td>            </tr>            <tr>            <td>            密码:            </td>            <td><input type="password" name="password"/></td>            </tr>            <tr>            <td colspan="2"><input type="submit" value="login"></td>            </tr>        </table>    </form>  </body></html>

登录后跳转的页面dologin.jsp

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <base href="<%=basePath%>">    <title>My JSP 'dologin.jsp' starting page</title>    <meta http-equiv="pragma" content="no-cache">    <meta http-equiv="cache-control" content="no-cache">    <meta http-equiv="expires" content="0">        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">    <meta http-equiv="description" content="This is my page">    <!--    <link rel="stylesheet" type="text/css" href="styles.css">    -->  </head>  <body>    登录成功 <br>  </body></html>
原创粉丝点击