Web container tomcat environment configuration with JSP support

Source: Internet
Author: User
Tags connect odbc sql string stmt variable window oracle database
Js|web System Environment:
1, operating system: Windows 2000
2. Database: Oracle 8i R2 (8.1.6) for NT Enterprise Edition
3, Installation path: C:\ORACLE

This example uses the software to download the address:
1, JDK-1.4-WIN.EXE[19.5MB]
http://java.sun.com/
2, JAKARTA-TOMCAT.ZIP[2.26MB]
http://jakarta.apache.org/builds/tomcat/release/v4.06

Installation method:
The method inside () is the directory on my machine, which is easy to use to change the default configuration.

First, install the JDK
The first step is to double-click the Jdk-1_2_2_006-win.exe file for installation and install using the default configuration
The default installation directory for JDK is the C:\JDK1.4.1,JRE default installation directory for C:\Program files\javasoft\jre\1.4;
(But for the convenience of later use, you can choose a better recorded directory, such as C:\JDK14;)

Step Two, update the environment variables
Start-> Setup-> Control Panel-> System-> Advanced-> Environment variable-> System variable
Append Classpath:c:\jdk1.2.2\lib\tools.jar;c:\jdk1.2.2\lib\dt.jar;
(Append Classpath:c:\jdk14\lib\tools.jar; c:\jdk14\lib\dt.jar;)
Append path:c:\jdk1.2.2;c:\jdk1.2.2\bin;
(Append path:c:\jdk14;c:\jdk14\bin;

second, the installation of Tomcat
The first step, directly double-click the downloaded EXE file. Go to the default installation (where you can choose the installation path, such as C:\TOMCAT4, and, if you want to start Tomcat as NT service later, you can choose NT Serveer, this option)

Step two, double-click Startup.bat to start the Tomcat server
At this point, a DOS window appears on the screen, stops motionless, and there are some running successful tomcat and initialization information,
proves that Tomcat is running successfully.

Step Fourth, enter http://localhost:8080/on the browser
If you can see the Tomcat Version 4.06 page, the Tomcat installation is successful.
The file you see on the page is placed in the C:\jakarta-tomcat\webapps\examples directory (c:\Tomcat4\webapps\examples directory)
There are some examples of JSP and servlet, the JSP environment is completed.

Note: The above Stop DOS window cannot be turned off, or the Tomcat server stops serving

Note: The JSP file runs slowly for the first time

third, the first JSP example
Name the program test.jsp and put it in the C:\jakarta-tomcat\webapps\examples directory (c:\Tomcat4\webapps\examples directory)
Enter http://localhost:8080/examples/test.jsp in Browser

<%
String Str = "Hello jsp!";
Out.print ("Hello jsp!");
%>

Iv. Write the first JSP program to connect Oracle test.jsp
1, connect into the Sql*plus
Log in as a System/manager user,
Sql> Conn System/manager

Create a new user: such as USER1/PASS1, give Connect,resource permissions.
Sql> Grant Connect,resource to User1 identified by Pass1;

Sql> Conn User1/pass1
Sql> CREATE TABLE Test (a number,b char (10));
sql> INSERT INTO test values (1, ' one ');
sql> INSERT INTO test values (2, ' II ');
sql> INSERT INTO test values (3, ' three ');
Sql> commit;

Sql> select * from test;

A B
---------- ----------
11
22
33

2. Configure ODBC (set on server side)

Start-> Settings-> Control Panel-> Admin Tools-> Data Source (ODBC)-> System dsn-> Add->
Select Oracle ODBC Driver-> Complete->

Data Source name:test name, easy to remember on the line, such as: test
Service name:oradb The instance name of the Oracle database, in this example: ORADB, the general Oracle default installation is ORCL
Userid:user1 Oracle user name, in this case: User1

Press "OK"-> Press "OK" to exit

3, in C:\jakarta-tomcat\webapps\examples (C:\Tocat\webapps\examples) under the creation of test.jsp, with notebook edit, enter the following code
<!--first import some of the necessary packages-->
<%@ page language= "Java" contenttype= "text/html; charset=gb2312 "%>
<%@ page info= "Database handler"%>
<%@ page import= "java.io.*"%>
<%@ page import= "java.util.*"%>

<!--tell the compiler to use SQL package-->
<%@ page import= "java.sql.*"%>

<%@ page import= "javax.servlet.*"%>
<%@ page import= "javax.servlet.http.*"%>

<%
Start with try
Try
{
Connection con;
Statement stmt;
ResultSet rs;

Loading the driver, the following code is loading the JDBD-ODBC driver
Class.forName ("Sun.jdbc.odbc.JdbcOdbcDriver");

Connect to the database with the appropriate driver, test "is the System DSN name
String url= "Jdbc:odbc:test";

Establish a connection, similar to creating a database join in ASP
Con=drivermanager.getconnection (URL, "user1", "Pass1");

Create a JDBC Declaration
stmt = Con.createstatement ();

Add new record
Stmt.executeupdate ("INSERT into Test (a,b) VALUES (1, ' 2 ')");

Query records
rs = Stmt.executequery ("SELECT a,b from Test");

Output query Results
Out.println ("<table border=1 width=400>");
while (Rs.next ())
{
String col1 = rs.getstring (1);
String col2 = rs.getstring (2);
Print the data that is displayed
Out.println ("<tr><td>" +col1+ "</td><td>" +col2+ "</td></tr>");
}
Out.println ("</table>");
}

If an error occurs during loading, give the appropriate error message
catch (Exception e) {}
%>

4, open a browser window, enter the following address to view the results of the operation
http://localhost:8080/examples/test.jsp

It's that simple! But here is the JDBC:ODBC; if you use JDBC later, you must download the corresponding database JDBC class and place it in the classpath.

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.