Java Server Pages - … · 3/10/2018 · Standard Actions 5 JSP Action Tags Description jsp:forward...

20
Java Server Pages JSP Part II

Transcript of Java Server Pages - … · 3/10/2018 · Standard Actions 5 JSP Action Tags Description jsp:forward...

Page 1: Java Server Pages - … · 3/10/2018 · Standard Actions 5 JSP Action Tags Description jsp:forward forwards the request and response to another resource. jsp:include includes another

Java Server PagesJSP Part II

Page 2: Java Server Pages - … · 3/10/2018 · Standard Actions 5 JSP Action Tags Description jsp:forward forwards the request and response to another resource. jsp:include includes another

• Actions

• Beans

• JSP & JDBC

• MVC

2

Agenda

Page 3: Java Server Pages - … · 3/10/2018 · Standard Actions 5 JSP Action Tags Description jsp:forward forwards the request and response to another resource. jsp:include includes another

Components

• Scripting Elements

• Directives

• Implicit Objects

• Actions

3

Page 4: Java Server Pages - … · 3/10/2018 · Standard Actions 5 JSP Action Tags Description jsp:forward forwards the request and response to another resource. jsp:include includes another

Actions• Actions are XML-syntax tags used to control the servlet engine

• Action elements are basically predefined functions

• <jsp:action_name attribute="value" />

4

Page 5: Java Server Pages - … · 3/10/2018 · Standard Actions 5 JSP Action Tags Description jsp:forward forwards the request and response to another resource. jsp:include includes another

Standard Actions

5

JSP Action Tags Description

jsp:forward forwards the request and response to another resource.

jsp:include includes another resource.

jsp:useBean creates or locates bean object.

jsp:setProperty sets the value of property in bean object.

jsp:getProperty prints the value of property of the bean.

jsp:plugin embeds another components such as applet.

jsp:param sets the parameter value. It is used in forward and include mostly.

jsp:fallback can be used to print the message if plugin is working. It is used in jsp:plugin.

Page 6: Java Server Pages - … · 3/10/2018 · Standard Actions 5 JSP Action Tags Description jsp:forward forwards the request and response to another resource. jsp:include includes another

include directive and include action

6

JSP include directive JSP include action

includes resource at translation time. includes resource at request time.

better for static pages. better for dynamic pages.

includes the original content in the generated servlet. calls the include method.

Page 7: Java Server Pages - … · 3/10/2018 · Standard Actions 5 JSP Action Tags Description jsp:forward forwards the request and response to another resource. jsp:include includes another

Java Beans• A component architecture for Java.

• Useful in JSP for encapsulating application logic and transferring data between pages.

• Beans are simply Java classes.

7

Page 8: Java Server Pages - … · 3/10/2018 · Standard Actions 5 JSP Action Tags Description jsp:forward forwards the request and response to another resource. jsp:include includes another

JavaBeans for programmersBeans must:

• Implement java.io.Serializable

• Have a constructor with no argument

Properties (a.k.a. alterable variables) must:

• Must have a get and set

• Must be private attributes

8

Page 9: Java Server Pages - … · 3/10/2018 · Standard Actions 5 JSP Action Tags Description jsp:forward forwards the request and response to another resource. jsp:include includes another

JavaBeans example

package mybeans;

import java.io.Serializable;

public class NameBean implements Serializable {

private String fullname;

public NameBean() {

super();

name = new String("World");

}

public void setFullname( String fullname ) {

this.fullname = fullname;

}

public String getFullname() {

return fullname;

}

}

9

Page 10: Java Server Pages - … · 3/10/2018 · Standard Actions 5 JSP Action Tags Description jsp:forward forwards the request and response to another resource. jsp:include includes another

<jsp:useBean>• Locates or instantiates a JavaBeans components.

<jsp:useBean id=“beanInstanceName”

scope=“page| request | session |

application”

class=“package.class” />

Example:

10

<jsp:useBean id=“myName” scope=“page”

class=“mybeans.NameBean”/>

Page 11: Java Server Pages - … · 3/10/2018 · Standard Actions 5 JSP Action Tags Description jsp:forward forwards the request and response to another resource. jsp:include includes another

<jsp:setProperty>• Sets the value of one or more properties in a Bean, using the Bean’s setter

methods.

• Declare the Bean with <jsp:useBean> first

<jsp:setProperty name=“beanInstanceName”

property=“propertyName”

value=“string” />

Example:

11

<jsp:useBean id=“myName”scope=“page”

class=“mybeans.NameBean”/>

<jsp:setProperty name=“myName”

property=“fullname” value=“abc”/>

Page 12: Java Server Pages - … · 3/10/2018 · Standard Actions 5 JSP Action Tags Description jsp:forward forwards the request and response to another resource. jsp:include includes another

<jsp:getProperty>• Gets a Bean property value, using the Bean’s getter methods.

• Declare the Bean with <jsp:useBean>

<jsp:getProperty name=“beanInstanceName”

property=“propertyName” />

Example:

12

<jsp:useBean id=“myName”scope=“page”

class=“mybeans.NameBean”/>

<H1>My Name <jsp:getProperty name=“myName”

property=“fullname”/>

</H1>

Page 13: Java Server Pages - … · 3/10/2018 · Standard Actions 5 JSP Action Tags Description jsp:forward forwards the request and response to another resource. jsp:include includes another

Expression Language (EL)Simplifies the accessibility of data stored in the Java Bean component, and other objects like

request, session, application etc.

${ expression }

13

<form action="process.jsp">

Enter Name:<input type="text" name=“input" /><br/><br/>

<input type="submit" value="go"/>

</form>

Welcome, ${ param.input }

Page 14: Java Server Pages - … · 3/10/2018 · Standard Actions 5 JSP Action Tags Description jsp:forward forwards the request and response to another resource. jsp:include includes another

Implicit Objects in EL

14

Implicit Objects Usage

pageScope it maps the given attribute name with the value set in the page scope

requestScope it maps the given attribute name with the value set in the request scope

sessionScope it maps the given attribute name with the value set in the session scope

applicationScope it maps the given attribute name with the value set in the application scope

param it maps the request parameter to the single value

paramValues it maps the request parameter to an array of values

header it maps the request header name to the single value

headerValues it maps the request header name to an array of values

cookie it maps the given cookie name to the cookie value

initParam it maps the initialization parameter

pageContext it provides access to many objects request, session etc.

Page 15: Java Server Pages - … · 3/10/2018 · Standard Actions 5 JSP Action Tags Description jsp:forward forwards the request and response to another resource. jsp:include includes another

Expression Language (EL)

15

<%

session.setAttribute("user",“abc");

%>

Value is ${ sessionScope.user }

<%

Cookie ck=new Cookie("name",“xyz");

response.addCookie(ck);

%>

Hello, ${cookie.name.value}

Page 16: Java Server Pages - … · 3/10/2018 · Standard Actions 5 JSP Action Tags Description jsp:forward forwards the request and response to another resource. jsp:include includes another

JSP & JDBC<%@page import="java.sql.*,java.util.*"%><html> <body>

<%try {

Class.forName("com.mysql.jdbc.Driver");Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/students", "root",

"password");Statement stmt = con.createStatement();ResultSet rs = stmt.executeQuery("select * from btcse");

%><table border=1 bgcolor="orange"><%

while (rs.next()) {%>

<tr><td><%=rs.getString("ENROLLMENT_NUMBER")%></td><td><%=rs.getString("STUDENT_NAME")%></td>

</tr><%

}con.close();

} catch (Exception e) {System.out.println(e);

}

%></table></body></html>

16

Page 17: Java Server Pages - … · 3/10/2018 · Standard Actions 5 JSP Action Tags Description jsp:forward forwards the request and response to another resource. jsp:include includes another

MVC: Model, View, Controller• Model 1

• Model 2

17

Page 18: Java Server Pages - … · 3/10/2018 · Standard Actions 5 JSP Action Tags Description jsp:forward forwards the request and response to another resource. jsp:include includes another

MVC

18

Page 19: Java Server Pages - … · 3/10/2018 · Standard Actions 5 JSP Action Tags Description jsp:forward forwards the request and response to another resource. jsp:include includes another

References

• https://docs.oracle.com/javaee/5/tutorial/doc/bnagx.html

• https://www.javatpoint.com/jsp-tutorial

19

Page 20: Java Server Pages - … · 3/10/2018 · Standard Actions 5 JSP Action Tags Description jsp:forward forwards the request and response to another resource. jsp:include includes another

Thank you

20