JSP development guide

Source: Internet
Author: User
Tags website server

JavaServer Pages (JSP) is a Java-based cross-platform Web development language.

JSP is compatible with Microsoft's Active Server Pages, but it uses HTML-like labels and JavaProgramCodeSegment instead of VBScript. When your website server does not provide local ASP support, that is, Apache or Netscape Server, you can consider using JSP. Although you can obtain additional ASP configuration modules for these servers, they are expensive, at present, sun is not charged for the JSP components you need (although sun may be charged in the future ). These components are also easy to obtain for Solaris, Linux, and windows.

Do not confuse JSP with servo JavaScript. The website server automatically converts the Java code segment written in JSP into Java Servlets. Many functions that must be previously controlled by Perl handwriting programs or server-specific APIs (such as ASP) can also be automatically processed through JSP.

Now let's start to help you build an executable jsp sample Website.

Install your machine to use JSP

You will need Java 2 software development tool (jsdk). Its original names are Java Development Tool (JDK), JavaServer website development tool (jswdk), tomcat, or other network servers that support JSP. Sun provides jsdk and jswdk for Windows, Solaris, and Linux for free.

If you want to use JSP on your current network server, but the server itself does not support JSP and Java Servlets, you can try Allaire's JRun, it serves as an additional network server device for Netscape Enterprise Edition and FastTrack servers, Microsoft's Internet Information Server (IIS) and personal network server (PWS), Apache, and other servers. You can also use the Java version of Apache network server, which is available in the latest jswdk.

Download and install the required components
Currently, 1.2.2-001 and jsdk downloadable versions are in the form of installable compression files. The downloaded file is about 20 mb, providing a complete Java development environment, allowing you to build a Java solution that uses standard APIs as the core. However, the only thing your network server needs to apply to JSP is the Java compiler. To let the network server know the compiler location, set the environment variable java. Home to the jsdk installation directory. If you install and accept the default directory on Windows, add the code set java. Home = C: 1.2.2 to your autoexec. BAT file and restart the file.

After jsdk is installed, download and install jswdk or beta tomcat, and use Java as the main Apache network server. It doesn't matter where it is installed, but you can find it. In general, it will be placed in the upper-layer Directory, which allows you to replace the network server of jswdk or jsdk without moving other network servers. After you have installed this file, you can prepare to develop JSP.

After jswdk is correctly installed, run the startserver command file to activate the network server. The default communication port is 8080. After activating the server, you must check that all C has the correct installation tool. You can load any one of the sample JSP files (http: // localhost: 8080/examples/JSP /). If you can successfully execute an example file, you can know that you have set the software correctly. If you see an error message in the console window of the activation server, you need to solve this problem. The most common problem is that the environment variable java. Home is not set (or incorrect. To view the current environment settings, type set in DOS mode.

Start

Before interpreting JSP syntax, create a quick web page that displays the current date and time and save it as sample. jsp:

<HTML>
<Head>
<Title> first page </title>
</Head>
<Body>
<H3> today is:
<% = New java. util. Date () %>
</H3>
</Body>
</Html>.

Put this file and all your HTML and JSP pages in the webpage directory under your jswdk installation directory. you can go to http: // localhost: 8080/sample. JSP download this page. when you visit this webpage for the first time, the website server will translate JSP into Java Servlet program code, then you will see the current date and time.

Now that you have downloaded, installed, and built a good development environment, you are ready to understand the JSP syntax and build your own JSP-based solutions.

Basic Principles of JSP syntax

After the installation, we will discuss the JSP syntax. to be lazy, you can download the syntax card. If you are not familiar with Java programming, you may want to refer to Sun's user manual. However, website creators should not do too much Java development. except for a few phone calls, the Java program code that appears on your JSP page should minimize it;

Remember this, now let's take a look at the JSP compiler guidance and instruction components, and then we will explain the JavaBeans and internal objects. there are five types of JSP compiler guidance and instruction components. after JSP 1.0, most JSP files are included in a single tag with <% as the start %> as the end. the new JSP 1.1 Specification has been published, and it is also compatible with XML.

JSP compiler guide and instruction Components

Compiler instructions
<% @ Compiler indication %>

Statement
<%! Declaration %>

Expression
<% = Expression %>

Program code segment/small instruction
<% Program code snippet %>

Note
<% -- Comment -- %>

Compiler instructions

The JSP compiler indicates the JSP Engine. They do not directly generate any visible output; instead, they tell the engine how to handle other JSP pages. They are always included in <% @? %> Volume label. The two main guidelines are page and include. We will not discuss the taglib compiler guidelines, but it can be used in jsp1.1 to create custom volume labels.

You can find page compiler instructions on the top of almost all your JSP pages. Although this is not necessary, it allows you to specify where to find the supported Java category:
<% @ Page import = "Java. util. Date" %>,

Where should the message be sent when a Java issue occurs:
<% @ Page errorpage = "errorpage. jsp" %>,

And you are? You need to manage the call period information for the user and may access multiple webpages (more discussion about the call period will be available in JavaBeans later ):
<% @ Page session = "true" %>.

The include compiler instructs you to divide your content into several manageable components, just like web pages with headers or footer. The webpage can be a fixed HTML webpage or a JSP webpage:
<% @ Include file = "filename. jsp" %>.

Announcement

JSP Declaration allows you to define the variables at the web page layer to store information or define supported functions, so that the rest of the JSP web page can be used. If you find that you have too many program codes, you 'd better put them in different Java classes. You can go to <%! ? %> Find the declaration in the volume label. Remember to add a semicolon after the variable declaration, just like any valid Java statement: <%! Int I = 0; %>.

Expression

JSP contains expressions. The evaluation expression results can be converted to strings and directly used on the output webpage. JSP operations belong to <% =? %> The volume label does not contain semicolons. It is useless to enclose the quotation marks.

<% = I %>
<% = "Hello" %>.

Program code segment/small instruction file

JSP program code snippets or small instruction files are included in <%? %> Volume label. When the Network Server accepts this request, the Java program code is executed. A small instruction file can be a raw HTML or XML file. Its internal program code snippets allow you to establish conditional execution program code, or just something that uses another program code. For example, the following program code combines expressions with small command files to display the string "hello" in the H1, H2, H3, and H4 labels ". A small instruction file is not limited to a line of original program code:

<% For (INT I = 1; I <= 4; I ++) {%>
<H <% = I %> Hello

<% }%>.

Note
The last major JSP component is embedded comments. Although you can include HTML comments in your file, if users view the original code of the webpage, they will also see these comments. If you do not want users to see your comments, you can place them in <% --? -- %> Volume label:

<% -- Comments for the servo -- %>.

JSP and JavaBean

Although you can put a large piece of program code in a small instruction file, most Java program code is a reusable component called JavaBean. Like ActiveX controls, JavaBean provides known functions and is designed for reuse at any time.

The value of Javabean is that it can be used through a set of features that provide access to the JavaBean settings. For example, this person is a JavaBean, and his name, Social Welfare Security number, and address can be characteristic. For JSP websites, you basically dynamically connect 'java' to your website.

Suppose that the JavaBean is created before the website is created. The first thing you need to do is to tell the JSP page that it needs to use the JavaBean. This can be done using the <JSP: usebean> volume label:
<JSP: usebean id = "localname" class = "com. jguru. Person" Scope = "application"/>.

<JSP: usebean> you must use the ID attribute to identify beans. here, you provide a name for the JSP webpage to identify beans. In addition to the ID attribute, you must also tell the webpage where to find the beans, or its Java category name. The category attribute provides how to locate it in various methods. The last required component is the scope attribute. with the help of the range attribute, You Can Tell Beans to ask for a single webpage (preset) [scope = "page"]; for a requested webpage [scope = "request"]; for the call period [scope = "session"]; or [scope = "application"] for the entire application to maintain its own information. for the call period range, you can easily maintain items on the JSP page, such as the shopping cart.

Once you declare a JavaBean, you can access its features to define it. To obtain a specific value, use the <JSP: getproperty> volume tag. With the <JSP: getproperty> label, you can specify the name of the bean to be used (from the ID field of usebean) and the value acquisition feature. Then, the real value will be placed in the output:
<JSP: getproperty id = "localname" property = "name"/>.

To change the JavaBean feature, you need to use the <JSP: setproperty> volume label. you also need to identify beans and the features to be corrected for this scale. In addition, you need to provide new values. if the name is correct, these can be obtained directly in the specified format:
<JSP: setproperty id = "localname" property = "*"/>;

To get this from a parameter, you must directly name this feature and the parameter:
<JSP: setproperty id = "localname" property = "Address" Param = "parametername"/>;

Or directly set it by name and value:
<JSP: setproperty id = "localname" property = "serialnumber" value = "string"/> or <JSP: setproperty id = "localname" property = "serialnumber" value = <% = expression %>/>.

Last point about JavaBean: To enable the network server to find JavaBean, you need to put their category files in a special location. For jswdk, the simplest thing is the class directory in the installation directory, such as the jswdk-1.0.1classes.

Internal JSP objects

The last component related to JSP syntax is called an internal object. In a small JSP instruction file, you can access these internal objects to interact with the servlet environment that executes JSP pages. Access to many internal objects should be simplified. However, these are examples, and their access is acceptable. To fully utilize internal object settings, you need to understand the latest Java Servlet API.

The following table lists the internal objects you can use.

Internal Object Description

Request
Client request, which contains parameters from the get/POST request

Response
Response from the webpage to the client

Pagecontext
The webpage attribute is managed here

Session
Request-related sessions

Application
Content being executed by Servlet

Out
Output stream used to send response

Config
Servlet architecture object

Page
JSP page itself

Exception
Exceptions not captured for error webpages

So what are these, and how should you use them? Basically, in your small instruction file, you can use them to access the servlet that executes JSP code. To avoid talking about too many details about servlet APIs, let's examine what you can do with them:

Without expressions, you can directly access the internal out object to print something to response:
<% Out. println ("hello"); %>.
You do not need to directly transmit parameters to JavaBean. You can obtain the parameter values through the request object:
<% String name = request. getparameter ("name"); Out. println (name); %>.
After you write a lot of applications using JSP, If you create a JavaBeans or find that you put too many original Java codes into your JSP file, you need to create a supported Java category to encourage repeated use and reduce the time required for JSP page conversion. To create a Java class, you must:

Add the bin directory of the jdswk installation directory to your path. Add C: 1.2.2bin; at the end of the path line of your autoexec. BAT file ;.

Run the following command to copy the JAR file to the jrelibext directory:
Copy C: jswdk-1.0.1libservlet.jar C: jdk1.2.2jrelibext.

Other related JSP resources:

Servlets and assumerver pages (JSP) 1.0: a tutorial

Assumerver pagestm: A developer's perspective

Assumerver pagestm News & articles

Assumerver pagestm technical resources

Servlets Taverne

Servlets.com

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.