Monday 14 January 2013

JSP - Java Server Pages

JSP(Java Server Pages)是由Sun Microsystems公司倡导、许多公司参与一起建立的一种动态网页技术标准。
JSP技术有点类似ASP技术,它是在传统的网页HTML文件(*.htm,*.html)中插入Java程序段(Scriptlet)和JSP标记(tag),
从而形成JSP文件(*.jsp)。 用JSP开发的Web应用是跨平台的,既能在Linux下运行,也能在其他操作系统上运行。
 
JSP技术使用Java编程语言编写类XML的tags和scriptlets,来封装产生动态网页的处理逻辑。网页还能通过tags和
scriptlets访问存在于服务端的资源的应用逻辑。JSP将网页逻辑与网页设计和显示分离,支持可重用的基于组件的设计,
使基于Web的应用程序的
开发变得迅速和容易。 

Web服务器在遇到访问JSP网页的请求时,首先执行其中的程序段,然后将执行结果连同JSP文件中的HTML代码一起
返回给客户。插入的Java程序段可以操作数据库、重新定向网页等,以实现建立动态网页所需要的功能。 

JSP与Java Servlet一样,是在服务器端执行的,通常返回该客户端的就是一个HTML文本,因此客户端只要有浏览器就
能浏览。 

JSP的1.0规范的最后版本是1999年9月推出的,12月又推出了1.1规范。目前较新的是JSP1.2规范,JSP2.0规范的征求
意见稿也已出台。

JSP页面由HTML代码和嵌入其中的Java代码所组成。服务器在页面被客户端请求以后对这些Java代码进行处理,然后
将生成的HTML页面返回给客户端的浏览器。Java Servlet 是JSP的技术基础,而且大型的Web应用程序的开发需要Java 
Servlet和JSP配合才能完成。JSP具备了Java技术的简单易用,完全的面向对象,具有平台无关性且安全可靠,主要面向
因特网的所有特点。 

1. JSP技术的强势 

(1)一次编写,到处运行。在这一点上Java比PHP更出色,除了系统之外,代码不用做任何更改。

(2)系统的多平台支持。基本上可以在所有平台上的任意环境中开发,在任意环境中进行系统部署,在任意环境中扩展。
相比ASP/PHP的局限性是现而易见的。 

(3)强大的可伸缩性。从只有一个小的Jar文件就可以运行Servlet/JSP,到由多台服务器进行集群和负载均衡,到多台
Application进行事务处理,消息处理,一台服务器到无数台服务器,Java显示了一个巨大的生命力。 

(4)多样化和功能强大的开发工具支持。这一点与ASP很像,Java已经有了许多非常优秀的开发工具,而且许多可以免
费得到,并且其中许多已经可以顺利的运行于多种平台之下。 

2. JSP技术的弱势 

(1) 与ASP一样,Java的一些优势正是它致命的问题所在。正是由于为了跨平台的功能,为了极度的伸缩能力,所以
极大的增加了产品的复杂性。 

(2) Java的运行速度是用class常驻内存来完成的,所以它在一些情况下所使用的内存比起用户数量来说确实是“最低
性能价格比”了。从另一方面,它还需要硬盘空间来储存一系列的.java文件和.class文件,以及对应的版本文件。 
 
__________________________________________________________________________________
 
1. Download & install JDK, JBoss, Apache-maven
 
2. Set environment variable for JAVA_HOME, JBOSS_HOME, M2_HOME & PATH
 
3. create the jsp file.
 
4. create the web.xml in the WEB-INF directory.
 
5. run command to generate war file: 
        jar cvf XXX.war XXX.jsp WEB-INF
 
6. put the war file in the %JBOSS_HOME%\standalone\deployments directory.
 
7. execute the JBoss web server using standalone.bat
 
8. URL to be used: http://localhost:8080/XXX/XXX.jsp

__________________________________________________________________________________  
 
Important: 
1. The character sequences <%= and %> enclose Java expressions, which are evaluated at run time.
2.   JSP allows user to write blocks of Java code inside the JSP by placing your Java code between <% and %> 
character (just like expressions, but without the = sign at the start of the sequence.)
3. Instead of using an expression, we are generating the HTML directly
by printing to the "out" variable.  The "out" variable is of type javax.servlet.jsp.JspWriter. 
      out.println("xxxxxxxx");
 
 

<jsp:useBean> in JSP

        
Syntax:  
<jsp:useBean id= "nameOfInstance" scope= "page | request | session | application" class= "package.class" type= "package.class > </jsp:useBean>.

The child tags of <jsp:useBean> are:
<jsp:setProperty name = "nameOfBeanInstance" property="*"  | propertyName ("parameterName")  | value=string | <%= expression%> >
<jsp:getProperty name="nameOfBeanInstance" property="propertyName"/>
Scope of <jsp:useBean>

1. page: It means that we can use the Bean within the JSP page.
2. request: It means that we can use the Bean from any JSP page processing the same request.
3. session: It means that we use the Bean from any Jsp page in the same session as the JSP page that created the Bean.
4. application: It means that we use the Bean from any page in the same application as the Jsp page that created the Bean.

No comments:

Post a Comment