JSP - Prof-Matteo-Palitto-Peano/Web-Services GitHub Wiki

Java Server Page

Una pagina JSP è costituita da markup (X)HTML frammentato da sezioni di codice Java.

Il codice Java e' inserito nella pagina HTML delimitandolo da appositi TAGs:

Dichiarazione

<%! codice %>

possono essere utilizzate sia per le variabili che per metodi.

Scriptlet

<% codice %>

Dove codice in questo caso puo' essere qualsiasi istruzione Java.

Esempio:

<div class="w3-card-4">
    <% if ( sex.equals("lui") ) { %>
  <header class="w3-container w3-blue">
          <% } else { %>
  <header class="w3-container w3-pink">
          <% } %>
    <div id="userList" style="white-space: nowrap"> <h4>User List: </h4> </div>
  </header>

Espressioni

<%= espressione java%>

L'espressione verra' valutata e il risultato convertito in stringa e inserito nella pagina HTML

<h2> connesso con il server all'indirizzo IP: <%= request.getRemoteHost() %></h2>

NOTA: L'uso nell'espressione precedente della variabile implicita request

Variabili Implicite

JSP mette a disposizione degli sviluppatori in ogni pagina delle variabili che possono essere usate senza essere esplicitamente dichiarate.

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 of server-specific features like higher performance JspWriters.

8

page

This is simply a synonym for this, and is used to call the methods defined by the translated servlet class.

9

Exception

The Exception object allows the exception data to be accessed by designated JSP.

Per ognuno di questi oggetti sono associati dei metodi, per esempio consideriamo l'oggetto request

S.No. Method & Description
1

Cookie[] getCookies()

Returns an array containing all of the Cookie objects the client sent with this request.

2

Enumeration getAttributeNames()

Returns an Enumeration containing the names of the attributes available to this request.

3

Enumeration getHeaderNames()

Returns an enumeration of all the header names this request contains.

4

Enumeration getParameterNames()

Returns an enumeration of String objects containing the names of the parameters contained in this request.

5

HttpSession getSession()

Returns the current session associated with the this request, or if the request does not have a session, creates one.

6

HttpSession getSession(boolean create)

Returns the current HttpSession associated with the this request or, if if there is no current session and create is true, returns a new session.

7

Locale getLocale()

Returns the preferred Locale that the client will accept content in, based on the Accept-Language header.

8

Object getAttribute(String name)

Returns the value of the named attribute as an Object, or null if no attribute of the given name exists.

9

ServletInputStream getInputStream()

Retrieves the body of the request as binary data using a ServletInputStream.

10

String getAuthType()

Returns the name of the authentication scheme used to protect the servlet, for example, "BASIC" or "SSL," or null if the JSP was not protected.

11

String getCharacterEncoding()

Returns the name of the character encoding used in the body of this request.

12

String getContentType()

Returns the MIME type of the body of the request, or null if the type is not known.

13

String getContextPath()

Returns the portion of the request URI that indicates the context of the request.

14

String getHeader(String name)

Returns the value of the specified request header as a String.

15

String getMethod()

Returns the name of the HTTP method with which this request was made, for example, GET, POST, or PUT.

16

String getParameter(String name)

Returns the value of a request parameter as a String, or null if the parameter does not exist.

17

String getPathInfo()

Returns any extra path information associated with the URL the client sent when it made this request.

18

String getProtocol()

Returns the name and version of the protocol the request uses.

19

String getQueryString()

Returns the query string that is contained in the request URL after the path.

20

String getRemoteAddr()

Returns the Internet Protocol (IP) address of the client that sent the request.

21

String getRemoteHost()

Returns the fully qualified name of the client that sent the request.

22

String getRemoteUser()

Returns the login of the user making this request, if the user has been authenticated, or null if the user has not been authenticated.

23

String getRequestURI()

Returns the part of this request's URL from the protocol name up to the query string in the first line of the HTTP request.

24

String getRequestedSessionId()

Returns the session ID specified by the client.

25

String getServletPath()

Returns the part of this request's URL that calls the JSP.

26

String[] getParameterValues(String name)

Returns an array of String objects containing all of the values the given request parameter has, or null if the parameter does not exist.

27

boolean isSecure()

Returns a boolean indicating whether this request was made using a secure channel, such as HTTPS.

28

int getContentLength()

Returns the length, in bytes, of the request body and made available by the input stream, or -1 if the length is not known.

29

int getIntHeader(String name)

Returns the value of the specified request header as an int.

30

int getServerPort()

Returns the port number on which this request was received.

⚠️ **GitHub.com Fallback** ⚠️