How to get started with beginners JSP example analysis

Source: Internet
Author: User
Tags object execution expression variables string variable valid apache tomcat
js| Beginner (Quick Start 1) JSP (JavaServer Pages) Introduction: In today's world, the popular Internet application development mainly includes: asp,php,cgi,jsp ... And many other schemes, technical advantages and disadvantages, but the JSP with its simple and easy to learn, cross-platform characteristics, in many programs unique, in a few years has formed a complete set of norms, and widely used in E-commerce and other fields. JSP in the domestic now has been more extensive attention.

2 How to quickly grasp the jsp:jsp is actually a JSP definition of some tags and Java program segments, as well as the mix of HTML files. So, if you want to master JSP must first have a certain understanding of HTML (or how to design a Web page), and then, you must have the basis of Java program (to know JSP is based on the Java language), Finally, some of the necessary knowledge of the JSP logo (it lets you know how to use the Java language and HTML group to synthesize the complete JSP), and this article we mainly introduce the JSP identification and the grammar rule. So it is best to have a little understanding of HTML and the Java language, if you have not learned that there is no relationship, the following introduction will let you have a more intuitive JSP to understand.

3 experience simple and easy to learn jsp:jsp simple and easy to learn, if you have the basis of HTML, to learn it is very easy, do not believe? First take a JSP program for you to see (Simple and classic helloword.jsp):


<%page language= "java"% >
< html>
< head >
< Title>hello world!</title>
< body bgcolor= "#FFFFFF" >
<%string msg= "JSP Example";
Defining String objects
Out.println ("Hello world!");
%>
<%=msg%> <!-display variable value->
</body>


After running, the output string on the window Hello world! JSP Example, how, a look at it, indeed very simple, except for the "<% >" logo, the others are ordinary hypertext, if you have the basis of hypertext, a look at it, but he and the hypertext is a little different, that is to join the "<% > "Identification, the use of the logo is a Java program, by which to control the display of dynamic data, and directly output to the location embedded in the identifier, the entire structure is quite intuitive, and later if the page changed, the modification has become very easy." As for the identification of the first line of the program, we can not take care of him at the moment, in the end, we will make a corresponding introduction to this kind of grammar.

Read the above JSP program, you should also have a bit of JSP understand it, yes, learning JSP is not a difficult thing, and later, you will feel more and more of the advantages of JSP, his cross-platform features in the Internet development Program is unique, Because JSP technology is built on top of the Java language, many of his features and applications are from the Java language, so if you want to learn JSP, you need to have the basis of Java programming (on the Java language, I do not introduce here, you can refer to the corresponding books, to recommend: "JAVA2 program Design "(Tsinghua University Press))

4 jsp start from scratch:

(a) Download the JDK (Java Development Kit) from the www.java.sun.com and install it, now assume that c:\jdk1.3 B builds the JSP runtime environment: To make the JSP run, you first build the JSP runtime environment. The main application Server software we use now is WebLogic application Server,apache Tomcat,allarie Jrun. WebLogic is now the market's highest occupancy of a server software, powerful, configuration is also very simple, as a commercial application of the server is a good choice, but as a beginner application, the proposal is not good, because this is the commercial software need to buy.

JRun is a Allarie company developed by the server software, but also has a relatively high reputation, and WebLogic is also a good choice, but also commercial software. The beginner, Tomcat is your best choice, he is a free server software, and his source code can be obtained free of charge, when you have in-depth understanding of it, you can freely expand to fit your own needs, although the configuration trouble a little, but above so many advantages, It is the best choice we have deserved. Here we will take Tomcat as an example, to introduce you to the Win2000 JSP environment configuration:

1 First, download tomcat from the Apache website (http://java.apache.org), downloading a. zip file (jakarta-tomcat.zip), unzip it to your directory, and assume C:\tomcat.

2 Enter Tomcat directory, find Startup.bat file, open edit. Precede the line call Tomcat start with two lines: Set Tomcat_home=c:\tomcat and set java_home=c:\jdk1.3 Note: c:\jdk1.3 is the jdk1.3 installation location.

3 then right click on "My Computer", select Properties. Open advanced options in System Properties, click "Environment Variables", and add two variables classpath and path in both user variables and system variables, and their values are: Classpath value C:\jdk1.3\bin\tools.jar; The value of C:\jdk1.3\lib\dt.jar path is c:\jdk1.3\bin;c:\jdk1.3

4 Execute Jakarta-tomcat\bin\startup.bat, you will see an open Tocmat startup window,

5 Open the browser, enter: http://localhost:8080, return to see the main Tomcat page

b) Introduction to the basic syntax of JSP:

I. Comment statement: "<%--comment--% >" will skip this identity at compile time;

Ii. inserting a valid program segment:<% code% >

Iii. defining global properties in a JSP file:


<%@ page
[language= "Java"]
Declaring the type of scripting language
[extends= "Package.class"]
Indicates the full name of the Java class to be added when JSP compiles.
But use it carefully, it will limit the ability to compile JSP.
[import= ' {package.class/package.*}, ... ']
List of Java packages that need to be imported
[session= "True/false"]
Set whether the customer needs HTTP session
[buffer= "none/8kb/sizekb"]
The size of the buffer is used by the Out object for processing
The output of the JSP to the client browser after execution
[autoflush= "True/false"]
Set if buffer overflows, do you need to force output,
If the value is defined as true (the default), the output is normal,
If it is set to false, if this buffer overflows,
can cause an unexpected error to occur. If you set the buffer to none,
Then you can't set the AutoFlush to False.
[isthreadsafe= "True/false"]//sets whether the JSP file can be used by multiple threads. The default value is true, which means that the
A JSP can handle requests from multiple users at the same time, if set to false,
A JSP can only process one request at a time
[info= "Text"]
A text in the execution JSP will be added verbatim in the JSP,
You can use the Servlet.getservletinfo method to retrieve it.
[errorpage= "Relativeurl"]
Sets whether this page is an error page,
If it is set to true, you can use the exception object
[Contenttype= "MimeType
[; Charset=characterset] "
/"text/html; Charset=iso-8859-1 "]
Sets the MIME type. The default MIME type is: text/html,
Default Character set is Iso-8859-1
[iserrorpage= "True/false"]%>

IV. Contains a file:

"<%@ include file=" Relativeurl "%>"

The file can be inserted (statically) at compile time;

V. contains an expression:

"<%= expression%>"

The value of an expression is displayed at run time;

Vi. declaring variables:


"<%! Declaration;
[Declaration;] + .. % > ";


Such as

"<%!int i;% >"

Vii. Create a bean instance and set its properties: ""; Class Property: Sample An object from one class; ID property: Indicates the reference name of this class object; Scope property: The scope of the bean's existence and the valid range of the ID variable name. If page is only valid for this page class (the class object will be re-created the next time the page is called); if it is a session, the class object is created once, for example, if it is only once, and if the class is always valid for application , and only once (equivalent to static amount)

d) Tomcat's JSP routines are described:


<!--Copyright (c)
1999 The Apache Software
Foundation. All rights
Reserved.
Number Guess Game
Written by Jason Hunter
< Jasonh@kasoftware.com>
CTO, K&a Software
Http://www.servlets.com-->;
<%@ page import = "num. Numberguessbean "%>
<! -Imported Java Package->
< Jsp:usebean id= "Numguess"
Class= "Num. Numberguessbean "
Scope= "Session"/>
<! -Creates an object and sets its application name to Numguess
And affirms that this object is always valid during session validity-->
< html>
< head>
< Title>number guess</title>
< body bgcolor= ' white ' >
< font size=4>
<% if (numguess.getsuccess ())
{
%>
Call the Getsuccess method of the Numberguessbean object
congratulations!
You got it.
And after just
<%= numguess.getnumguesses ()%>
tries.< p>
<% numguess.reset (); %>
Care to < a href= "numguess.jsp" >try again?
<%} else if (numguess.getnumguesses () = 0)
{
%> Welcome to the number Guess game.
I ' m thinking of a number between 1 and 100.
< form method=get>
What ' s your guess?
< input Type=text name=guess>
< input type=submit value= "Submit" >
</form>
<%} else {%>//
Good guess, but nope. Try < b>
<%= numguess.gethint ()%></b>.
You have made <%= numguess.getnumguesses ()
%> guesses.< p>
I ' m thinking of a number between 1 and 100.
< form method=get> What ' s your guess?
< input Type=text name=guess>
< input type=submit value= "Submit" >
</form> <%}%>
</FONT>
</body>


From the example we can see that JSP is through the logic of the program to control the display of the page, through the program to get the dynamic properties of the display.

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.