Rabbit Eight Brother Note 1: Java beginner notes

zhaozj2021-02-16  51

1.JDK configuration:

Path: c: /jdk1.4/bin; (The purpose is to execute Javac, Java, Jar, etc. command in any position)

Classpath:.; C: /jdk1.4/lib/dt.jar; c: /jdk1.4/lib/tools.jar; c: /jdk1.4/lib/j2ee.jar;

Note: "." Represents the current path. J2EE.JAR includes a class such as servlet, otherwise the error such as servlet and httpservletResponse can be found when compiling the servlet file.

2.Tomcat must be configured:

Java_Home: such as: C: /JDK1.4;

Catalina_Home: (that is, Tomcat installation root directory), such as: c: /jakarter9/thirdparty/jakarta-tomcat-4.1.24-le-jdk14

3. Antant must configure:

Ant_home: is also the root directory of Ant installation

Also add ant_home / bin to the path (the purpose is to execute the ANT command at any location).

4. JAR usage:

JAR's usage is simple, as long as the JDK configuration is correct, you can use the jar command to pack .jar files.

If I wrote a simple access to the servlet, the code is as follows:

============= servlet1.java ===============================================================================================================================================================================

// This is a servlet that is generated by jbuilder wizard

Package Untitled4;

Import javax.servlet. *;

Import javax.servlet.http. *;

Import java.io. *;

Import java.util. *;

Import java.sql. *;

Public class servlet1 extends httpservlet {

Private static final string content_type = "text / html; charset = GBK";

// Initialize global variables

Public void init () throws servletexception {

}

// Handle GET request

Public void doget (httpservletRequest request, httpservletResponse response) throws servletexception, ioException {

Response.setContentType (Content_Type);

String r = ""; // to print the variables on the screen

ResultSet RS = null; // Results

Statement Stmt = NULL; // SQL statement

Connection conn = null; // Database connection

PrintWriter out = response.getwriter ();

Try {

Try {

Class.Forname ("com.mysql.jdbc.driver); // Loads Mysql's Drive Conn = DriverManager.getConnection (// Connect to the specified database, user root, password is empty

"JDBC: MySQL: // LocalHost / MyDB", "root", "");

STMT = conn.createstatement (); // Create a SQL statement object

String queryString = "select * from myTable"; // Construct query statement

RS = stmt.executeQuery (queryString); // Execute the query, if it is INSERT, UPDATE, DELETE uses the ExecuteUpdate method.

While (rs.next ()) {// If the result set returned by the query is not aircraft loop

String name = rs.getstring ("name"); // Get the value of the Name field

String id = rs.getstring ("id"); // Get the value of the ID field

R = R ":" Name "," ID; // Get the variable value to be printed

}

}

Finally {

IF (rs! = null) {

rs.close (); // Close result set

}

IF (stmt! = null) {

Stmt.close (); // Turn off the SQL statement object

}

IF (conn! = null) {

Conn.close (); // Turn off the database connection object

}

}

}

Catch (SQLException E) {

}

Catch (classnotfoundexception e) {

}

/ / Output Variable R

Out.println ("");

Out.println ("");

Out.println ("r =");

Out.println (r);

Out.println ("");

Out.println ("");

}

// Remove resources

Public void destroy () {

}

}

========================================

First compile, Javac -d. Servlet1.java first, as shown in the figure:

-d. Refers to a folder named named in the current path to generate the package specified in the code, and put the compiled .class file in this folder.

This example will generate a folder named untricled4. After the compilation is successful, a folder Untitled4 will be generated. There is a compiled servlet1.class file.

JAR's help file is as follows:

After the compilation is successful, you can pack it into .jar files, the practice is as follows:

JAR CVF 4.jar Untitled4 /.

Figure:

Note: 4. JAR is the name of the .jar file after the package, untricted4 is the directory where .Class is located, refers to the files in the specified directory in 4.jar this .jar file.

Enclose a folder B in the Tomcat's WebApps directory, create a new folder web-inflight in B. If you create a new folder lib in web-inflight, copy the 4.jar file to B / Web-INF / LIB. If you don't want to put it under WebApps, you want to modify Server.xml, add your own custom path:

If you want to add D: / LTF to the path / mydir specified by yourself, add the following to the of this file as follows:

Reloadable = "true" crossText = "true">

. Here is just to introduce this usage, follow the procedures to be placed in WebApps.

Create a file web.xml in B / WEB-INF. The content is as follows:

servlet1

Untitled4.Servlet1

servlet1

/ servlet1

After writing this file, the directory structure of web-infers is as follows:

The files in the lib are as follows:

MySQL drivers can also be placed here, it is best to place in the lib directory of Tomcat's CommON, because there are all applications that can be deployed on Tomcat.

There is also another class directory in Web-INF. This directory is placed directly. Class files that are packaged into .jar.

Our servlet1.class can be placed in this directory, but you have to keep the Untitled4 folder to copy the original structure, otherwise you can't run. Now let's take a look at the implementation effect:

Start Tomcat:

Perform Startup.bat in the bin directory in the Tomcat root directory,

The following window appears:

Start IE, enter the address bar: http: // localhost: 8080. "8080" is the port number to change, the change method is to modify the server.xml in the Tomcat's conf directory.

As long as "8080" is changed to the number specified by its own, such as "80".

Let's take a look at the execution results:

Please note that I entered http: // localhost: 8080 / b / servlet1, is servlet1, not servlet1, if you enter servlet1, an error occurs:

why? Our class name is clearly uppercase servlet1, rather than lower-written servlet1, why is you can't enter uppercase? The answer is in Web.xml, please note the value of the URL-Pattern section:

If you change the / servlet1 to / servlet1, you will enter your uppercase, otherwise you will have an error just entered.

My data in my MySQL database is as follows:

I posted this picture, introducing you this MySQL console, this console is a visualized tool developed by MySQL vendors. I found very similar to the console of the SQLServer with MS during use.

It is currently a Beta version of 0.9.3, which is a multi-language version. I found a small bug in the process of using the part in the figure, and this part should be Chinese characters, but the display is garbled. Although some bugs, this tool is a life-saving straw for my mysql's newcomer. If you need to download the official website of MySQL, you look forward to the official version.

In addition: The example of just now can be packaged into. War file deployment, the process is as follows:

Note that this example must be packaged in the web-inflicity file instead of packaging the outer folder B, and in the command line Web-INF must be capitalized, otherwise it cannot be performed correctly after deployment.

Put the packaged C. War to the directory of the Tomcat's WebApps, as shown:

The C directory in this folder is automatically unnormal after Tomcat startup.

Start Tomcat, open IE, type: http: // localhost: 8080 / c / servlet1, the result is as follows:

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

New Post(0)