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:


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 **/%>


<%! 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%> 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:







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

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:


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

Form and its application in information transmission in the application:


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.


<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.

分类:Java 时间:2010-03-29 人气:781
blog comments powered by Disqus


  • Disrupt the device using Proguard Java code protection 2010-12-22

    The security of Java code, the problem has always been, JVM mechanisms leading, Java is very easy to be decompiled. So that the other is easy to get your original program, even a little difference between anti-compiled, but the basic will not affect

  • jsp page using java code 2010-05-28

    Previously, I do not like jsp pages directly to use java code block, mosaic java code, but after work, use this situation, I changed the mind, in fact, it has the convenience of his office ah! <%@ page language="java" import="java.util.*

  • How to use the js code or Java code jsp tag 2011-05-05

    JSP tag is still very convenient, such as Struts, Spring and other JSP tags provided to us, you can use them to get some variable or calculation. For example struts2 of <s:url value="/admin/unmi.action"/> automatically for us in front of t

  • js and jsp variable in java code access solution for each variable 2010-07-29

    1. js variable for java jsp page code variable. Methods: var JS variable name = <% = JAVA variable name%> 2. java code for js variable. Description: In the JSP in; Java part is executed on the server; js part of the client's browser implementation;

  • Found during the use of it for the MyEclipse Java code for the failure of the auto-suggest, and for normal JSP was able to give prompt 2010-09-28

    Found during the use of it for the MyEclipse Java code for the failure of the auto-suggest, and for normal JSP was able to give tips. After a lot of Baidu Internet to find a solution, but because of different reasons for this phenomenon, so basically

  • MyEclipse 6.5 jsp and java code to modify the font size 2010-11-17

    1, jsp fonts English version of MyEclipse 5.5.1GA, select Preferences under the Windows menu, sub menu, open the Preferences dialog box on the left there is a General item, expand it, Below it there is a Appearence, select one of the "Colors and Font

  • java code programming optimization 2010-03-26

    Procedures for the use of available resources (memory, CPU time, network bandwidth, etc.) is limited, the purpose of optimization is to allow programs to use the least resources to complete the scheduled tasks. Optimization usually contains two aspec

  • Java language coding standards (Java Code Conventions 2009-01-05

    At the beginning of Notes ") http://www.huihoo.org/code/java_code_conventions.html - Package and the introduction of statements (see "package and the introduction of statements") - Class and interface declarations (see "classes and int

  • java Code Generation 2010-05-01

    1. Generated verification code: using technology jsp, servlet 2. Generated verification code in the servlet: package servlet; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.image.BufferedImage; import java.io.I

iOS 开发

Android 开发

Python 开发



PHP 开发

Ruby 开发






Javascript 开发

.NET 开发



Copyright (C) codeweblog.com, All Rights Reserved.

CodeWeblog.com 版权所有 闽ICP备15018612号

processed in 0.391 (s). 10 q(s)