Win2000XP apache2.0.48 + tomcat5.0.19 + mysql4.0.18 + jdk1.5 (1.4) + JSPservlet integration advancement (3)

zhaozj2021-02-16  35

5 test and supplementary description

5.1 Integration to the current report, the system is tested, first of all, please restart Apache, Tomcat and MySQL service

5.1.1 Building a "testdb" database with MySQL Control Center in MySQL, and establish a database user TEST, password TestPWD and build a USER table, there are three fields

NamevarCharPasswordvarcharidint

5.1.2 JSP Test

Under the web publishing directory, build a web-inflica, you can copy a copy from $ TOMCAT / WebApps / root, keep the original directory, then modify the web.xml file under the web-inf directory, the content is changed to the following

DB Connection JDBC / TEST javax.sql.datasource Container

org.apache.jsp.index_jsp org.apache.jsp.index_jsp

org.apache.jsp.index_jsp /index.jsp

Note: Every time you have finished the web.xml file, you have to restart tomcat to take effect and then create a Test.jsp file under D: / WebApps according to the settings of the previous article.

The content is as follows:

<% @ page import = "javax.naming.context"%> <% @ page import = "javax.sql.datasource"%> <% @ Page Import = "javax.naming.initialcontext"%> <% @ Page Import = "java.sql. *"%>

<% @ Page ContentType = "Text / HTML; Charset = GB2312"%> -> <% Datasource DS = NULL; try {context initctx = new InitialContext (); context envctx = (context) INITCTX.LOOKUP ("Java: Comp / ENV"); DS = (DataSource) ENVCTX.LOOKUP ("JDBC / Test"); if (DS! = Null) Out.println "Database connection success"); else out.println ("Database Connection Failure");} Catch (Exception Ne) {Out.println (NE);}%> and then access http://127.0.0.1:8080/test. JSP and http://127.0.0.1/test.jsp should display the same results

If the database connection is successful, you have successfully integrated. If the display database connection fails, the configuration of at least JSP is successful, but there is a problem with the database connection. If the error is displayed, please check it yourself.

5.1.3 Servlet configuration and test

5.1.3.1 Compile a hello.java file, the content is as follows

Import javax.servlet. *; import javax.servlet.http. *; import java.io. *;

Public class hello extends httpservlet {

Public void doget (httpservletRequest Request, httpservletResponse response) throws oException

String name = Request.getParameter ("UserName");

IF (name == null) name = (string) Request.getSession (). GetAttribute ("username");

IF (name == null) Name = "world";

Response.setContentType (Text / HTML ");

PrintWriter out = response.getwriter ();

Out.println ("Hello" Name "!");

Public void dopost (httpservletRequest request, httpservletResponse response) throws oews oityption {dogt (request, response);}}

5.1.3.2 Creating a servlet directory in the web publishing directory, dedicated servlet, improves operational efficiency, the foregoing has been configured in the httpd.conf file, then the D: / WebApps / Web mentioned above Inf Directory Copy to the servlet directory, then put the hello.class files of the Hello.java just compiled into the D: / WebApps / Servlet / Web-INF / CLASES directory, open D: / WebApps / servlet / web-inf /Web.xml file, add content between as follows:

Hello Hello Hello / Hello

Add a servlet mapping, pay attention to / hello to access http://127.0.0.1/kervlet/hello or http://127.0.0.1:8080/servlet/hello can be accessed This servlet, if the servlet directory is XXX directory , you can access http://127.0.0.1/xxx/helloWorld, pay attention to the $ apache / httpd.conf file Content make corresponding modifications, then restart Apache, Tomcat and other services

If you have successful, congratulations, you have completed Win2000 / XP apache2.0.48 tomcat5.0.19 mysql4.0.18 jdk1.5 (1.4) JSP / Servlet integration

I don't know how long I have learned, I have just learned that I have found a lot of information in the past few days. After I have continuously experiment, I will summarize the article after the netizen, and the purpose is mainly to throw the jade. For beginners, I will ask everyone to advise. The row of articles written, it is inevitable that there is a mistake and deficiencies, thank you, don't lose eggs.

Author: Arthurz Email: YEPPO1979@msn.com

转载请注明原文地址:https://www.9cbs.com/read-26161.html

New Post(0)