JSP Quick Start

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 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 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:. 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 </H <% = I %>

<% }%>.



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 in? Html>



JSP entry



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 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 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:. 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 </H <% = I %>

<% }%>.



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 \ jswdk-1.0.1 \ classes.



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 JDSWK installation directory \ bin directory to your PATH. Add C:. 2.2 \ bin; at the end of the PATH line of your autoexec. bat file ;.

 



Run the following command to copy the JAR file to the \ jre \ lib \ ext directory:

Copy c: \ jswdk-1.0.1 \ lib \ servlet. jar c: \ jdk1.2.2 \ jre \ lib \ ext.

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.