JavaServerPages (JSP) is a Java-based cross-platform web development language. JSP is compatible with Microsoft's ActiveServerPages, but it uses HTML-like volumes and Java code segments instead of VBScript. When the website server you are using does not provide local ASP support, that is, Apache or Netscape server, you can consider using JSP JavaServer Pages (JSP) is a cross-platform web development language based on Java.
JSP is compatible with Microsoft's Active Server Pages, but it uses HTML-like volumes and Java code segments 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 server-side 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.
1. install your machine to use JSP
II. basic principles of JSP syntax
III. JSP and JavaBean
4. internal JJSP objects
V. Other JSP-related resources
JSP Development 2
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 is released. the JSDK downloadable version is in the form of installable compression. 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:
First Page
Today is:
<% = New java. util. Date () %>
.
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.
JSP development 3
Basic principles of JSP syntax
After the installation, we will discuss the JSP syntax. if you want 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 indication <% @ compiler indication %>
Declaration <%! Declaration %>
Expression <% = expression %>
Program code segment/small instruction <% program code segment %>
Comment <% -- 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 ++) {%>
> 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:
<% -- Comment on the server -- %>.
JSP Development 4
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 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 JavaBean. this work can be used. To complete: .
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 Volume label. With 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: .
You need to use For this label, you also need to identify beans and the features to be corrected. In addition, you also need to provide new values. if the name is correct, these can be obtained directly in the specified format: ;
To get this from a parameter, you must directly name this feature and the parameter: ;
Or directly set it by name and value: Or />.
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.
JSP development 5
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 returned from the response webpage to the client
The pageContext page is managed here.
Session related to the request
Content being executed by application servlet
Output stream used to send response
Config servlet architecture object
Page JSP page itself
Exception for the error webpage, exceptions not captured
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 in Directory of the JDSWK installation directory to your PATH. Add C: 1.2.2in; 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.