jsp_neizhiduixiang

jsp的内置对象

jsp的常用的内置对象有:out、request、response、session、application、Page、pageContext、

jsp的常用的内置对象有:out、request、response、session、application、Page、pageContext、exception 、config

1、out对象

(1)打印一首诗

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<%@ 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 'out.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>out内置对象:out对象是JSPWriter类的实例,是像客户端输出内容常用的对象。</h1>
<h4>常用的方法如下:</h4>
1void println()向客户端打印字符串<br>
2、void clear()清除缓冲区内容,如果在flush之后调用会抛出异常<br>
3、void clearBuffer();清除缓冲区的内容,如果在flush之后调用不会抛出异常与上面的一个相似<br>
<hr>
<%
out.println("<h2>静夜思</h2>");
out.println("床前明月光<br>");
out.println("疑是地上霜<br>");
out.flush();
//out.clear();//这里会抛出异常,就不会再执行了
//out.clearBuffer();//不会抛出异常,后面的程序仍然执行
out.println("举头望明月<br>");
out.println("低头思故乡<br>");
%>
<hr>
缓冲区大小;<%=out.getBufferSize() %>byte<br>
缓冲区剩余大小:<%=out.getRemaining() %>byte<br>
是否自动清空缓冲区:<%=out.isAutoFlush() %><br>
<hr>
</body>
</html>

(2)打印九九乘法表

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
<%@ 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 'Nine-nine.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>
<!-- 定义函数 -->
<%!
//返回九九乘法表对应的HTML代码,通过表达式来调用,在页面上显示
String printMultiTable()
{
String s="";
for(int i=1;i<=9;i++){
for(int j=1;j<=i;j++){
s+=i+"*"+j+"="+(i*j)+"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
}
s+="<br>";//追加换行标签
}
return s;
}
//jsp内置out对象,使用脚本方式,打印九九乘法表
void printMultiTable2(JspWriter out)
throws Exception//对两个out.println抛出异常
{
for(int i=1;i<=9;i++){
for(int j=1;j<=i;j++){
out.println(i+"*"+j+"="+(i*j)+"&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
}
out.println("<br>");//追加换行标签
}
}
%>
<h1>打印九九乘法表</h1> <br>
<hr>
<%=printMultiTable() %><hr>
<%printMultiTable2(out); %>
</body>
</html>

2、request对象(上)

request常用对象

常用的两个,也比较容易弄混的两个request.getParameter(String args[])、request.getParameterValues(String args[])

例:

主页面req.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
<%@ 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 'reg.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>
<h2>用户注册</h2>
<hr>
<form name="regForm" action="request.jsp" method="post">
<table>
<tr><td>用户名:<input type="text" name="username" /></td></tr>
<tr>
<td>爱好:
<td><input type="checkbox" name="favo" value="read" >读书</td>
<td><input type="checkbox" name="favo" value="swim" >游泳</td>
<td><input type="checkbox" name="favo" value="ball" >打球</td>
</tr>
<tr><td colspan="2"><input type="submit" value="提交"></td></tr>
</table>
</form>
<br>
<br>
<a href="request.jsp?username=秀儿">URL传参数</a>
</body>
</html>

跳转到request.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<%@ 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 'request.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>request内置对象</h1>
<%
request.setCharacterEncoding("utf-8");//解决中文乱码问题,无法解决url乱码 。如果要解决url问题,在Tomcat下的server.xml里找到Connector,并在后面配置URIEncoding="utf-8"
%>
用户名:<%=request.getParameter("username") %><br>
爱好:<%
if(request.getParameterValues("favo")!=null)
{
String[] fav=request.getParameterValues("favo");
for(int i=0;i<fav.length;i++){
out.println(fav[i]+"&nbsp;"+"&nbsp;");
}
}
%>
</body>
</html>

3、request与response

request与response