1. JSP的内置对象有
request,请求对象,代表来自客户端的请求
response, 响应对象,代表对客户端的响应
out,输出对象,通过out对象发送的内容将是浏览器需要显示的内容。
application 应用程序对象,负责提供应用程序在服务器中运行时提供的一些全局信息。
config 配置对象
page 页面对象
exception 异常对象
pageContext 页面上下午对象
session 会画对象, 代表服务器与客户端所建立的会话,当需要保存在不同的JSP页面中保留客户信息的情况下使用,比如在线购物、客户轨迹跟踪等。
2. request,out对象的使用
创建login.jsp, result.jsp
login.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<%@ 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=
"result.jsp"
method=
"post"
>
username: <input type=
"text"
name=
"username1"
><br>
password: <input type=
"password"
name=
"password1"
><br>
<input type=
"submit"
value=
"submit"
>
<input type=
"reset"
value=
"reset"
>
</form>
</body>
</html>
|
result.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<%@ 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(
"username1"
);
String password = request.getParameter(
"password1"
);
out.println(
"username:"
+ username +
"<br>"
);
out.println(
"password:"
+ password +
"<br>"
);
System.out.println(
"username:"
+ username );
System.out.println(
"password:"
+ password );
%>
</body>
</html>
|
result.jsp 使用了request和out对象。
3. session对象的使用
session1.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<%@ 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=
"session2.jsp"
>
姓名
<input type=
"text"
name=
"username"
>
<input type=
"submit"
value=
"提交"
>
</form>
</body>
</html>
|
session2.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<%@ 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"
);
session.setAttribute(
"LogName"
, username);
%>
你的名字是 <%=username %>已經写入session
<br>
<a href=
'session3.jsp'
>check</a>
</body>
</html>
|
session3.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<%@ 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>
session值为:
<% String yourname = (String)session.getAttribute(
"LogName"
);
if
(yourname ==
null
) {
%>
您还未登录
<% }
else
{ %>
"<%=yourname %>"
已经登录
<%} %>
</body>
</html>
|
本文转自Work Hard Work Smart博客园博客,原文链接:http://www.cnblogs.com/linlf03/p/7637575.html,如需转载请自行联系原作者