JBuilder2005 Development Web Application _jsp programming

Source: Internet
Author: User
Tags auth stmt tomcat server
JBuilder is an open Java IDE that integrates Tomcat, WebLogic, and other servers. Although JDK, Tomcat, and WebLogic are constantly upgraded, we can still use their latest version in JBuilder. Because the configuration of Tomcat servers is more complex, programmers accustomed to Windows platforms are often puzzled by the use of Tomcat. This paper presents an example of database Connection pool (DBCP) in a Tomcat environment, illustrates the general steps of using JBuilder to develop Web applications, and answers some of the frequently encountered problems.

JBuilder2005 's version of the JDK is 1.4.2_04-b05, whose files are placed under the directory jbuilder_homejdk1.4, and the latest version of Tomcat is 5.0.27, with files in the directory Jbuilder_home Hirdparty jakarta-tomcat-5.0.27 under. The following is an example of a database Connection pool (DBCP) using a tomcat environment.

1. File-new Project new project file, input project file name MyWeb and directory C:myweb

2. Project-project properties set the attributes of the project file, select Tomcat as the server

3. File-new new Web Module (WAR)

Enter the name dbtest and directory dbtest for Web module

4. File-new new JSP, input JSP file name test.jsp, produce test.jsp file modify test.jsp content

TEST.JSP:

<%@ page contenttype= "text/html; Charset=big5 "%>
<title> DB Test </title>
<body>
<%
Foo. Dbtest TST = new Foo. Dbtest ();
Tst.init ();
%>
Foo <%= Tst.getfoo ()%> <br/>
Bar <%= Tst.getbar ()%>
</body>

A runtime configuration called test will be generated.

Select Run-configurations-edit to modify runtime configuration, in particular, you can specify the port number of the server and whether to automatically search for the ports that are occupied.

5. File-new class, type name dbtest and package name foo, produce Dbtest.java file and modify its contents

Dbtest.java

Package foo;

Import javax.naming.*;
Import javax.sql.*;
Import java.sql.*;
public class Dbtest {
String foo = "Not Connected";
int bar =-1;
public void init () {
try{
Context ctx = new InitialContext ();
if (CTX = null)
throw new Exception ("boom-no context");
DataSource ds = (DataSource) ctx.lookup ("Java:comp/env/jdbc/testdb");
if (ds!= null) {
Connection conn = Ds.getconnection ();
IF (conn!= null) {
Foo = "Got Connection" +conn.tostring ();
Statement stmt = Conn.createstatement ();
ResultSet rst =stmt.executequery ("SELECT ID, foo, bar from TestData");
if (Rst.next ()) {
Foo=rst.getstring (2);
Bar=rst.getint (3);
}
Conn.close ();
}
}
}catch (Exception e) {
E.printstacktrace ();
}
}
Public String Getfoo () {return foo;}
public int Getbar () {return bar;}
}



6. Modify the contents of Web.xml

Xml:

<?xml version= "1.0" encoding= "UTF-8"?

<web-app xmlns= "Http://java.sun.com/xml/ns/j2ee" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi: schemalocation= "Http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version= "2.4"

<description> MySQL Test App </description>
<resource-ref>
<description> DB Connection </description>
<res-ref-name> Jdbc/testdb </res-ref-name>
<res-type> Javax.sql.DataSource </res-type>
<res-auth> Container </res-auth>
</resource-ref>
</web-app>

7. F9 run the application, the Tomcat subdirectory will be generated in the MyWeb directory, which contains the Conf subdirectory,

Dbtest.xml files are generated in the Tomcat_homeconfcatalinalocalhost directory

8. Add the file in the mywebtomcatconf directory Server8080.xml to the engineering document and modify the Server8080.xml content

Server8080.xml:


<?xml version= "1.0" encoding= "UTF-8"?
<server debug= "0" port= "8081" shutdown= "Shutdown"
<service name= "Catalina"
<connector acceptcount= "connectiontimeout=" 60000 "debug=" 0 "maxthreads=" "minsparethreads=" 5 "port=" 8080 "/>
<engine debug= "0" defaulthost= "localhost" name= "Catalina"

<context path= "/dbtest" docbase= "C:mywebdbtest" debug= "5" reloadable= "true" crosscontext= "true" workdir= "C: Mywebtomcatworkdbtest ">

<logger classname= "Org.apache.catalina.logger.FileLogger" prefix= localhost_dbtest_log. "suffix=". txt "timestamp=" True '/>

<resource name= "Jdbc/testdb" auth= "Container" type= "Javax.sql.DataSource"/>

<resourceparams name= "Jdbc/testdb"
<parameter>
<name> Factory </name>
<value> org.apache.commons.dbcp.BasicDataSourceFactory </value>
</parameter>
!--
Maximum number of DB connections in pool. Make sure
Configure your mysqld max_connections large enough to handle
All of your DB connections. Set to 0 for no limit.
-->
<parameter>
<name> maxactive </name>
<value> </value>
</parameter>
!--
Maximum number of idle DB connections to retain in pool.
Set to 0 for no limit.
-->
<parameter>
<name> Maxidle </name>
<value> </value>
</parameter>
!--
Maximum a DB connection to become available
In MS, example seconds. An Exception is thrown if
This timeout is exceeded. Set to-1 to wait indefinitely.
-->
<parameter>
<name> maxwait </name>
<value> 10000 </value>
</parameter>
!--MySQL DB username and password for DB connections-->
<parameter>
<name> username </name>
<value> SA </value>
</parameter>
<parameter>
<name> Password </name>
<value> Topcomputer </value>
</parameter>
!--Class name for mm.mysql JDBC driver-->
<parameter>
<name> Driverclassname </name>
<value> Com.microsoft.jdbc.sqlserver.SQLServerDriver </value>
</parameter>
!--
The JDBC connection URL for connecting to your MySQL DB.
The autoreconnect=true argument to the URL makes sure
Mm.mysql JDBC Driver'll automatically reconnect if mysqld closed the
Connection. Mysqld By default closes idle connections after 8 hours.
-->
<parameter>
<name> URL </name>
<value> jdbc:microsoft:sqlserver://nt04:1433;databasename=test </value>
</parameter>
</ResourceParams>
</Context>
</Host>
</Engine>
</Service>
</Server>


9. Place the JDBC driver in the c:borlandjbuilder2005 hirdpartyjakarta-tomcat-5.0.27commonlib directory

10. Set up Database test in SQL Server, database table file TestData

Creattable.sql:


if exists (SELECT * from dbo.sysobjects WHERE id = object_id (N ' [dbo].[ TestData] and OBJECTPROPERTY (ID, N ' isusertable ') = 1)
drop table [dbo]. [TestData]
Go

CREATE TABLE [dbo]. [TestData] (
[ID] [int] not NULL,
[Foo] [varchar] (m) COLLATE chinese_taiwan_stroke_ci_as NULL,
[Bar] [INT] Not NULL
) on [PRIMARY]

Go

Enter a few records as test data.

F9.
12. The contents of the Startup.bat and Shutdown.bat for the establishment of batch documents in C:mywebmulu are as follows:

Startup.bat:

C:borlandjbuilder2005jdk1.4injavaw-classpath "c:borlandjbuilder2005 hirdpartyjakarta-tomcat-5.0.27in Ootstrap.jar; C:borlandjbuilder2005jdk1.4lib Ools.jar ""-dcatalina.home=c:/borland/jbuilder2005/thirdparty/ jakarta-tomcat-5.0.27 "Org.apache.catalina.startup.bootstrap-config" C:mywebtomcatconfserver8080.xml "Start"

Shutdown.bat:

C:borlandjbuilder2005jdk1.4injavaw-classpath "c:borlandjbuilder2005 hirdpartyjakarta-tomcat-5.0.27in Ootstrap.jar; C:borlandjbuilder2005jdk1.4lib Ools.jar ""-dcatalina.home=c:/borland/jbuilder2005/thirdparty/ jakarta-tomcat-5.0.27 "Org.apache.catalina.startup.bootstrap-config" C:mywebtomcatconfserver8080.xml "Stop"

13. Run Startup.bat, enter http://localhost:8080/DBTest/test.jsp in browser

How do I deploy a Web application?

1. Pack and build the war file

2. Copy Dbtest.war to Tomcat_homewebapps

3. Create a file in the Tomcat_homeconfcatalinalocalhost directory Dbtest.xml

Dbtest.xml

!--
Context configuration file for the Tomcat administration Web App
$Id: Admin.xml,v 1.2 2002/07/23 12:13:05 Remm EXP $

-->

<context path= "/dbtest" docbase= "/dbtest" debug= "5" reloadable= "true" crosscontext= "true" workdir= ". /work/dbtest ">

<logger classname= "Org.apache.catalina.logger.FileLogger" prefix= localhost_dbtest_log. "suffix=". txt "timestamp=" True '/>

<resource name= "Jdbc/testdb" auth= "Container" type= "Javax.sql.DataSource"/>
<resourceparams name= "Jdbc/testdb"
<parameter>
<name> Factory </name>
<value> org.apache.commons.dbcp.BasicDataSourceFactory </value>
</parameter>
!--
Maximum number of DB connections in pool. Make sure
Configure your mysqld max_connections large enough to handle
All of your DB connections. Set to 0 for no limit.
-->
<parameter>
<name> maxactive </name>
<value> </value>
</parameter>
!--
Maximum number of idle DB connections to retain in pool.
Set to 0 for no limit.
-->
<parameter>
<name> Maxidle </name>
<value> </value>
</parameter>
!--
Maximum a DB connection to become available
In MS, example seconds. An Exception is thrown if
This timeout is exceeded. Set to-1 to wait indefinitely.
-->
<parameter>
<name> maxwait </name>
<value> 10000 </value>
</parameter>
!--MySQL DB username and password for DB connections-->
<parameter>
<name> username </name>
<value> SA </value>
</parameter>
<parameter>
<name> Password </name>
<value> Topcomputer </value>
</parameter>
!--Class name for mm.mysql JDBC driver-->
<parameter>
<name> Driverclassname </name>
<value> Com.microsoft.jdbc.sqlserver.SQLServerDriver </value>
</parameter>
!--
The JDBC connection URL for connecting to your MySQL DB.
The autoreconnect=true argument to the URL makes sure
Mm.mysql JDBC Driver'll automatically reconnect if mysqld closed the
Connection. Mysqld By default closes idle connections after 8 hours.
-->
<parameter>
<name> URL </name>
<value> jdbc:microsoft:sqlserver://nt04:1433;databasename=test </value>
</parameter>
</ResourceParams>
</Context>


4. Start Tomcat,dbtest.war will extract to Tomcat_homewebappsdbtest and generate Dbtest directory in Tomcat_homeworkcatalinalocalhost directory

Why can't I generate a war file?

Set the build-related property build Web archive in the Proterties for Web Module dialog box.

How do I add directories and files to a Web application?

Right-click the module directory, select New-directory from the pop-up menu, enter the directory name, or right-click the directory where you want to create the file, select New-file in the pop-up menu, select a file type, and enter a filename. Note that the file you are adding can only be a specified file type. The directories and files that are added are packaged into the war file.

How do I add other types of files?

You can copy the file to the specified directory, set the property content in the Proterties for Web Module dialog box, select Include all classes and resources, and then package the joined files into the war file.

How do I use the specified JDK?

Select Menu Tools-configure-jdks, press the New button in the pop-up dialog box, and then select the JDK path.

Select Menu Project-project Properties and select the JDK to join in the pop-up dialog box.

How do I use the specified tomcat?

Select the menu enterprise-configure Servers, select Tomcat5.0 in the pop-up dialog box, and press the Copy button.

Select the server copy of copy of Tomcat 5.0, select Home Directory

Select Menu Project-project Properties, set property Server in the pop-up dialog box, select the joined Tomcat server

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.