JSP – Syntax ”; Previous Next In this chapter, we will discuss Syntax in JSP. We will understand the basic use of simple syntax (i.e, elements) involved with JSP development. Elements of JSP The elements of JSP have been described below − The Scriptlet A scriptlet can contain any number of JAVA language statements, variable or method declarations, or expressions that are valid in the page scripting language. Following is the syntax of Scriptlet − <% code fragment %> You can write the XML equivalent of the above syntax as follows − <jsp:scriptlet> code fragment </jsp:scriptlet> Any text, HTML tags, or JSP elements you write must be outside the scriptlet. Following is the simple and first example for JSP − <html> <head><title>Hello World</title></head> <body> Hello World!<br/> <% out.println(“Your IP address is ” + request.getRemoteAddr()); %> </body> </html> NOTE − Assuming that Apache Tomcat is installed in C:apache-tomcat-7.0.2 and your environment is setup as per environment setup tutorial. Let us keep the above code in JSP file hello.jsp and put this file in C:apache-tomcat7.0.2webappsROOT directory. Browse through the same using URL http://localhost:8080/hello.jsp. The above code will generate the following result − JSP Declarations A declaration declares one or more variables or methods that you can use in Java code later in the JSP file. You must declare the variable or method before you use it in the JSP file. Following is the syntax for JSP Declarations − <%! declaration; [ declaration; ]+ … %> You can write the XML equivalent of the above syntax as follows − <jsp:declaration> code fragment </jsp:declaration> Following is an example for JSP Declarations − <%! int i = 0; %> <%! int a, b, c; %> <%! Circle a = new Circle(2.0); %> JSP Expression A JSP expression element contains a scripting language expression that is evaluated, converted to a String, and inserted where the expression appears in the JSP file. Because the value of an expression is converted to a String, you can use an expression within a line of text, whether or not it is tagged with HTML, in a JSP file. The expression element can contain any expression that is valid according to the Java Language Specification but you cannot use a semicolon to end an expression. Following is the syntax of JSP Expression − <%= expression %> You can write the XML equivalent of the above syntax as follows − <jsp:expression> expression </jsp:expression> Following example shows a JSP Expression − <html> <head><title>A Comment Test</title></head> <body> <p>Today”s date: <%= (new java.util.Date()).toLocaleString()%></p> </body> </html> The above code will generate the following result − Today”s date: 11-Sep-2010 21:24:25 JSP Comments JSP comment marks text or statements that the JSP container should ignore. A JSP comment is useful when you want to hide or “comment out”, a part of your JSP page. Following is the syntax of the JSP comments − <%– This is JSP comment –%> Following example shows the JSP Comments − <html> <head><title>A Comment Test</title></head> <body> <h2>A Test of Comments</h2> <%– This comment will not be visible in the page source –%> </body> </html> The above code will generate the following result − A Test of Comments There are a small number of special constructs you can use in various cases to insert comments or characters that would otherwise be treated specially. Here”s a summary − S.No. Syntax & Purpose 1 <%– comment –%> A JSP comment. Ignored by the JSP engine. 2 <!– comment –> An HTML comment. Ignored by the browser. 3 <% Represents static <% literal. 4 %> Represents static %> literal. 5 ” A single quote in an attribute that uses single quotes. 6 “ A double quote in an attribute that uses double quotes. JSP Directives A JSP directive affects the overall structure of the servlet class. It usually has the following form − <%@ directive attribute=”value” %> There are three types of directive tag − S.No. Directive & Description 1 <%@ page … %> Defines page-dependent attributes, such as scripting language, error page, and buffering requirements. 2 <%@ include … %> Includes a file during the translation phase. 3 <%@ taglib … %> Declares a tag library, containing custom actions, used in the page We would explain the JSP directive in a separate chapter JSP – Directives JSP Actions JSP actions use constructs in XML syntax to control the behavior of the servlet engine. You can dynamically insert a file, reuse JavaBeans components, forward the user to another page, or generate HTML for the Java plugin. There is only one syntax for the Action element, as it conforms to the XML standard − <jsp:action_name attribute=”value” /> Action elements are basically predefined functions. Following table lists out the available JSP Actions − S.No. Syntax & Purpose 1 jsp:include Includes a file at the time the page is requested. 2 jsp:useBean Finds or instantiates a JavaBean. 3 jsp:setProperty Sets the property of a JavaBean. 4 jsp:getProperty Inserts the property of a JavaBean into the output. 5 jsp:forward Forwards the requester to a new page. 6 jsp:plugin Generates browser-specific code that makes an OBJECT or EMBED tag for the Java plugin. 7 jsp:element Defines XML elements dynamically. 8 jsp:attribute Defines dynamically-defined XML element”s attribute. 9 jsp:body Defines dynamically-defined XML element”s body. 10 jsp:text Used to write template text in JSP pages and documents. We would explain JSP actions in a separate chapter JSP – Actions JSP Implicit Objects JSP supports nine automatically defined variables, which are also called implicit objects. These variables are − S.No. Object & Description 1 request This is the HttpServletRequest object associated with the request. 2 response This is the HttpServletResponse object associated with the response to the client. 3 out This is the PrintWriter object used to send output to the client. 4 session This is the HttpSession object associated with the request. 5 application This is the ServletContext object associated with the application context. 6 config This is the ServletConfig object associated with the page. 7 pageContext This encapsulates use
Category: jsp
JSP – Overview
JSP – Overview ”; Previous Next What is JavaServer Pages? JavaServer Pages (JSP) is a technology for developing Webpages that supports dynamic content. This helps developers insert java code in HTML pages by making use of special JSP tags, most of which start with <% and end with %>. A JavaServer Pages component is a type of Java servlet that is designed to fulfill the role of a user interface for a Java web application. Web developers write JSPs as text files that combine HTML or XHTML code, XML elements, and embedded JSP actions and commands. Using JSP, you can collect input from users through Webpage forms, present records from a database or another source, and create Webpages dynamically. JSP tags can be used for a variety of purposes, such as retrieving information from a database or registering user preferences, accessing JavaBeans components, passing control between pages, and sharing information between requests, pages etc. Why Use JSP? JavaServer Pages often serve the same purpose as programs implemented using the Common Gateway Interface (CGI). But JSP offers several advantages in comparison with the CGI. Performance is significantly better because JSP allows embedding Dynamic Elements in HTML Pages itself instead of having separate CGI files. JSP are always compiled before they are processed by the server unlike CGI/Perl which requires the server to load an interpreter and the target script each time the page is requested. JavaServer Pages are built on top of the Java Servlets API, so like Servlets, JSP also has access to all the powerful Enterprise Java APIs, including JDBC, JNDI, EJB, JAXP, etc. JSP pages can be used in combination with servlets that handle the business logic, the model supported by Java servlet template engines. Finally, JSP is an integral part of Java EE, a complete platform for enterprise class applications. This means that JSP can play a part in the simplest applications to the most complex and demanding. Advantages of JSP Following table lists out the other advantages of using JSP over other technologies − vs. Active Server Pages (ASP) The advantages of JSP are twofold. First, the dynamic part is written in Java, not Visual Basic or other MS specific language, so it is more powerful and easier to use. Second, it is portable to other operating systems and non-Microsoft Web servers. vs. Pure Servlets It is more convenient to write (and to modify!) regular HTML than to have plenty of println statements that generate the HTML. vs. Server-Side Includes (SSI) SSI is really only intended for simple inclusions, not for “real” programs that use form data, make database connections, and the like. vs. JavaScript JavaScript can generate HTML dynamically on the client but can hardly interact with the web server to perform complex tasks like database access and image processing etc. vs. Static HTML Regular HTML, of course, cannot contain dynamic information. What is Next? I would take you step by step to set up your environment to start with JSP. I”m assuming you have good hands-on with Java Programming to proceed with learning JSP. If you are not aware of Java Programming Language, then we would recommend you go through our Java Tutorial to understand Java Programming. Print Page Previous Next Advertisements ”;
JSP Tutorial
JSP Tutorial PDF Version Quick Guide Resources Job Search Discussion Java Server Pages (JSP) is a server-side programming technology that enables the creation of dynamic, platform-independent method for building Web-based applications. JSP have access to the entire family of Java APIs, including the JDBC API to access enterprise databases. This tutorial will teach you how to use Java Server Pages to develop your web applications in simple and easy steps. Why to Learn JSP? JavaServer Pages often serve the same purpose as programs implemented using the Common Gateway Interface (CGI). But JSP offers several advantages in comparison with the CGI. Performance is significantly better because JSP allows embedding Dynamic Elements in HTML Pages itself instead of having separate CGI files. JSP are always compiled before they are processed by the server unlike CGI/Perl which requires the server to load an interpreter and the target script each time the page is requested. JavaServer Pages are built on top of the Java Servlets API, so like Servlets, JSP also has access to all the powerful Enterprise Java APIs, including JDBC, JNDI, EJB, JAXP, etc. JSP pages can be used in combination with servlets that handle the business logic, the model supported by Java servlet template engines. Finally, JSP is an integral part of Java EE, a complete platform for enterprise class applications. This means that JSP can play a part in the simplest applications to the most complex and demanding. Applications of JSP As mentioned before, JSP is one of the most widely used language over the web. I”m going to list few of them here: JSP vs. Active Server Pages (ASP) The advantages of JSP are twofold. First, the dynamic part is written in Java, not Visual Basic or other MS specific language, so it is more powerful and easier to use. Second, it is portable to other operating systems and non-Microsoft Web servers. JSP vs. Pure Servlets It is more convenient to write (and to modify!) regular HTML than to have plenty of println statements that generate the HTML. JSP vs. Server-Side Includes (SSI) SSI is really only intended for simple inclusions, not for “real” programs that use form data, make database connections, and the like. JSP vs. JavaScript JavaScript can generate HTML dynamically on the client but can hardly interact with the web server to perform complex tasks like database access and image processing etc. JSP vs. Static HTML Regular HTML, of course, cannot contain dynamic information. Audience This tutorial has been prepared for the beginners to help them understand basic functionality of Java Server Pages (JSP) to develop your web applications. After completing this tutorial you will find yourself at a moderate level of expertise in using JSP from where you can take yourself to next levels. Prerequisites We assume you have little knowledge of how web applications work over HTTP, what is web server and what is web browsers. It will be great if you have some knowledge of web application development using any programming language. Frequently Asked Questions about JSP There are some very Frequently Asked Questions(FAQ) about JSP, this section tries to answer them briefly. What is the full form of JSP? The full form of JSP is JavaServer Pages. In some textbooks, you can find that JSP stands for Jakarta Server Pages. Is JSP front end or backend? JSP is a front-end technology used to develop a graphical user interface of a Java application. Which web servers support JSP technology? The JSP technology is supported by several web servers. The most popular are Apache Tomcat, GlassFish, Apache TomEE, WildFly and many more. What is JSP life cycle? There are four steps involved in JSP life cycle, which are − JSP Compilation − In this step, JSP is parsed and converted into a servlet for the compilation. JSP Initialization − The initialization is performed by invoking the jspInit() method. JSP Execution − In the following step, all the requests are handled. JSP Cleanup − The final step involves removal of JSP from use. Who invented JSP? JSP was released by Sun Microsystems in 1999. What is the syntax of JSP? The syntax of JSP is very simple, we simply need to start JSP tag with “<%” and end with “%>”. Then, save the JSP file with the extension “.jsp”. Which version of JSP is the latest? The latest version of JSP is 3.1 which was released on 31st April 2022. Which is better servlet or JSP? Both Servlet and JSP are used to create web applications using Java programming language. Both have their unique characteristic and functionality. Servlet is faster as compared to JSP, however, JSP is more flexible than servlet. How can we handle the exceptions in JSP? In JSP, we use errorPage and isErrorPage attributes to handle exceptions. How to learn JSP? Here is the summarized list of tips which you can follow to learn JSP − First and most important to make up your mind to learn JSP. Install the required IDE and other software that are essential for JSP on your computer system. Follow our tutorial step by step starting from the very beginning. Read more articles, watch online courses or buy a book on JSP to enhance your knowledge. Try to develop small software or projects using JSP. Print Page Previous Next Advertisements ”;