/ W3SCHOOLS

W3schools - JSP_Action

이 페이지는 다음에 대한 공부 기록입니다
Lecture에서 배웠던 내용을 복습하며 작성했습니다

찾으시는 정보가 있으시다면
주제별reference를 이용하시거나
우측 상단에 있는 검색기능을 이용해주세요

Actions

Use constructs in XML syntax to control the behavior of the servlet engine

Can dynamically insert a file, reuse JavaBeans components, forward the user to another page, or generate HTML for the Java plugin

<jsp:action_name attribute = “value”/>

Action elements are basically predefined functions

Syntax Purpose
jsp:include Includes a file at the time the page is requested
jsp:useBean Finds or instances a JavaBean
jsp:setProperty Sets the property of a JavaBean
jsp:getProperty Inserts the property of a JavaBean into the output
jsp:forward Forwards the requester to a new page
jsp:plugin Generates browser-specific code
that makes an OBJECT or EMBED tag for the Java plugin
jsp:element Defines XML elements dynamically
jsp:attribute Defines dynamically-defined XML element’s attribute
jsp:body Defines dynamically-defined XML element’s body
jsp:text Used to write template text in JSP pages and documents

Common Attribute

Two attributes that are common to all Action elements : id and scope

Id

  • Uniquely identifies the Action element, and allows the action to be referenced inside the JSP page

  • If the Action creates an instance of an object, the id value can be used to reference it through the implicit object PageContext

Scope

  • Identifies the lifecycle of the Action element

  • Id and Scope are directly related, as the Scope determines the lifespan of the object associated with the id

  • Scope has four possible values : page, request, session application

Include

<jsp:include page=“relative URL” flush=“true”/>

Unlike the include directive, which inserts the file at the time the JSP page is translated into a servlet, this action inserts the file at the time the page is requested

Page : The relative URL of the page to be include

Flush : The Boolean attribute determine whether the included resource has its buffer flushed before it is included

useBean

<jsp:useBean id = “name” class = “package.class”/>

Is quite versatile. It first searches for an existing object utilizing the id and scope variables

If an object is not found, it then tries to create the specified object

Once a bean class is loaded, Can use jsp:setProperty and jsp:getProgerty to modify and retrieve the bean properties

Class : Designates the full package name of the bean

Type : Specifies the type of the variable that will refer to the object

BeanName : Gives the name of the bean as specified by the instantiate () method of the java.beans.Beans class

setProperty

Sets the properties of a Bean, the Bean must have been previously defined before this action

In this case, setProperty is executed regardless of whether a new bean was instantiated or an existing bean was found

<jsp:useBean id = “name”……/>
<jsp:setProperty name = “name” property = “someProperty”…./>

In this case, setProperty is executed only if a new object was instantiated

<jsp:useBean id = “name” …… >
    <jsp:setProperty name = “name” property = “someProperty” …./>
</jsp:useBean>

Name : Designates the bean the property of which will be set

Property : Indicates the property you want to set, A value of “*” means that all request parameters whose names match bean property names will be passed to the appropriate setter methods

Value : Is to be assigned to the given property, if the parameter’s value is null or does not exist, the setProperty action is ignored

Param : Is the name of the request parameter whose value the property is to receive

getProperty

<jsp:useBean id = “name”…/>
<jsp:getProperty name = “name” property = “someProperty” .…/>

Is used to retrieve the value of a given property and converts it to a string, and finally inserts it into the output

Forward

<jsp:forward page = “Relative URL”>

Terminates the action of the current page and forwards the request to another resource such as a static page, another JSP page or a Java Servlet

Plugin

<jsp:plugin type = "applet" codebase = "dirname" code = "MyApplet.class" width = "60" height = "80">
    <jsp:param name = "fontcolor" value = "red" />
    <jsp:param name = "background" value = "black" />

    <jsp:fallback>
        Unable to initialize Java Plugin
    </jsp:fallback>
</jsp:plugin>

Is used to insert Java components into a JSP page

It determines the type of browser and inserts the <object> or <embed> tags as needed

If the needed plugin is note present, it downloads the plugin and then executes the Java component

The Java component can be either and Applet or a JavaBean

<fallback> can be used to specify an error string to be sent to the user in case the component fails

Element, attribute, body

Are used to define XML elements dynamically

It means that the XML elements can be generated at request time rather than statically at compile time

<body>
    <jsp:element name = "xmlElement">
        <jsp:attribute name = "xmlElementAttr">
            Value for the attribute
        </jsp:attribute>
        <jsp:body>
            Body for XML element
        </jsp:body>
    </jsp:element>
</body>

<!-- this would produce the following HTML code at run time -->
<body>
    <xmlElement xmlElementAttr = "Value for the attribute">
        Body for XML element
    </xmlElement>
</body>

Text

<jsp:text>Template data </jsp:text>

Can be used to write the template text is JSP pages and documents

The body of the template can’t contain other elements;

It can only contain text and EL expressions(EL are explained in a subsequent chapter)

An alternative is to embed the value in a CDATA section

<jsp:text><![CDATA[<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">]]></jsp:text>
    <head><title>jsp:text action</title></head>
    <body>
        <books><book><jsp:text>  
            Welcome to JSP Programming
        </jsp:text></book></books>
    </body>
</html>