Build a dynamic website with JSP

zhaozj2021-02-08  246

First, what is JSP

JSP (JavaServer Pages) is advocated by Sun Microsystems, many companies participating together

A dynamic web technology standard, its URL is http://www.javasoft.com/products

/ Jsp. Add Java program fragments in traditional web html files (* .htm, *. Html) (Script

LET) and JSP Tag (TAG) constitute a JSP page (* .jsp). Web server encountered access JS

When the P page request, first execute the program fragment, and then return the execution result in the HTML format.

Household. The program clip can operate the database, re-set the web, and send email, etc., this is the establishment

Dynamic sites needed. All program operations are executed on the server, and the network is transmitted to the client.

The result is only the result, the lowest requirements for the customer browser, can achieve no PLUGIN, no ActiveX,

No Java Applet, even without frame.

This article will introduce the method of developing dynamic web pages using JSP technology, and will also briefly analyze JSP technology and Microso.

The difference between FT company's ASP technology.

Second, how to install and start

In order to experiment JSP technology, we need to establish an operational environment, which is quite simple:

1, download JDK at http://java.sun.com/jdk/ (Java 2 SDK, Standard EDI)

TION, V 1.2.2).

2, download JSWDK at http://java.sun.com/products/jsp/ (JavaServer WE)

b Development Kit 1.0.1). Linux users can at http://jakarta.apache.org

/ Download Tomcat 3.0.

3, installation

Taking the Windows NT environment as an example, the JDK installation is first running downloaded JDK1_2_2-win.exe,

After modifying the system environment parameters, add [x:] jdk1.2.2bin in the PATH parameter and add new environments

Parameter classpath = [x:] jdk1.2.2libtools.jar, where [x:] is a hard disk that installs JDK

The characters (C:, D:, etc.). JSWDK installation only needs to release the jwwwdk1_0_1-win.zip to the hard disk

In the root directory (C:, D:, etc.), then you can find the JSWDK-1.0.1 directory on the hard disk. In the future

If you don't want to keep JSWDK, you can delete this directory, no system files and registry left

. More detailed installation process and Solaris / UNIX and Linux, JDK, Tomcat installation can be referred to

The installation instructions in the obtained file package.

4, start

Taking the Windows NT environment as an example, you can start STARTSERVER.BAT in the JSWDK-1.0.1 directory.

A web server that supports JSP web pages in JSWDK. In order not to with existing web servers (such as II

S, PWS, etc.) conflicts, JSWDK's web server uses an 8080 port. Type http: // localhost: 8080 or http://127.0.0.1:8080, if you can see J

SWDK's Welcome page shows that the JSP experiment environment has been built, and you can go to the next experiment. To turn off the web service

The server runs StopServer.bat.

Third, JSP simple example

The document directory of the web server included in the JSWDK is JSWDK-1.0.1Webpages, the main article in the default.

The file is index.html and index.jsp.jsp.jsp in the default. That is to say, visit http: // localhost

: 8080 is equal to access JSWDK-1.0.1WebpageSindex.html.

Create a text file hi.jsp with a text editor, such as the Notepad in Windows.

Save under the JSWDK-1.0.1WebPages directory, its contents are as follows:

<p></ HEAD</p> <p><Body "</p> <p><%</p> <p>String msg = "this JSP Test."</p> <p>OUT.PRINT ("Hello World!");</p> <p>%</p> <p><H2 "<% = msg%" </ h2 "</p> <p></ Body "</p> <p></ Html</p> <p>Type http: // localhost: 8080 / hi.jsp, Jswdk in the address bar of the browser</p> <p>The server will execute the JAVA program statement enclosed in the JSP file, where Out.print is</p> <p>Text output to the web page, the role of statement <% = variable expression% "is to change the variables or in java scriptlet</p> <p>The value of the expression is output to the web page.</p> <p>Assign the variable MSG to the Chinese string, output with <% =% ", or use Out.print to output Chinese characters</p> <p>Strings, the experiment running under the English version NT4 and Redhat 6.1 is normal, but in Chinese NT 4.0 and Chinese 9</p> <p>8, then there will be garbled.</p> <p>Fourth, unified website interface</p> <p>The JSP supports the server-side file containing, you can insert multiple other files in a JSP file, used</p> <p>Enhance a unified website interface. Modify the above Hi.JSP and save it as MyPage.jsp:</p> <p><% @ Include file = "TOP.HTM"% "</p> <p><%</p> <p>String msg = "this JSP Test."</p> <p>OUT.PRINT ("Hello World!");</p> <p>%</p> <p><H2 "<% = msg%" </ h2 "</p> <p><% @ Include file = "bot.htm"% "</p> <p>Set up the framework of the design website, such as FrontPage, Dreamweave, etc.</p> <p>The well-planned frame structure file is divided into two parts, and half of the above is saved as top.htm, half of the following</p> <p>For BOT.HTM, the code is as shown below:</p> <p><Html</p> <p><HEAD</p> <p><Meta http-equiv = "content-type" content = "text / html; charSet = GB2312" "</p> <p><Title "My Home </ TITLE"</p> <p></ HEAD</p> <p><Body "</p> <p><Table border = "0" width = "100%" cellpadding = "4" cellspacing = "0" align =</p> <p>"center" "</p> <p><TR "</p> <p><Td width = "100%" colSpan = "2" bgcolor = "# 837ed1" align = "center" <font</p> <p>Face = "Lishu"</p> <p>Color = "# fff00" size = 5 "Home Title </ font"</p> <p></ Td "</p> <p></ TR "</p> <p><TR "</p> <p><TD BGCOLOR = "# 837ed1" width = "15%" Valign = "TOP" align = "center" "<br"</p> <p><Font color = "# fff" "option </ font" <p "<font color =" # ffffff "option</p> <p></ Font "</ p"</p> <p><P <font color = "# ffffff" "option </ font" </ p "</p> <p><P "<font color =" # ffffff "...... </ font" </ p "</p> <p><P "</ p"</p> <p></ Td "</p> <p><TD width = "85%" VALIGN = "TOP" "</p> <p>-------------------------------------------------- --------------------</p> <p>------------------------------------------</p> <p></ Td "</p> <p></ TR "</p> <p></ TABLE "</p> <p></ Body "</p> <p></ Html</p> <p>Type http: // localhost: 8080 / mypage.jsp in the address bar of your browser.</p> <p>This way, the interface of the site can be unified, and the designer can concentrate on handling the user on the functional module.</p> <p>Record, connect to the database, send email, etc. Each JSP file has the following structure:</p> <p><% @ Include file = "TOP.HTM"% "</p> <p><%</p> <p>/ / Implement some functions</p> <p>%</p> <p><% @ Include file = "bot.htm"% "</p> <p>Maintaining the interface of the website is relatively easy, as long as Top.htm and Bot.htm can be modified, you can affect all</p> <p>Web page.</p> <p>V. Server parameter settings</p> <p>JSWDK's web server parameters are saved in JSWDK-1.0.1Webserver.xml, playing with Windows Word board</p> <p>Open and edit this file to modify the default settings. This section is mainly for JSWDK, Linux under TO</p> <p>The setting method of MCAT is slightly different.</p> <p>JSwdk Default Document Directory is jswdk-1.0.1WebPages, which can create subdirectory in this directory, such as JSWDK-1.0.1Webpagestest, you can use http: // localhost / test / test / TEST</p> <p>Ask this directory, in order to make this sub-directory to execute the JSP program, you must also be in WebServer.xml</p> <p><Service "</ service" section joins:</p> <p><WebApplication ID = "Test" mapping = "/ test" DOCBASE = "WebPages / TEST" /</p> <p>"And, you must also establish a JSWDK-1.0.1WebPageStestWeb-Inf directory and from JSWDK-1.0.1</p> <p>Copy the following four files in the webpagesWeb-INF directory: mappings.properties, mime.p</p> <p>Roperties, servlets.properties and webapp.properties. Complete these processes can</p> <p>Notify the JSWDK's web server to perform the JSP program in http: // localhost / test.</p> <p>Javabean</p> <p>One of the JSP web attractive places is to combine the functionality of the program in the web page.</p> <p>JavaBean is a Java class (Class), which has some function or method or by encapsulation attributes or</p> <p>Handle an object of a business. JavaBean is organized into package for management,</p> <p>In the event, put a set of Javabeans in a certain directory, plus a Package a certain directory.</p> <p>This example is TEST. Directory TEST must be placed in the system environment classpath included, the system can</p> <p>Find the Javabean in it. JSWDK will jswk-1.0.1webpagesWeb-Infjspbe in the default state</p> <p>ANS joins ClassPath. When you build your own Javabean and Package, you will not be in this directory.</p> <p>Lost a simple way.</p> <p>Here is a simple JavaBean framework. Create a text file HelloWorld with a text editor</p> <p>.java, and saved in the JSWDK-1.0.1WebpagesWeb-Infjspbeanstest directory, its contents are as follows</p> <p>:</p> <p>Package test;</p> <p>Public class helloworld {</p> <p>Public string name = "my first bean";</p> <p>Public String gethi ()</p> <p>{</p> <p>Return "Hello from" Name;</p> <p>}</p> <p>}</p> <p>After HelloWorld.java, after the DOS state, enter the directory JSWDK-1.0.1WebpagesWeb-i</p> <p>NfjspBeans, compiled HelloWorld.java with JDK's Javac commands as follows:</p> <p>Javac HelloWorld.java</p> <p>Note that Java is case sensitive, in the program, the case in the compile command line cannot be written</p> <p>. Compiling success reveals a JavaBean. See how to use this Javabean in JSP.</p> <p>Create a text file hi-bean.jsp with a text editor, and save it in the JSWDK-1.0.1WebpageStest directory, its contents are as follows:</p> <p><Html</p> <p><HEAD</p> <p><Title "javabean test </ TITLE"</p> <p></ HEAD</p> <p><Body "</p> <p><JSP: Usebean ID = "Hellobean" scope = "session" class = "test.helloworld"</p> <p>/ "</p> <p><% = Hellobean.gethi ()% "</p> <p><Hr "</p> <p><%</p> <p>Hellobean.name = "jsp";</p> <p>Out.print (Hellobean.Gethi ());</p> <p>%</p> <p></ Body "</p> <p></ Html</p> <p>In the JSP page, use the <JSP: UseBean ... / "syntax to create a JavaBean object and name it</p> <p>For Hellobean. Readers can see the settings from this simple example, get JavaBean properties, and tune</p> <p>Use the JavaBean method. Type http: // localhost: 8080 / in the address bar of the browser</p> <p>Test / hi-bean.jsp, the result is shown in Figure 3. Note that if you modify and recompile Javabe</p> <p>The result of the AN program that needs to be turned off and restarted the JSWDK's web server. Such as</p> <p>If you only modify the JSP file, you don't have to restart the JSWDK's web server.</p> <p>Although this only completed a very simple JavaBean framework, but follow this framework to design</p> <p>A variety of diverse JavaBeans. For example, access data from JSP is usually achieved by JavaBean.</p> <p>.</p> <p>7. Database connection</p> <p>Database connection is the most important part of the dynamic website, the technology connected to the database in Java is JDBC</p> <p>(Java Database Connectivity). Many database systems with JDBC drivers, Java</p> <p>The order is connected to the database through the JDBC driver, performing queries, extracting data, etc. Sun also</p> <p>Developed JDBC-ODBC Bridge, using this technology Java program to access the number with an ODBC driver</p> <p>According to the library, most database systems have an ODBC driver, so Java programs can access such things such as OR.</p> <p>ACLE, SYBASE, MS SQL Server, and MS Access and other databases. Here is how to use Access</p> <p>Now a dynamic FAQ (FAQ) website. First create an Access database FAQ.mdb,</p> <p>The table FAQS has a field ID (automatic incremental, and is a primary keyword), SUBJECT (text, long</p> <p>Degree 200), Answers. Some common problems and answers for programming knowledge can be stored in this table.</p> <p>case,</p> <p>Then, add System DSN in the ODBC Datasource module of Control Panel.</p> <p>, Name FAQ, and point to FAQ.mdb. Create a JavaBean, named FAQ.JAVA, and save it in JSW</p> <p>DK-1.0.1WebpagesWeb-InfjspBeanstest directory. The content of FAQ.java is as follows: package test;</p> <p>Import java.sql. *;</p> <p>PUBLIC CLASS FAQ {</p> <p>String sdbdriver = "sun.jdbc.odbc.jdbcodbcdriver";</p> <p>String sconnstr = "JDBC: ODBC: FAQ";</p> <p>Connection conn = NULL;</p> <p>ResultSet RS = NULL;</p> <p>Public FAQ () {</p> <p>Try {</p> <p>Class.Forname (SDBDriver);</p> <p>}</p> <p>Catch (java.lang.classnotfoundexception e) {</p> <p>System.err.println ("FAQ ():" E.getMessage ());</p> <p>}</p> <p>}</p> <p>Public ResultSet ExecuteQuery (String SQL) {</p> <p>RS = NULL;</p> <p>Try {</p> <p>Conn = drivermanager.getConnection (Sconnstr);</p> <p>Statement Stmt = conn.createstatement ();</p> <p>RS = stmt.executequery (SQL);</p> <p>}</p> <p>Catch (Sqlexception EX) {</p> <p>System.err.println ("Aq.executeQuery:" ex.getMessage ());</p> <p>}</p> <p>Return RS;</p> <p>}</p> <p>}</p> <p>After using the method introduced by the previous section, it is created under the JSWDK-1.0.1Webpagestest directory.</p> <p>JSP file FAQ.jsp, its contents are as follows:</p> <p><Html</p> <p><HEAD</p> <p><Meta http-equiv = "content-type" content = "text / html; charSet = GB2312"</p> <p>"</p> <p><Title "My FAQ! </ Title "</p> <p></ HEAD</p> <p><Body "</p> <p><P "<b" This is my FAQ! </ B "</ p"</p> <p><% @ Page language = "java" import = "java.sql. *"% "</p> <p><JSP: Usebean ID = "Workm" scope = "page" class = "test.faq" / "</p> <p><%</p> <p>ResultSet RS = Workm.executeQuery ("SELECT * FROM FAQS"); String Tt;</p> <p>While (rs.next ()) {</p> <p>Tt = rs.getstring ("Answer");</p> <p>Out.print ("<li" rs.getstring ("Subject") "</ li");</p> <p>OUT.PRINT ("<pre" tt "</ pre");</p> <p>}</p> <p>Rs.close ();</p> <p>%</p> <p>Type http: // localhost: 8080 / test / faq.jsp, FAQ.js in the address bar of the browser</p> <p>P call JavaBean, read the content from the database and output it.</p> <p>Limited to the space, this article does not list complex examples of JSP-JavaBean-JDBC / ODBC-database, readers can</p> <p>Found and downloaded to the database connection example from the last recommended URL in this article.</p> <p>Eight, technical analysis</p> <p>Microsoft's ASP technology is also a dynamic web development technology. JSP and ASP are very similar in form</p> <p>The ASP programmer recognizes <% "and <% =%." But in-depth exploration, you will find them.</p> <p>A lot of differences, the most important of which have the following three points:</p> <p>1, JSP is more efficient and safe</p> <p>ASP is stored in the source code to interpret how to operate, each ASP web page call needs to explain the source code.</p> <p>, Running efficiency is not high. In addition, the vulnerability of IIS has made many website source programs, including the author</p> <p>With ASP development, the ASP program is all downloaded.</p> <p>JSP is compiled into a zona code before execution, byte code by Java Virtual Machine (Java)</p> <p>Virtual Machine) Explain the execution, the efficiency of the source code is high; the server is also available on the server.</p> <p>The HE mechanism can improve the access efficiency of the bytecode. The first call JSP page may be slightly slow because it is compiled</p> <p>It is a lot of cache. At the same time, the JSP source program is unlikely to be downloaded, especially Javabean</p> <p>The program can be fully placed in an abroad directory.</p> <p>2, JSP components (Component) is more convenient</p> <p>ASP expands complex features with COM, such as file upload, email, and processing business or complex</p> <p>Calculate the separation to become an independent reusable module. JSP achieves the same functionality through JavaBean</p> <p>Charge.</p> <p>In development, COM development is far more complicated and cumbersome, and the learning asp is not difficult, but learns to develop CO.</p> <p>M is not simple. Javabean is more simple, from this example, it can be seen that JavaBean is very</p> <p>Convenience.</p> <p>In terms of maintenance, COM must register on the server. If you modify the COM program, you must re-register.</p> <p>Even must shut down and restart. JavaBean does not need to register, put it in the directory contained in ClassPath.</p> <p>Ok. If Javabean has modified, JSWDK and Tomcat also need to turn off and re-run (but not shutdown), but developers have promised that they will do not need to turn off the server in future versions.</p> <p>In addition, JavaBean is complete OOP, which can be easily established for different business processing functions.</p> <p>Subject library, such as user rights control, email automatic reply, etc.</p> <p>3, JSP adaptation platform is more</p> <p>ASP is currently only available for NT and IIS. Although there is a CHILISoft plugin under UNIX to support ASP, ASP this</p> <p>The function of the body is limited, and it must be expanded through the combination of ASP COM. It is very difficult for COM in Unix</p> <p>.</p> <p>JSP is different, almost all platforms support Java, JSP Javabean can pass unobstructed under all platforms</p> <p>. IIS under NT passes a plugin, such as JRun (http://www3.allaire.com/products/</p> <p>JRUN /) or servletexec (http://www.newatlanta.com/) can support JSP</p> <p>. The famous Web server Apache has been able to support JSP. Because Apache is widely used in NT, UNIX and</p> <p>On Linux, JSP has a wider range of running platforms. Although the NT operating system now accounts for a large market share</p> <p>Amount, but the advantage of UNIX in terms of server is still very large, and the new rise Linux is not small.</p> <p>Transplant from a platform to another platform, JSP and JavaBean don't even need to recompile because Java words</p> <p>The section code is standard and the platform is independent. The author will get LIN in the JSP page of the experiment under NT.</p> <p>UX is running, it feels very satisfied.</p> <p>Nine, conclusions</p> <p>In summary, JSP can be described as a tool to build a dynamic website, so it is recommended to readers, I wish you master J</p> <p>SP has developed an excellent website. ASP programmers also tried, there is session, request, in JSP, Request,</p> <p>Response / Out, etc., try it or try.</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-2546.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="2546" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.043</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'HJW_2FGuIfh0EHYQPAmN45x8CS_2B9Pg1_2F_2FxdCRIaGoCKkfIJbPfOV13qCH_2FB8071M3cP_2BNN0bKsU_2BiFe4s4vWcKqg_3D_3D'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>