转载自【黑米GameDev街区】 原文链接: http://www.himigame.com/java-servlet/858.html
对于Servlet 学习过后,那么接触到JSP,那么可以发现JSP编程则会突然喜爱上,因为JSP不仅仅也是一个servlet,还能帮我们直接从servlet的容器(Tomcat)为我们直接生成对应的servlet代码。至于更加详细的介绍大家可以自行baidu~goole
下面让我们首先写一个JSP,然后大家就明白了其用途:
1. 首先我们编写如下一个jsp文件,这里起名 himi.jsp,其内容如下:
1
2
3
4
5
6
7
8
9
10
|
&
lt
;
html
&
gt
;
&
lt
;
head
&
gt
;
&
lt
;
/
head
&
gt
;
&
lt
;
body
&
gt
;
&
lt
;
%
out
.
println
(
"HelloWorld!"
)
;
%
&
gt
;
&
lt
;
/
body
&
gt
;
&
lt
;
/
html
&
gt
;
|
OK,将其之间放置我们上一篇创建的webapp根目录即可;
然后尝试访问一下这个himi.jsp 观察如下:(这里Himi在tomcat目录下创建的webapp叫“MyWebApp”)
OK,发现Servlet 返回给Client端一个“ Hello World! ”字符串
大家先到tomcat目录下的/work/Catalina/localhost/MyWebApp/org/apache/jsp文件夹下我们可以看到如下两个文件:
a)himi_jsp.class b)himi_jsp.java
不二话打开 himi_jsp.java 如下:
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
|
/*
* Generated by the Jasper component of Apache Tomcat
* Version: Apache Tomcat/7.0.27
* Generated at: 2012-05-24 16:07:57 UTC
* Note: The last modified time of this file was set to
* the last modified time of the source file after
* generation to assist with modification tracking.
*/
package
org
.
apache
.
jsp
;
import
javax
.
servlet
.
*
;
import
javax
.
servlet
.
http
.
*
;
import
javax
.
servlet
.
jsp
.
*
;
public
final
class
himi_jsp
extends
org
.
apache
.
jasper
.
runtime
.
HttpJspBase
implements
org
.
apache
.
jasper
.
runtime
.
JspSourceDependent
{
private
static
final
javax
.
servlet
.
jsp
.
JspFactory
_jspxFactory
=
javax
.
servlet
.
jsp
.
JspFactory
.
getDefaultFactory
(
)
;
private
static
java
.
util
.
Map
&
lt
;
java
.
lang
.
String
,
java
.
lang
.
Long
&
gt
;
_jspx_dependants
;
private
javax
.
el
.
ExpressionFactory
_el_expressionfactory
;
private
org
.
apache
.
tomcat
.
InstanceManager
_jsp_instancemanager
;
public
java
.
util
.
Map
&
lt
;
java
.
lang
.
String
,
java
.
lang
.
Long
&
gt
;
getDependants
(
)
{
return
_jspx_dependants
;
}
public
void
_jspInit
(
)
{
_el_expressionfactory
=
_jspxFactory
.
getJspApplicationContext
(
getServletConfig
(
)
.
getServletContext
(
)
)
.
getExpressionFactory
(
)
;
_jsp_instancemanager
=
org
.
apache
.
jasper
.
runtime
.
InstanceManagerFactory
.
getInstanceManager
(
getServletConfig
(
)
)
;
}
public
void
_jspDestroy
(
)
{
}
public
void
_jspService
(
final
javax
.
servlet
.
http
.
HttpServletRequest
request
,
final
javax
.
servlet
.
http
.
HttpServletResponse
response
)
throws
java
.
io
.
IOException
,
javax
.
servlet
.
ServletException
{
final
javax
.
servlet
.
jsp
.
PageContext
pageContext
;
javax
.
servlet
.
http
.
HttpSession
session
=
null
;
final
javax
.
servlet
.
ServletContext
application
;
final
javax
.
servlet
.
ServletConfig
config
;
javax
.
servlet
.
jsp
.
JspWriter
out
=
null
;
final
java
.
lang
.
Object
page
=
this
;
javax
.
servlet
.
jsp
.
JspWriter
_jspx_out
=
null
;
javax
.
servlet
.
jsp
.
PageContext
_jspx_page_context
=
null
;
try
{
response
.
setContentType
(
"text/html"
)
;
pageContext
=
_jspxFactory
.
getPageContext
(
this
,
request
,
response
,
null
,
true
,
8192
,
true
)
;
_jspx_page_context
=
pageContext
;
application
=
pageContext
.
getServletContext
(
)
;
config
=
pageContext
.
getServletConfig
(
)
;
session
=
pageContext
.
getSession
(
)
;
out
=
pageContext
.
getOut
(
)
;
_jspx_out
=
out
;
out
.
write
(
"<html>\n"
)
;
out
.
write
(
"\t<head>\n"
)
;
out
.
write
(
"\t</head>\n"
)
;
out
.
write
(
"\n"
)
;
out
.
write
(
"\t<body>\n"
)
;
out
.
write
(
"\t\t"
)
;
out
.
println
(
"HelloWorld!"
)
;
out
.
write
(
"\n"
)
;
out
.
write
(
"\t</body>\n"
)
;
out
.
write
(
"</html>"
)
;
}
catch
(
java
.
lang
.
Throwable
t
)
{
if
(
!
(
t
instanceof
javax
.
servlet
.
jsp
.
SkipPageException
)
)
{
out
=
_jspx_out
;
if
(
out
!=
null
&
amp
;
&
amp
;
out
.
getBufferSize
(
)
!=
0
)
try
{
out
.
clearBuffer
(
)
;
}
catch
(
java
.
io
.
IOException
e
)
{
}
if
(
_jspx_page_context
!=
null
)
_jspx_page_context
.
handlePageException
(
t
)
;
}
}
finally
{
_jspxFactory
.
releasePageContext
(
_jspx_page_context
)
;
}
}
}
|
看到这里大家应该明白了,当客户端访问jsp后,jsp默认生成一个 servlet 并且编译字节码,(当然是第一次访问生成并编译,后续访问就不会再次编译了);
JSP 语法:
拐回头来看 himi.jsp 代码:
1
2
3
4
5
6
7
8
9
10
|
&
lt
;
html
&
gt
;
&
lt
;
head
&
gt
;
&
lt
;
/
head
&
gt
;
&
lt
;
body
&
gt
;
&
lt
;
%
out
.
println
(
"HelloWorld!"
)
;
%
&
gt
;
&
lt
;
/
body
&
gt
;
&
lt
;
/
html
&
gt
;
|
【JSP 自动生成的servlet代码,其实内置了一些对象,如下:】
out 就是PrintWriter;
req 就是HttpServletRequest,
resp 就是HttpServletResponse
1. <% … %> 两个符号之间是我们的servlet代码;也就是程序代码块,在其中可以任意放置java代码;
也就是说客户端访问jsp后默认将我们himi.jsp中的内容转换成servlet;
2. 其中注视的方式:
1) <%–… …–%> 2) <%//… …%> 3) <%/*… …*/%>
要注意:以上注视都是服务器端的注视方式,如果你在jsp使用客户端注视方式:
<!– … –> 那要格外注意!如果你在客户端注视方式加入<%…%>如下:
<!– <% %> –>中是无法阻止服务器端的代码!只能保证<%..%>不保证在客户端显示出来罢了;
3. <%! … %> 表示将其中的代码成为自动生成servlet类的成员变量或者成员函数;
4. <%= … %> 等同于 <% out.println(); %>
5. request.getParameter(“xxx”); 可以动态获取到xxx这个属性的参数;
例如传入一个 xxx.jsp?xxx=yyy
6. <%@Directive 属性=”属性值”%> 编译期间的命令;
Directive 常用: a) page b) include c) taglib
JSP服务器端和客户端代码互相调用方法:
示例代码段1:
1
2
3
4
5
|
xx
.
jsp
&
lt
;
script
&
gt
;
&
lt
;
%
out
.
println
(
"var a =10"
)
;
%
&
gt
;
&
lt
;
/
script
&
gt
;
|
示例代码段2:
1
2
3
4
5
|
xx
.
jsp
&
lt
;
script
&
gt
;
var
a
=
' <% out.print(23); %> '
;
&
lt
;
/
script
&
gt
;
|
注意 :
1)JSP服务器端代码<%…%>总执行在Client端代码之前
2)代码段2 要注意 “ print(23) ”而不是“println(23)” 因为println是会换行造成客户端语法错误!;