Skip navigation.
Home

Java Server Pages

Java Server Page technology provides a simple, fast way to develop dynamic web applications. In JSP you can create web pages to access Databases, Directories ...etc to generate a dynamic content. JSP contains Custom tag option in order to reduce the amount of code you write in JSP page and write it in the Tag Handler which can be reusable. JSP specification has the common used tag libraries to make it easier and easier to make the basic operations in java like loops, statement, expressions and even access your session and request object. This tag libraries is [JSTL|http://java.sun.com/products/jsp/jstl/index.jsp] (JSP Standard Tag Libraries) __How JSP is working?__%%% when you write a jsp Page the [Web Server] compile it and create a [Servlet] for this jsp page. __How can i write a jsp page? __%%% JSP pages is a web pages which combine JSP Scriptlet with HTML tags, so if you create a page .jsp and write your html tags the web server will compile it, then you can request your page with its name. If you would like to add any java code you can use the JSP scriptlet tags as follow: {{{ <% //add java code here. %> }}} Then the web server will execute this block on the server and return you the result inside the html. Fore more information about web server setup read about tomcat in [this article|http://www.egjug.org/?q=tomcat_linux]. __JSP Hello World__%%% *Setup your Java SDK. *[Setup your web server|http://www.egjug.org/?q=node/101]. *Create your first page: {{{ Hello EGJUG

Welcome to Egjug

<% out.write("This is my first JSP page"); out.write("

This is a text with html tags

"); %> }}} *Copy the page to the web server root directory. *Request your page from the browser. {{{ http://localhost:8080/index.jsp }}} __How can i declare a variable?__%%% Well, in JSP you can declare a variable like this: {{{ <%! String hello = "Hello Egjug!";%> }}} and use it as follow: {{{ <%=hello%> }}} as you see the tag followed by exclamation mark used for declaration and the tag followed by equal mark used for printing. __The next question will be is there any limitation over JSP? __%%% In JSP pages you can write all what you would like to do as a Java program, because JSP is compiled back to servlet which can contain any code. JSP is not a scripting language. You can write a java code withing scriptlet tags in the JSP page. __How can i use java API's? __%%% The page directive tag is used to import a java classes.%%% In the following example, you will see how to use the java API's in your jsp code. {{{ <%@ page import="java.util.*"%> <%! Vector numbers = new Vector();%> <% for(int i=0;i<10;i++) { numbers.add(i+""); } for(int i=0;i<10;i++) { %> <%=numbers.get(i)%> <% } %> }}}