Upload
profbnk
View
163
Download
0
Embed Size (px)
Citation preview
JavaServer PagesIDE: Netbeans
Server: GlassFish 4.1
M I T
Prof B N Kshirsagar
www.mit.asia
Oracle Workforce Development Program
NetBeans
First program “newjsp.jsp”
GlassFish Server
Deploying Web App “WebApplication1”
Running “newjsp.jsp”
How code works<%-- Document : newjsp Created on : Sep 14, 2015, 8:47:33 AM Author : B N Kshirsagar--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%><!DOCTYPE html><html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP Page</title> </head> <body> <%@page import="java.util.*" %> <h1>Hello World!</h1> <br> The time is now <%= new Date() %> ${1+3} </body></html>
Systemproperties.jsp
Output : systemproperties.jsp
Code : systemproperties.jsp <body>
<h1>Your Computer System Properties</h1> <br> Java version is <%= System.getProperty("java.version") %> <hr> OS is <%= System.getProperty("os.name") %> <hr> Java Home is <%= System.getProperty("java.home") %> <hr> User name is <%= System.getProperty("user.name") %> <hr> User Home is <%= System.getProperty("user.home") %> <hr> User dir is <%= System.getProperty("user.dir") %> </body>
Scriptlet <% ... %>
Scriptletdate.jsp
Code : scriptletdate.jsp <body> <%@page import="java.util.*" %> <% // This is a scriptlet. Notice that the "date" // variable we declare here is available in the // embedded expression later on. System.out.println( "Evaluating date now" ); java.util.Date date = new Date(); int a = 10; int b = 20; int c = a + b; %> Hello! The time is now <%= date %> <hr> The value of a <%= a %> <hr> The value of b <%= b %> <hr> The value of sum of and b i.e. c <%= c %> <hr>
Scriptlet + HTML : MIXING
Output : scriptletif.jsp
Code : scriptletif.jsp <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP Page</title> </head> <body>
<% boolean hello = true ; if ( hello ) { %> <P>Hello, world ! <% } else{ %> <P>Goodbye, world <% } %> </body> </html>
HTML inside Scriptlet !
“scriptlethello.jsp” – output
Code : scriptlethello.jsp<%-- Document : scriptlethello.jsp Created on : Oct 2, 2015, 10:16:48 AM Author : B N Kshirsagar--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%><!DOCTYPE html><% out.println("<HTML>"); out.println("<Title> This is scriptlet</Title>"); out.println("<Body>"); out.println("Hello! The time is now ..."); out.println("</Body>"); out.println("</HTML>"); %>
Scriptlettable.jsp
Scriptlettable.jsp : output
code : scriptlettable.jsp <body> <h1>Hello World!</h1> <center> <TABLE BORDER=2><% for ( int i = 0; i < 5; i++ ) { %> <TR> <TD>Number</TD> <TD><%= i+1 %></TD> </TR> <% }%></TABLE>
Directives <%@ ... %>
Code : directives.jsp<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP Directive Page</title> </head> <body> <h1>Directives !</h1>
<%@ page import="java.util.*" %>
<% System.out.println( "Evaluating date now" ); Date date = new Date(); %> Hello! The time is now <%= date %> </body></html>
Directiveinclude.jsp
Code : directiveinclude.jsp <body> <h1>Include Directive page !</h1> <h2>This program uses JSP include directive , it includes file systemproperties.jsp </h2> <br> Going to include systemproperties.jsp...<BR> <%@ include file="systemproperties.jsp" %>
</body>
Declaration <%! ... %>
Code : declaration.jsp <body> <h1>JSP Declarations !</h1> <%@ page import="java.util.*" %>
<%! Date theDate = new Date(); Date getDate() { System.out.println( "In getDate() method" ); return theDate; } Date computeDate() { System.out.println("Re-Initializing date " ); theDate = new Date(); return theDate; } %> <%-- Hello! The time is now <%= getDate() %> --%>
Hello ! New time is <%= computeDate() %>
Declaration.jsp output
JSP tags <jsp:forward ..../>
Code : tag.jsp <body> <h1>JSP tags !</h1> Going to include datepage.jsp...<BR>
<% boolean logic = true; if (logic) { out.println("Logic is true"); %> <jsp:forward page="datepage.jsp"/> <% } else { out.println("Logic is false"); %> <jsp:include page="datepage.jsp"/> <% } %> </body>
Session
Output : “sessionindex.jsp”
Code : sessionindex.jsp<body> <h1>Session form !</h1>
<FORM METHOD=POST ACTION="SaveName.jsp">
What's your name? <INPUT TYPE=TEXT NAME=username SIZE=20><br>
Age <input type="number" name="age" size="3"> <P><INPUT TYPE=SUBMIT> </FORM> </body>
Action page : SaveName.jsp
Code: SaveName.jsp <body> <h1>Session Saving Page!</h1> <% String name = request.getParameter("username"); session.setAttribute("theName", name); String agenew = request.getParameter("age"); session.setAttribute("theAge", agenew); %> <a href="NextPage.jsp">Continue </a> </body>
Continue : NextPage.jsp
Code: NextPage.jsp<body> <h1>Session parameter used !</h1> Hello, <%=
session.getAttribute("theName") %> (<%= session.getAttribute("theAge") %>)
</body>
Data from user
Saving user data
Greeting user with session data
Form handling with beans
SaveNamedetails.jsp
Nextpagebean.jsp
bean
Bean : UserData.java package user;
public class UserData { String username; String email; int age;
public void setUsername( String value ) { username = value; }
public void setEmail( String value ) { email = value; }
public void setAge( int value ) { age = value; }
public String getUsername() { return username; }
public String getEmail() { return email; }
public int getAge() { return age; }}
classpath
classpathCreate folder “user” in WebAppication1 folder , Copy bean i.e. “UserData.java” inside “user”
folder
Classpath setting: Project(WebApp)>>Properties>>Libraries>
>Compile tab>>add jar/folder ..... Navigate
“user” folder >> ok Restart GlassFish Server ( WebServer )Run application
Output : getName.jsp
Form values
SaveNamedetails.jsp ... Using bean
Values retrieval ... Using bean
Code: SaveNamedetails.jsp<body> <h1>Hello World!</h1> <jsp:useBean id="user" class="user.UserData"
scope="session"/> <jsp:setProperty name="user" property="*"/>
<A HREF="nextpagebean.jsp">Continue</A>
</body>
Code: nextpagebean.jsp<body> <h1>Hello World!</h1> <jsp:useBean id="user" class="user.UserData" scope="session"/> You entered<BR> Name: <%= user.getUsername() %><BR> Email: <%= user.getEmail() %><BR> Age: <%= user.getAge() %><BR>
</body>
Thank you
Contact: Prof Rahul Mapari
MIT- Oracle Workforce Development ProgramDepartment of Computer Science & Engineering
G.S.Mandal’s Maharashtra Institute of Technology, Aurangabad, MS, INDIA