JSP dynamic web page programming (I)

JSP (JavaServer Pages):

JSP pages from the traditional form of web pages is HTML file to add Java program fragment (Scriptlet), and JSP tags. Servlet / JSP container received a request issued by the client, the first implementation of the program in which fragments, and then implementation of the results to HTML format to respond to the client.

JSP Basic syntax:

Note:

1, the client can see the comments :<!-- comment [<% = expression%>] -> (you can add annotations dynamic expression)

2, developing a dedicated programmer annotations :<%-- comment -%> or <%/** this is a comment **/%>

Statement:

<%! declaration; [declaration ;]+...%> the declaration of variables for the class variable. Means: If the same time, there are n users in the implementation of this JSP page, they will share this variable. Will cause trouble on the thread synchronization. (Not recommended)

A small script:

<% Java code% "the definition of variables such as local variables, there is no multi-threading synchronization problem is thread-safe.

Expression:

<% = expression%> can not use ";" as an expression of the end of the symbol.

JSP directive elements:

Instead we are told how the JSP engine to compile JSP pages.

page directive: to set the JSP page attributes and related functions.

Syntax: <% @ page attribute1 = "value1" attribute2 = "value2" ...%>

<jsp:directive.page attribute1="value1"...>

For example:

<% @ page contentType = "text / html; charset = utf-8" language = "java" import = "java.net .*"%>

include directives: used to indicate the JSP compiler JSP container insert a file that contains text or code, this contains the process is static. Note that include files should avoid the use of <html> <body>, may lead to errors.

Syntax: <% @ include file = "relativeURLspec"%>

<jsp:directive.include file="relativeURLspec">

For example:

<% @ include file = "Hello.html">

taglib directive:

Syntax: <% @ taglib uri = "tagLibraryURI" prefic = "tagPrefix"%>

<jsp:directive.taglib uri="tagLibraryURI" prefic="tagPrefix">

JSP action elements:

<jsp:include>

<jsp:forward>

<jsp:param>

<jsp:useBean>

<jsp:setProperty>

<jsp:getProperty>

-------------------------------------------------- -------------------------------------------------- --------

request the submission of objects and network information:

JSP provides request object to receive the client to send back data.

URL General Syntax: protocol: / / host: port / virtualPath? QueryString

protocol: that the agreement, the statement in the remote machine is used to transfer information between the low-level mechanism (http ftp https, etc.).

host: that the request should be sent to the remote machine name or ip address.

port: the statement requests the server to be listening port number machine.

virtualPath: containing a slash as the separator of a group identifier, the server it is mapped to a physical path and JSP location.

querString: is the query string, which is a paired list of names and values, as a JSP parameters are passed to the JSP pages to deal with it. If there are multiple parameters need to pass, will be "&" was separated from the name and value pairs.

For example: http://localhost:8080/root/request.jsp?p1=val1&p2=val2

request object provides methods:

Access path and protocol information:

String getProtocol () Get the protocol used for communication and version number (eg: HTTP / 1.1)

String getScheme () Get the request protocol name (eg: HTTP)

String getPathInfo () Get the request path and query string in a JSP additional information between the

String getContextPath ()

String getRequestURI ()

String getServletPath ()

String getRealPath (String path)

Access to the host and port information:

String getServerName () to obtain respond to the request of the server name.

int getServerPort () to obtain respond to the request of the server-side host port number.

String getLocalName () Get the server to respond to the request host name.

String getLocalAddr () to obtain respond to the request of the server-side address.

int getLocalPort () to obtain respond to the request of the server-side port.

String getRemoteAddr () to obtain the requesting client IP address.

String getRemoteHost () to obtain the requesting client host name.

int getRemotePort () to obtain the requesting client host port.

Get the query string:

String getQueryString () for get () method to pass the parameter string that is URL,? The back part.

Get the query string variable value:

String getParameter (String name) Get the client sends to the server-side parameter values.

Enumeration getParameterNames () Returns a collection of all the parameters in the request.

String [] getParameterValues (String name) specified in the request parameters to obtain all values.

For example:

http://localhost:8080/liuxl/jsp/querystring.jsp?str=JSP

String s = request.getParameter ( "str"); to obtain the value of the variable str, s, the value of post-implementation "JSP".

Note: getParameter () method returns the value can only be a string. If the transmission val = 100, then the need for type conversion.

if(request.getParameter("val")!=null) // To determine whether a string is empty  
{    
        num=Integer.parseInt(request.getParameter("val")); // Converts a string into an integer  
}
else
{
        num=0;
}



Form and its application in information transmission in the application:

Form:

Is a form element can contain the regional, HTML for the form to provide a variety of graphical user interface component elements (such as text boxes, password boxes, pull-down menus, etc.), these form elements allow users to enter information in the form , you can use them to constitute a form content, as a user can enter the domain, and can be submitted to a JSP for processing.

Format:

<form name="" action="" method="">

...

</ form>

Where: name is used to specify the name of the form. action is used to specify when the form is submitted to perform the action. method to pass the request to the JSP specifies the method used (GET, POST, PUT, commonly used POST).

1, <input> label commonly used in the input information or submit information

Syntax: <input type="" name="" value="">

Description: type allows you to specify input type (text (text box), password (the password box), checkbox (check box), radio (radio button), image (image domain), reset (reset button), submit (submission button)). name for the form element's name. value for the form element's initial value.

For example:

Text Box: <input name="" type="text" value="" size="" maxlength="">

Password box: <input name="" type="password" value="" size="" maxlength="">

The submit button: <input name="" type="submit" value="">

Reset Button: <input name="" type="reset" value="">

Radio button: <input name="" type="radio" value="" checked>

(For a set of options to select from one of the options, checked is an optional attribute, if the property, then the button is displayed as being selected, otherwise it is not selected.)

eg: <input name="radiobutton" type="radio" value="1" checked> Option 1

<input name="radiobutton" type="radio" value="2"> Option 2

In the case of a selected button to submit URL is as follows :...? radiobutton = 1 ...

Checkbox: <input name="" type="checkbox" value="" checked>

(Used from a set of options to select multiple options, checked ibid. And similar to radio buttons, check boxes into groups often use radio buttons with the difference is that a number of selected check boxes will be been submitted, so the name can be the same (according to an array of treatment) may also be different (different variables for processing))

eg: <input name="checkbox" type="checkbox" value="swim"> swim

<input name="checkbox" type="checkbox" value="run"> run

If the submission, both are selected, then the URL format is as follows :...? checkbox = swim & checkbox = run ...

2, <textarea> tag declares a user can enter multiple lines of text area.

Syntax: <textarea name="" rows="" cols=""> text </ textarea>

Description: cols for the text field width (number of characters), rows for the text field's height (rows)

3, <select> tag declares a list of available options, the user can select one or more options.

Syntax: <select name="" size="" multiple>

<option value="" selected> option </ option>

...

<option value=""> option </ option>

</ select>

Note: name is the name, size is a list of regional height (to display the number of options), multiple is an optional attribute, if that can choose a number. selected is an optional attribute, it indicates that the option is set to is selected.
  • del.icio.us
  • StumbleUpon
  • Digg
  • TwitThis
  • Mixx
  • Technorati
  • Facebook
  • NewsVine
  • Reddit
  • Google
  • LinkedIn
  • YahooMyWeb

Related Posts of JSP dynamic web page programming (I)

  • JAVA EE JSP_JNDI

    dsfdsa http://lindows.javaeye.com/admin/blogs/213348 Tomcat 6 with the connection pool data source configuration http://www.blogjava.net/ec2008/archive/2008/07/19/216063.html project: test Driver path: D: \ workspace \ test \ WebRoot \ WEB-INF \ lib ...

  • Hibernate connection pool configuration

    Hibernate connection pool configuration <! - Jdbc -> <property name="connection.driver_class"> oracle.jdbc.driver.OracleDriver </ property> <property name="connection.url"> jdbc: oracle: thin: @ 10.203.14.132:15

  • In the servlet use Bean

    According to Sun's definition, JavaBean is a reusable software components. In fact JavaBean is a Java class, through the package into a property and methods of treatment of a function or a business object, referred to as bean. Because JavaBean is ...

  • hibernate generic generic DAO

    package org.lzpeng.dao; import java.io.Serializable; import java.util.List; import org.hibernate.Criteria; import org.hibernate.Query; import org.hibernate.criterion.Criterion; import org.springside.modules.orm.hibernate.Page; /** * * @version 2009-1-10 *

  • Servlet brief introduction

    Servlet brief introduction: Servlet is a small application server Are used to complete the B / S architecture, the client requests the response to treatment Platform independence, performance, able to run thread Servlet API for Servlet provides the s ...

  • Spring2.0 + hibernate3.1 + log4j + mysql demo

    applicationContext.xml Non-attachment jar package, necessary friends can send an email to todd.liangt @ gmail.com

  • Struts2 + hibernate + spring problem user log in

    dao layer services layer action jsp <tr> <td align="center"> <b> user name: </ b> </ td> <td> <s: textfield name = "czyNumber" cssClass = "textstyle" theme = "simple" size = &q

  • Hibernate secondary cache

    Hibernate cache: 2-bit cache, also known as process-level cache or SessionFactory level cache, secondary cache can be shared by all of the session Cache configuration and the use of: Will echcache.xml (the document code in hibernate package directory ...

  • The level Hibernate cache

    Hibernate cache level: (1) a cache is very short and the session life cycle consistent, also known as session-level cache-level cache or transaction-level cache (2) Ways of Supporting level cache: get (); load (); iterator (); only entity object cach ...

  • Hibernate's lazy strategy

    hibernate Lazy strategy can be used in: <class> tag, it can be true / false Tags can <PROPERTY> values true / false type of necessary tools to enhance <set> <list> can tag values true / false / extra <many-to-one> <on ...

blog comments powered by Disqus
Recent
Recent Entries
Tag Cloud
Random Entries