JSP technology to generate dynamic web page _ MySQL

Source: Internet
Author: User
With the development of WEB technology, WEB content ranges from static pages to dynamic pages with rich content. Dynamic page generation is a challenge for WEB developers. There are many ways to solve this problem, such as plug-in technology and server-based APIs. But one problem is that these methods are for a specific web server, as mentioned by Microsoft
With the development of WEB technology, WEB content ranges from static pages to dynamic pages with rich content. Dynamic page generation is a challenge for WEB developers. There are many ways to solve this problem, such as plug-in technology and server-based APIs. But one problem is that these methods are for a specific web server, for example, Microsoft's ASP technology only targets its IIS and Personal web servers.
Currently, CGI, PHP, and JavaServer Page (JSP) technologies are widely used to generate dynamic web pages. Among them, CGI obtains information by accessing other applications and returns it to the browser. CGI programs are usually developed in C or PERL; jsp implements dynamic content by compiling the Jsp component into a Java Servlet running on the server. The following describes the features of Jsp and traditional CGI:
Portability:
Most WEB servers support CGI interfaces, but CGI programs cannot run across platforms. Java Servlet programs have the advantages of JAVA programs and can run on any platform, most WEB and application servers support JAVA and servlet APIs.
Performance:
In the traditional CGI environment, each client request to the CGI program generates a new process on the server to load and execute the CGI program. A large number of parallel requests greatly reduce its performance. JSP does not have this limitation. Each program is loaded once and resident in the server memory to serve future requests. In addition, JSP is much easier to write and control multiple threads than CGI.
Development and release:
Java Servelt has all the advantages of JAVA, and it is relatively easy to develop. java simplifies exception handling. its object-oriented feature makes developer collaboration a simple task. JSP technology accelerates dynamic website development from the following aspects:
1) separate static page development from dynamic content generation
JSP uses HTML or XML tags to plan and design the layout and style of web pages. JSP Tags are used to implement dynamic content, the dynamic content is encapsulated and run on the server. in this way, the page layout and style can be edited and debugged separately without affecting the dynamic content generation.
2) component reuse
Most JSP pages reuse the platform-independent components-javaBeans to meet complex requirements. These components can be reused by developers.
3) simplify page development by marking
Through JSP, developers can easily use the beans component to set and access their attributes. At the same time, JSP also allows users to customize tags and accept tags developed by third parties to facilitate the use of functional components provided by third parties.
After learning about the features of JSP, let's talk about the installation and development of JSP instances. (The operating system uses Nt4.0 as an example)
Jdk and jswdk must be installed to run JSP pages. Install jdk first. after jdk is installed, ensure that javac and java work properly. Then install jswdk. In fact, you only need to extract jswdk to a directory. To make jswdk run properly, you also need to modify the environment variable CLASSPATH as follows:
If JDK is used, you do not need to modify CLASSPATH. if JDK is used, set the environment variable JAVA_HOME to point to the JDK installation path.
After completing the preceding steps, you can start Java WebServer.
Run the startserver. bat file in the jswdk directory to start Java Server. the default port used by the Web server is 8080. If the Web server needs to be restarted after it is updated, stop the started Web server and run stopserver. bat to stop it.
After the assumerver is started, if it is normal, enter http: // localhost: 8080/in the browser address to display the Page about Java Webserver Page. There is also an examples directory under the jswdk directory, which is a self-contained web application in jswdk. a web application contains resources such as jsps, servlets, html files, and images. We can also create a new web application to put relevant files in it. Let's analyze how to create a new web application.
To create a new web application, you must create a directory under the jswdk installation directory. for example, to create a web application named myweb, the directory structure is as follows:



Jswdk_inatall myweb web-inf
Servlets
Jsp beans
Webapp. properties
Servlets. properties
Mime. properties
Mappings. properties
Here: jswdk_install indicates the jswdk installation directory. four files suffixed with properties are attribute files, which can be copied from jswkd_install/web-inf. Modify the webserver. xml file under the jswdk_install directory and add the following line to map myapp:
Modify the startserver. bat file and add the myweb/web-info/jsp/beans directory to the corresponding environment variable beanJars. you can also add this path to the CLASSPATH environment variable.
To add a jsp file, you can put the file in the myweb directory or its lower-level Directory. to add the servlet, you only need to put the compiled servlet into the servlets; put the compiled java beans in the beans directory.
Note that you need to restart the Web Server to make the modification take effect after you modify the beans or servlet.
After learning about these rules, we will create the first JSP file myfirst. jsp as follows:

My first Jsp file <titile> <br> </pead> <br> <body> <br> <% out. println ("this is my first jsp file "); %> <br> </body> <br> </ptml> <br> place the file in the jswkd_install \ myweb Directory. if the Web server is started, enter <br> http: // localhost: 8080/myweb/myfirst. jsp <br> we can see the execution result. After completing the first simple jsp file, we can compile a java beans and call the beans in the jsp file. <Br> java bean source code HelloWorld. java Content: <br> public class helloWorld {<br> public String name; <br> public boolean setHello (String name) {<br> this. name = new String (name); <br >}< br> public String sayHello () <br >{< br> return name; <br >}< br> compile the file with JDK: <br> javac helloWorld. java <br> after successful compilation, the generated bytecode file HelloWorld. put the class in the myweb/web-inf/jsp/beans Directory. <br> In the following jsp file test. jsp calls helloWorld, test. jsp content: <br> <ptml> <br> <pead> <br> <title> Jsp and java bean



<%
String hello = "this is a bean test ";
HelloBean. setHello (hello );
Out. println (helloBean. sayHello () +"
");
%>


Place the jsp file in the jswdk_install \ myweb \ directory.
Restart the web Server and enter:
Http: /localhost: 8080/myweb/test. jsp
The execution result is displayed;
Note that in test. jsp
Scope = "session" indicates that the object can be referenced on other pages of the same session after being created. For example, we can reference the object created in test. jsp in aftertest. jsp. the content of aftertest. jsp contains the following code:
<%
HelloWorld rebean = (helloWorld) session. getValue ("helloBean ");
Out. println ("bean used in aftertest. jsp" + rebean. sayHello ());
%>
Note that the object to be referenced must have been created; otherwise, an exception occurs.
The following shows an example of using java beans to access data in jsp. The database I use is oracle8. the connection string created through SQL * Net is begpinter. the database server runs on the machine named begpinterserver. the following content of JspJdbc. java:
// You need to import the java. SQL package to use JDBC
Import java. SQL .*;
Import oracle. jdbc. driver .*;
Public class JspJdbc
{
Connection conn = null;



Public ResultSet rset = null;
Public JdbcCheckup (){
// Load the Oracle JDBC driver
Try {
DriverManager. registerDriver (new oracle. jdbc. driver. OracleDriver ());
} Catch (SQLException e1 ){
System. err. println ("executeQuery:" + e1.getMessage ());
}
}



Public ResultSet executeQuery (String SQL ){
Rset = null;
Try {
Conn = DriverManager. getConnection
("Jdbc: oracle: thin: @ bgpinterserver: 1521: bgpinter", "SCOTT", "TIGER ");
Statement stmt = conn. createStatement ();
Rset = stmt.exe cuteQuery (SQL );
} Catch (SQLException e1 ){
System. err. println ("error:" + e1.getMessage ());
}
Return rset;
}
}
After compilation, place the JspJdbc. class file in the myweb \ web-inf \ jsp \ beans directory. Call beans in the following jsp file. the content of jspdb. jsp is as follows:



Database in Jsp





<% @ Page language = "java" import = "java. SQL. *, oracle. jdbc. driver. *" errorPage = "errorpage. jsp" %>

<%
ResultSet rset = jspdatabase.exe cuteQuery ("SELECT * FROM emp ");
Out. println ("





");While (reset. next ()){Out. println (" ");Out. println (" ");}Rest. close ();Out. println ("
Number Name
"+ Rset. getInt (" eptno ") +""+ Rset. getString (" enameeptno ") +"
");
%>


The error page. jsp used to display exceptions is as follows:


<% @ Page isErrorPage = "true" %>
The exception <% = exception. getMessage () %>


Restart the Web server to make the newly created java beans take effect. if the connection to the data server is normal, enter
Http: // localhost: 8080/myweb/jspdb. jsp
The query result is displayed.
Through the above introduction, I believe you know something about Jsp. For more information about Jsp technology, visit the following sites:
Http://java.sun.com/products/jsp

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.