Developing web applications with JBuilder 2005

Time:2021-11-28

JBuilder is an open Java IDE, which integrates tomcat, Weblogic and other servers. Although JDK, Tomcat and Weblogic are constantly upgraded, we can still use their latest versions in JBuilder. Because the configuration of Tomcat server is complex, programmers who are used to Windows platform are often confused about the use of Tomcat. This paper gives an example of using database connection pool (DBCP) in Tomcat environment, explains the general steps of developing web applications with JBuilder, and answers some frequently encountered problems.

The JDK version of JBuilder 2005 is 1.4.2_ 04-b05, whose files are placed in the directory JBuilder_ Under homejdk1.4, the latest version of Tomcat is 5.0.27, and its files are placed in the directory JBuilder_ Home hirdparty jakarta-tomcat-5.0.27. The following first gives an example of using the database connection pool (DBCP) in the Tomcat environment.

1. File new project create a new project file, and enter the project file name myweb and directory C: myweb

2. Project project properties set the properties of the project file and select Tomcat as the server

3. File new creates a new web module (War)

Enter the name dbtest and directory dbtest of the web module

4. File new create a new JSP, enter the name of the JSP file test.jsp, generate the test.jsp file, and modify the content of test.jsp

  Test.jsp:

  <%@ page contentType=”text/html; charset=Big5″ %>
  <html>
  <head>
  <title>DB Test</title>
  </head>
  <body>
  <%
  foo.DBTest tst = new foo.DBTest();
  tst.init();
  %>
  <h2>Results</h2>
  Foo <%= tst.getFoo() %><br/>
  Bar <%= tst.getBar() %>
  </body>
  </html>

A runtime configuration named test will be generated.

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

5. File new class, enter the class name dbtest and package name foo, generate the 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 content of web.xml

  web.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. When F9 runs the application, Tomcat subdirectory will be generated in myweb directory, including conf subdirectory,

In Tomcat_ The dbtest.xml file is generated in the homeconf catalinalocalhost directory

8. Add the file server8080.xml in mywebtomcatconf directory to the project file and modify the content of server8080.xml

  server8080.xml:

  <?xml version=”1.0″ encoding=”UTF-8″?>
  <Server debug=”0″ port=”8081″ shutdown=”SHUTDOWN”>
  <Service name=”Catalina”>
  <Connector acceptCount=”10″ connectionTimeout=”60000″ debug=”0″ maxThreads=”75″ minSpareThreads=”5″ port=”8080″/>
  <Engine debug=”0″ defaultHost=”localhost” name=”Catalina”>
  <Host appBase=”C:myWebTomcatwebapps” autoDeploy=”false” debug=”0″ deployXML=”false” name=”localhost” unpackWARs=”false”>

  <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 you
  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>100</value>
  </parameter>
  <!–
  Maximum number of idle dB connections to retain in pool.
  Set to 0 for no limit.
  –>
  <parameter>
  <name>maxIdle</name>
  <value>30</value>
  </parameter>
  <!–
  Maximum time to wait for a dB connection to become available
  in ms, in this example 10 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 that the
  mm.mysql JDBC Driver will 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: Borland jbuilder2005 hirdparty jakarta-tomcat-5.0.27 commonlib directory

10. Establish the database test in SQL server, and the 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] (50) COLLATE Chinese_Taiwan_Stroke_CI_AS NULL ,
  [bar] [int] NOT NULL
  ) ON [PRIMARY]

  GO

Enter several records as test data.

  11. F9
12. Create batch files startup.bat and shutdown.bat in C: mywebmulu. The contents are as follows:

  startup.bat:

  C:BorlandJBuilder2005jdk1.4injavaw -classpath “C:BorlandJBuilder2005 hirdpartyjakarta-tomcat-5.0.27inootstrap.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.27inootstrap.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 and enter in the browser http://localhost:8080/DBTest/test.jsp

How to deploy web applications?

1. Package to generate war file

2. Copy dbtest.war to Tomcat_ HOMEwebapps

3. In Tomcat_ Create the file dbtest.xml in the homeconf catalinalocalhost directory

  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 you
  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>100</value>
  </parameter>
  <!–
  Maximum number of idle dB connections to retain in pool.
  Set to 0 for no limit.
  –>
  <parameter>
  <name>maxIdle</name>
  <value>30</value>
  </parameter>
  <!–
  Maximum time to wait for a dB connection to become available
  in ms, in this example 10 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 that the
  mm.mysql JDBC Driver will 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 and dbtest.war will be decompressed to Tomcat_ Homewebappsdbtest, and in Tomcat_ Generate dbtest directory in homeworkcatalinalocalhost directory

Why can’t a war file be generated?

Set the build related property build Web Archive in the protocols for web module dialog box.

How to add directories and files to web applications?

Right click module directory, select new directory in the pop-up menu, and enter the directory name; Or right-click the directory where you want to create a file, select new file in the pop-up menu, select file type, and enter the file name. Note that the files added in this way can only be the specified file type. In this way, the added directories and files will be packaged into the war file.

How to add other types of files?

You can copy the file to the specified directory, set the content attribute in the proteins for web module dialog box, and select include all classes and resources. In this way, you can also package the added file into the war file.

How to use the specified JDK?

Select the menu tools configure jdks, press the new button in the pop-up dialog box, and then select the path of JDK.

Select project project properties from the menu, and select the JDK to be added in the pop-up dialog box.

How to use the specified Tomcat?

Select the menu enterprise configure servers, select Tomcat 5.0 in the pop-up dialog box, and then press the copy button,

Select the server copy of Tomcat 5.0 generated by copy, and select home directory

Select the menu project project properties, set the property server in the pop-up dialog box, and select the Tomcat server to join

Recommended Today

Android master notes – start optimization

The only way to start and open the app, the first experience, is related to core data such as user retention and conversion rate; Start analysis startup type Android vitals can monitor the application cold, hot and warm startup time. Via ADB shell am start – w Execute the command to start and print the […]