JSP

JSP

JSP- Java Server Pages By Prof. B.A.Khivsara Note: The material to prepare this presentation has been taken from internet and are generated only for students reference and not for commercial use. Introduction to JSP JSP technology is used to create web application just like Servlet technology. It can be thought of as an extension to servlet because it provides more functionality than servlet such as expression language, jstl etc.

A JSP page consists of HTML tags and JSP tags. The jsp pages are easier to maintain than servlet because we can separate designing and development. It provides some additional features such as Expression Language, Custom Tag etc. Advantages of 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 nonMicrosoft Web servers. 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. Advantage of JSP over Servlet 1) Extension to Servlet JSP technology is the extension to servlet technology. We can use all the features of servlet in JSP. In addition to, we can use implicit objects,

predefined tags, expression language and Custom tags in JSP, that makes JSP development easy. 2) Easy to maintain JSP can be easily managed because we can easily separate our business logic with presentation logic. In servlet technology, we mix our business logic with the presentation logic. 3) Fast Development: No need to recompile and redeploy If JSP page is modified, we don't need to recompile and redeploy the project. The servlet code needs to be updated and recompiled if we have to change the look and feel of the application. 4) Less code than Servlet

In JSP, we can use a lot of tags such as action tags, jstl, custom tags etc. that reduces the code. Moreover, we can use EL, implicit objects etc. Creating a JSP Page A JSP page looks similar to an HTML page, but a JSP page also has Java code in it. We can put any regular Java Code in a JSP file using a scriplet tag which start with <% and ends with %>. JSP pages are used to develop dynamic responses.

Creating a JSP Page in Eclipse Open Eclipse, Click on New Dynamic Web Project Creating a JSP Page in Eclipse Give a name to your project and click on OK Creating a JSP Page in Eclipse You will see a new project created in Project Explorer Creating a JSP Page in Eclipse

To create a new JSP file right click on Web Content directory, New JSP file Creating a JSP Page in Eclipse Give a name to your JSP file and click Finish. Creating a JSP Page in Eclipse Write something in your JSP file. The complete HTML and the JSP code, goes inside the tag, just like HTML pages.

Creating a JSP Page in Eclipse To run your project, right click on Project, select Run As Run on Server Creating a JSP Page in Eclipse To start the server, Choose existing server name and click on finish Creating a JSP Page in Eclipse See the Output in your browser. Elements of JSP Scripting Element

Comment Directive Declaration Scriptlet Expression Example <%-- comment --%> <%@ directive %> <%! declarations %> <% scriplets %>

<%= expression %> JSP Comments JSP comment marks text or statements that the JSP container should ignore. Syntax of the JSP comments <%-- This is JSP comment --%> Example shows the JSP Comments

A Test of Comments

<%-- This comment will not be visible in the page source --%> 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. syntax of Scriptlet <% code fragment %> first example for JSP

Hello World Hello World!
<% out.println("Your IP address is " + request.getRemoteAddr()); %> 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. Syntax for JSP Declarations <%! declaration; %> 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. you cannot use a semicolon to end an expression. Syntax of JSP Expression <%= expression %> Example shows a JSP Expression <%= "Welcome "+request.getParameter("uname") %>

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 JSP page directive The page directive defines attributes that apply to an entire JSP page. Syntax of JSP page directive

<%@ page attribute="value" %> Database Connectivity in JSP using JDBC Java JDBC is a java API to connect and execute query with the database. JDBC API uses jdbc drivers to connect with the database. JDBC Driver is a software component that enables java application to interact with the database. There are 4 types of JDBC drivers: 1.

2. 3. 4. JDBC-ODBC bridge driver Native-API driver (partially java driver) Network Protocol driver (fully java driver) Thin driver (fully java driver) JDBC Connectivity with JSP Software Requirement

1 MySQL 2 MySQL Connector (Jar file) 3 Apache Tomcat Server 4 Eclipse Important Note:

Copy MySQL Connector (Jar file) into Tomcats Bin and Lib folder Before running program right click on project name -> Build path -> Configure build path -> Libraries -> Add External Jar file -> Mysql .jar file -> ok JDBC Connectivity with JSPCreate table and add rows in database(mysql) Create the Employee table in the db1 database as follows

mysql> Create database db1; mysql> use db1; mysql> create table emp ( id int, name varchar (255)

); Query OK, 0 rows affected (0.08 sec) JDBC Connectivity with JSPCreate table and add rows in database(mysql) Insert Data mysql> INSERT INTO emp VALUES (100, 'Zara''); mysql> INSERT INTO Employees VALUES (101, 'Mamta'); JSP Code in Eclipse File->New->Web->Dynamic Web Project->Give Project Name Right click on Project name->new->package->Give package

name For Writing JSP Code project-> Right click->new->JSP file-> Give file name with .jsp Extension In Body tag write java code in script let tag <% %> Right click on .jsp file -> Run -> Run as Server JDBC Connectivity with JSPExample 1- JSP Code for to select data from emp table <%@ page import ="java.sql.*" %> <%@ page import ="javax.sql.*" %> <% Class.forName("com.mysql.jdbc.Driver");

java.sql.Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/db1","root","root"); Statement st= con.createStatement(); ResultSet rs=st.executeQuery("select * from emp"); out.print("

Employee Database

"); out.print("

"); out.print("

"); JDBC Connectivity with JSPExample 1- JSP Code for to select data from emp table while(rs.next())

{ out.print("

"); out.print("

"); out.print("

"); out.print("

"); } %>

JDBC Connectivity with JSPExample 2- JSP Code for insert data in emp table and display the inserted data from table It requires 2 files 1. S1.html (Html file to take values from user) 2. s2.jsp (jsp file to insert data in database and to display the inserted data) JDBC Connectivity with JSPExample 2- JSP Code for insert data in emp table and display the inserted data from table

S1.html

Enter Employee No
Enter Empoyee Name

JDBC Connectivity with JSPExample 2- JSP Code for insert data in emp table and display the inserted data from table S2.jsp <%@ page import ="java.sql.*" %> <%@ page import ="javax.sql.*" %> <% Class.forName("com.mysql.jdbc.Driver"); java.sql.Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3307/db1","root","root")

; Statement st= con.createStatement(); int id= Integer.parseInt(request.getParameter("t1")); String name=request.getParameter("t2"); st.executeUpdate("insert into emp values("+id+",'"+name+"')"); JDBC Connectivity with JSPExample 2- JSP Code for insert data in emp table and display the inserted data from table ResultSet rs=st.executeQuery("select * from emp"); out.print("

Employee Database

"); out.print("

ENo Ename
"); out.print(rs.getInt(1)); out.print(" "); out.print(rs.getString(2)); out.print("
");

out.print("

"); while(rs.next()) { out.print("

"); out.print("

"); out.print("

"); out.print("

");

} %> References

https://www.tutorialspoint.com/jsp/jsp_overview.htm https://www.javatpoint.com/jsp-tutorial https://www.studytonight.com/jsp/introduction-to-jsp.php https://www.studytonight.com/jsp/creating-a-jsp-page.php https://www.studytonight.com/jsp/jsp-scripting-element.php https://www.javatpoint.com/java-jdbc

Recently Viewed Presentations

  • Detection of Influenza A on Fomites.

    Detection of Influenza A on Fomites.

    Caulerpa taxifolia Inhibited FIV in vitro Blue-green algae (allophycocyanin) Neutralized enterovirus 71 cytopathic effect in vitro Blue-green algae (Cyanovirin-N) Inactivates HIV Spirulina platensis Inhibits HIV activity Spirulina platensis (calcium spirulan) Inhibits HSV-1, human cytomegalovirus (HCMV), measles virus, mumps virus, influenza...
  • NOMENCLATURE C2.4 Organic Compounds NAMING COMPOUNDS There is

    NOMENCLATURE C2.4 Organic Compounds NAMING COMPOUNDS There is

    Find the longest carbon chain - this is the basis of the name (eth, meth, pent etc). Number the C atoms in the chain, giving the end that has any side chains or functional groups the smallest numbers possible. If...
  • High Performance, Federated, Service-Oriented Geographic Information Systems Ahmet

    High Performance, Federated, Service-Oriented Geographic Information Systems Ahmet

    Federated data sets are defined under the tags called "layer" with attribute cascaded (i.e. federated) set to 1. Since Federator is an extended WMS, aggregated capability is an extension to WMS's standard capability. Federator publishes these data sets as if...
  • &quot;The Scarlet Ibis&quot; Discussion notes - Quia

    "The Scarlet Ibis" Discussion notes - Quia

    Allusion- literary term Allusion- a reference in a work of literature to a well-known character, place, or situation from literature, music, mythology, film, religion (especially the Bible), art, or history. The Family Guy frequently uses allusions. Ex. This scene is...
  • Chapter 7: IP Addressing CCNA Routing and Switching

    Chapter 7: IP Addressing CCNA Routing and Switching

    Translation - Network Address Translation 64 (NAT64) allows IPv6-enabled devices to communicate with IPv4 devices. ... it is the link-local address of the router that is used as the default gateway for other devices on the link. ... Cisco IOS...
  • Clinical Examination of the Ear, Nose and Throat

    Clinical Examination of the Ear, Nose and Throat

    ENT examination/equipment is not intuitive. Be familiar with gear. THE EAR. Position. Good light. Headlight / reflected light from headmirror. Side on to patient. Inspect, Palpate, Use otoscope. ... Clinical Examination of the Ear, Nose and Throat Last modified by:
  • Kewarganegaraan 11 - upnjatim.ac.id

    Kewarganegaraan 11 - upnjatim.ac.id

    Ttp, policy principles ini dpt diterjemahkan scr berlainan oleh lembaga2 pemerintahan yg berbeda2 (meski sebenarnya ini tdk boleh terjadi) Kenyataan ini dpt dilihat pd kasus pemerintahan hsl Pemilu 2004, khususnya antara DPR & Presiden. Ini tentu memp implikasi baik positif...
  • Mod262 - Amazon Web Services

    Mod262 - Amazon Web Services

    Existing FM situation: Up to 200GWh/d capacity shortfall due to the rejection of NG's planning application for a PRI in Corse / Tirley area. Second Public Enquiry held July 2010 and awaiting Sec of State decision (DECC / C&LG joint...
ENo Ename
"); out.print(rs.getInt(1)); out.print(" "); out.print(rs.getString(2)); out.print("