Java Server Pages (JSP) is a Java API in J2EE which allows dynamic creation of web pages using Java. It has the same purpose as other existing technologies like CGI or PHP. In this tutorial you will learn about Lifecycle of JSP pages, Dynamic content generation, Invoking Java code using JSP scripting elements, JavaBeans for JSP and Error Handling. The main difference between servlets and JSPs is that servlets are Java classes and JSPs are not (they are embedded in HTML pages). This difference also specifies where you want to use servlets and where you want to use JSPs: If you generate all of your html pages dynamically and will have complex logic, you will probably use servlets. If dynamic content is very low compared to the static content in your web page, then you will probably use JSPs.
Dynamic content generation
In this section, we will create our first JSP which will write “Hello World” to the web page if no user-name parameter is given. If user-name parameter is given (e.g. Smith), then it will print “Hello < user-name >”. (e.g. Hello Smith).
The source code of HelloWorld.jsp is shown in Figure-2.
Figure-2 The source code of HelloWorld.jsp
Figure-3 shows the HelloWorld.jsp when it is run without a user-name. The result of running the same JSP with a user-name of John is shown in Figure-4. As you will notice user-name is given to the JSP as a name parameter added to the URL of the JSP.
Figure-3 HelloWorld.jsp without a user-name parameter
Figure-4 HelloWorld.jsp with a user-name parameter
No comments:
Post a Comment