Author: Crossing the Divide 2007. URL: http://blog.csdn.net/stupidwu/archive/2006/12/07/1433142.aspx
Many problems will be encountered in the process of configuring the jsp development environment. I would like to thank many seniors on the Internet for their experience summaries. The author Articles such as cl41's JSP connection guide to MySQL database and the author Saulzy's MySQL study notes are just timely help for me. In order to help beginners like me, I have summarized the experience of many authors here. I am very grateful to them for their selflessness. The spirit of dedication, and I hope more people will carry forward this spirit. Haha, I once again express my highest respect to the author of the referenced article!
If the following software download link address cannot be displayed, please go directly to the official website to download it!
Software download
Mysql
Download version: mysql-4.1.13-win32.zip (Windows downloads)
http://dev.mysql.com/downloads/mysql/4.1.html
JDBC driver download version: mysql-connector-java-3.1.10.zip
http://dev.mysql.com/downloads/connector/j/3.1.htmlDownload
j2sdk (JAVA 2 PLATFORM STANDARD EDITION DEVELOPMENT KIT 5.0)
Download version: jdk-1_5_0_04-windows-i586-p.exe
http://java.sun.com/j2se/1.5.0/download.jsp
download tomcat
Download version: jakarta-tomcat-5.5.9.exe
http://jakarta.apache.org/site/downloads/downloads_tomcat-5.cgi
installation, configure j2sdk:
Execute the j2sdk installation program and install according to the default settings. You can also customize the path, but you need to modify the following configuration
to configure j2sdk:
Configure environment variables:
Add the following environment variables to My Computer->Properties->Advanced->Environment Variables->System Variables:
JAVA_HOME=C:Program FilesJavajdk1.5.0_04
CLASSPATH=%JAVA_HOME%libdt.jar;%JAVA_HOME%libtools.jar;
path=%JAVA_HOME%bin;%SystemRoot%system32;%SystemRoot%;%SystemRoot%System32WbemWrite
a simple java program to test whether J2SDK has been installed successfully:
public class hello
{
public static void main(String args[])
{
System.out.println("Hello");
}
}
Save the program as a file named hello.java.
Open a command prompt window, go to the directory where hello.java is located, and type the following command
javac hello.java
java hello
If hello is printed at this time, the installation is successful. If this sentence is not printed, carefully check whether the above configuration is correct.
Note that "Hide extensions for known file types" should be unchecked in the system folder options (My Computer? Tools? View)
to install and configure tomcat
Execute the tomcat installation program and install according to the default settings. You can also customize the path, but you need to modify the following configuration.
Add the following environment variables to My Computer->Properties->Advanced->Environment Variables->System Variables.
CATALINA_HOME=C:Program FilesApache Software FoundationTomcat 5.5
CATALINA_BASE=C:Program FilesApache Software FoundationTomcat 5.5
Modify the classpath in the environment variable and add servlet-api.jar under commonlib in the tomat installation directory to the classpath.
The modified classpath is as follows:
classpath=%JAVA_HOME%libdt.jar;%JAVA_HOME%libtools.jar;C:Program FilesApache Software FoundationTomcat 5.5commonlibservlet-api.jar;
Start tomcat in IE Visit http://localhost:8080 . If you see the welcome page of tomcat, the installation is successful.
Install mysql
Unzip mysql-4.1.13-win32.zip and run setup.exe
The first thing that appears is the installation wizard welcome interface, just click "Next"
Continue, select the installation type, select "Custom" custom installation, and then click "Next" to next step. The custom installation interface will appear. Select the installation path: C:MySQL Server 4.1 (customizable)
Click "OK" to return to the custom installation interface. The path has been changed to the set path. Point "Next" and prepare to start the installation point "Install". After the installation is completed, the interface for creating a MySQL.com account will appear. If this is the first time to use MySQL, select " Create anew free MySQL.com accout”
Click "Next", enter your email address and the password you set to log in to MySQL.com. After filling in, click "Next" to enter the second step. Fill in your name and other related information. After filling in, click "Next" to enter the third step. After filling in the phone number, company name and other information, click "Next".
Then an interface for previewing the information you just filled in will appear. Click "Next" to display the installation completion interface. Note that there is an option for the configuration wizard (Configure the MySQL Server now).
It is recommended to check Configure your MySQL now. Many people say that MySQL cannot be started after installing it because MySQL is not configured.
Click "Finish" to complete the installation and start configuring MySQL
Click "Next" to enter the configuration type selection page. Select "Detailed configuration"
Click "Next" to enter the service type selection page. Select "Developer Machine" so that it does not occupy a lot of system resources. After clicking "Next", enter the database usage selection page. Select "Multifunctional Database"
Click "Next" to enter the page for selecting the InnoDB data storage location. You don't need to change the settings. Just put it directly in the Installation Path installation directory. Then click "Next" and select the number of simultaneous connections for MySQL. Select "Manual Setting" and set it to 100 (according to Set according to your own needs)
Click "Next" to configure MySQL's port in the TCP/IP communication environment and select the default port 3306.
Click "Next" and select the character settings in MySQL. Note that the selection here will affect whether you can use Chinese in MySQL. Select the gb2312 character set to support Simplified Chinese and click "Next". Set the Windows service options. Note that the selection here is critical.
"Install As Windows Service" must be checked, which is to run MySQL as a Windows service.
Use the default "MySQL" for "Service Name"
The "Launch the MySQL Server automatically" below must be checked, so that MySQL will automatically start the service when Windows starts. Otherwise, MySQL must be started manually.
Many people say that after installing MySQL, it cannot start, cannot connect, and a 10061 error occurs. This is the reason.
Click "Next" to set the login password of the root account root. "Modify Security Settings" is to set the password of the root account. Enter the password you set.
"Create An Anonymous Account" is to create an anonymous account. This will cause unauthorized users to illegally access your database, which is a security risk. It is recommended not to check it.
Click "Next", the MySQL configuration wizard will configure MySQL based on all the settings you set above, so that the operation of MySQL meets your needs. Click "Execute" to start the configuration. When "Service started successfully" appears, your configuration is complete, MySQL After the service starts successfully, click "Finish" to complete. The entire MySQL configuration is completed. All that remains is to use the MySQL client to connect to the MySQL server and then use it.
Install JDBC driver:
Unzip mysql-connector-java-3.1.10.zip
that will be used are mysql-connector-java-3.1.10-bin-g.jar and mysql-connector-java-3.1.10-bin.jar.
Create the mysqlforjdbc subdirectory in the C:Program FilesJava directory and enter Move mysql-connector-java-3.1.10-bin.jar to this directory and enter the C:Program FilesJavajdk1.5.0_04lib directory and change mysql-connector-java-3.1.10-bin-g Copy .jar to the directory and configure the classpath, append %JAVA_HOME%libmysql-connector-java-3.1.10-bin-g.jar;C:Program FilesJavamysqlforjdbcmysql-connector-java- 3.1.10-bin.jar; Go to this environment variable and append the following environment variables:
CLASSPATH=%JAVA_HOME%libdt.jar;%JAVA_HOME%libtools.jar;C:Program FilesApache Software FoundationTomcat5.5commonlibservlet-api.jar;%JAVA_HOME% libmysql-connector-java-3.1.10-bin-g.jar;C:Program FilesJavamysqlforjdbcmysql-connector-java-3.1.10-bin.jar;
The purpose of configuring this is to allow the java application to find the driver connected to mysql.
View and start the MySQL service. After installing MySQL under Windows XP, it has automatically started the service, and there is a shortcut connection to its client in the start menu, which can be viewed through the Windows Service Manager. "Start" - "Run", enter "services.msc" and press Enter.
Pop up the Windows Service Manager, and then you can see the service item named "mysql", with "Started" marked on the right
In the Start menu - All Programs - MySQL - MySQL Server 4.1 - MySQL Command Line Client, use the client's shortcut to connect and enter the password set during the installation.
Use the database
After Mysql is installed, go to Start Menu - All Programs - MySQL - MySQL Server 4.1 - MySQL Command Line Client. Use the client's shortcut to connect and enter the password set during installation.
Use the basic commands of mysql (edit each input in the mysql command line There must be a semicolon at the end after completing the command)
Show databases: show databases;
Use database: use database name;
To build a database, create a database first in mysql, and create a table about in the database.
Command: create database first;
set permissions (user and password) for the database
Command: grant all privileges on first.* to test@localhost identified by “123456”;
After you execute this command, as long as you log in with username: test, password: 123456, you can only operate on the first database, thus avoiding the use of root.
Enter the command: use first;
Use the first database;
create a table command in the first database: create table about(id int(8) primary key,name varchar(10));
if the data in the table:
Command: insert into about values('xyw1026','laojiang');
Exit command: exit
JSP connection mysql
Create the subdirectory myapp in the C:Program FilesApache Software FoundationTomcat5.5webapps directory
Enter the C:Program FilesApache Software FoundationTomcat5.5webappsmyapp directory and use Notepad to write a file and save it as first.jsp
The code is as follows:
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="java.sql.*"%>
<html>
<body>
<%Class.forName("org.gjt.mm.mysql.Driver").newInstance();
String url ="jdbc:mysql://localhost/softforum?user=soft&password=soft1234&useUnicode=true&characterEncoding=8859_1"
//first is your database name
Connection conn= DriverManager.getConnection(url);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from first";
ResultSet rs=stmt.executeQuery(sql);
while(rs.next()) {%>
Your first field content is: <%=rs.getString(1)%>
Your second field content is: <%=rs.getString(2)%>
<%}%>
<%out.print("Database operation is successful, congratulations");%>
<%rs.close();
stmt.close();
conn.close();
%>
</body>
</html>
Enter in the browser:
http://127.0.0.1:8080/myapp/first.jsp
If it appears:
id|num
0 | laojiang
database operation is successful, congratulations on
successful configuration
Enter the C:Program FilesApache Software FoundationTomcat5.5webappsmyapp directory and create a new directory WEB-INF under the myapp directory. Note that the directory name is case-sensitive;
Use Notepad under WEB-INF to create a new file named web.xml with the following content:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
" http://java.sun.com/dtd/web-app_2_3.dtd ">
<web-app>
<display-name>My Web Application</display-name>
<description>
A application for test.
</description>
</web-app>
Use Notepad to create a new test jsp page under myapp. The file name is index.jsp. The file content is as follows:
<html><body><center>
Now time is: <%=new java.util.Date()%>
</center></body></html>
Restart Tomcat
Open the browser and enter http://localhost:8080/myapp/index.jsp
If you see the current time, it means the installation is successful.
Build your own Servlet:
Use Notepad to create a new servlet program with the file name HelloWorld.java and the file content as follows:
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloWorld extends HttpServlet
{
public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html><head><title>");
out.println("This is my first Servlet");
out.println("</title></head><body>");
out.println("<h1>Hello,World!</h1>");
out.println("</body></html>");
}
}
Compile and cut HelloWorld.java to the C:Program FilesApache Software FoundationTomcat5.5commonclasses directory and enter in command line mode:
cd C:Program FilesApache Software FoundationTomcat5.5commonclasses
javac HelloWorld.java
Use javac HelloWorld.java to compile this file. If it fails to import javax.servlet.*
Then copy and add the servlet-api.jar file in the C:Program FilesApache Software FoundationTomcat5.5commonlib directory to
Just go to the C:Program FilesJavajre1.5.0_04libext directory
and then a compiled servlet file will be generated under C:Program FilesApache Software FoundationTomcat5.5commonclasses: HelloWorld.class
Use Notepad to open the web.xml in the C:Program FilesApache Software FoundationTomcat5.5webappsROOTWEB-INF directory as shown below. The added content is as follows:
.
.
.
<!-- JSPC servlet mappings start -->
<servlet>
<servlet-name>org.apache.jsp.index_jsp</servlet-name>
<servlet-class>org.apache.jsp.index_jsp</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>org.apache.jsp.index_jsp</servlet-name>
<url-pattern>/index.jsp</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>HelloWorld</servlet-name>
<servlet-class>HelloWorld</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorld</servlet-name>
<url-pattern>/servlet/HelloWorld</url-pattern>
</servlet-mapping>
<!-- JSPC servlet mappings end -->
</web-app>
The servlet section in this paragraph declares the Servlet you want to call, and servlet-mapping is the declared servlet " Mapping" to the /servlet/HelloWorld address,
start Tomcat, start the browser, enter http://localhost:8080//servlet/HelloWorld, if you see the output HelloWorld! This means that the servlet written is successful.
Note: If you modify web.xml and add a new class, you must restart Tomcat.
You can also test in a directory you created, such as:
Copy HelloWorld.class to the C:Program FilesApache Software FoundationTomcat5.5webappsmyappWEB-INFclasses directory. If the classes directory does not exist, create one and open C:Program FilesApache with Notepad. The web.xml in the Software FoundationTomcat5.5webappsmyappWEB-INF directory is as follows. The modified content is as follows:
.
.
.
<!-- JSPC servlet mappings start -->
<servlet>
<servlet-name>HelloWorld</servlet-name>
<servlet-class>HelloWorld</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorld</servlet-name>
<url-pattern>/servlet/HelloWorld</url-pattern>
</servlet-mapping>
<!-- JSPC servlet mappings end -->
</web-app>
Start Tomcat, start the browser, enter http://localhost:8080/myapp/servlet/HelloWorld and if you see the output HelloWorld! This means that the servlet written is successful.
Note: If you modify web.xml and add a new class, you must restart Tomcat.
Create your own javaBean:
Use Notepad to create a new java program. The file name is TestBean.java. The file content is as follows:
package test;
public class TestBean{
private String name = null;
public TestBean(String strName_p){
this.name=strName_p;
}
public void setName(String strName_p){
this.name=strName_p;
}
public String getName(){
return this.name;
}
}
Compile and place TestBean.java under c:test and use the following command to compile:
C:test>javac TestBean.java
Then a compiled bean file will be generated under c:Test: TestBean.class
Cut the TestBean.class file to the C:Program FilesApache Software FoundationTomcat5.5webappsmyappWEB-INFclassestest directory,
If there are no subdirectories, create a
new TestBean.jsp file. The file content is:
<%@ page import="test.TestBean" %>
<html><body><center>
<%
TestBean testBean=new TestBean("This is a test java bean.");
%>
Java bean name is: <%=testBean.getName()%>
</center></body></html>
Restart Tomcat, start the browser, enter http://localhost:8080/myapp/TestBean.jsp. If you see the output Java bean name is: This is a test java bean
This means that the javaBean written was successful.