Configure the JSP environment of TOMCAT + ECLIPSE + MYSQL

Source: Internet
Author: User
Tags jboss

Step 1: (software preparation)

1.

J2SDK (download URL: http://java.sun.com/j2se)

TOMCAT (download URL: http://www.apache.org/dist/jakarta)

MYSQL (download URL: http://www.mysql.com/downloads)

MYSQL-CONNERCTOR-JAVA (download URL: http://www.mysql.com/downloads)

ECLIPSE-SDK (download URL: http://www.eclipse.org/download)

Tomcat (tomcatpluginv31.zip) Sysdeo Eclipse Tomcat Launcher plugin

(Download URL: http://www.sysdeo.com/eclipse,

Here I am using J2SDK1.4.2.08 and TOMCAT5.0.28 + ECLIPSE3.1 + tomcatPluginV31 (BETA ))

Appendix:

In addition, eclipse3.0.2contains Chinese plug-ins (nlpack-eclipse-sdk-3.0.x-win32.zip)

Download URL:

Http://sunsite.informatik.rwth-aachen.de/eclipse/downloads/drops/L-3.0.1_Translations-200409161125/

It can be used in conjunction with ECLIPSE3.0.2 (Chinese plug-ins are placed under Plugins );

The Lomboz plug-in also supports JSP syntax highlighting. Tanghan is an ECLIPSE database management plug-in and Jboss is a J2EE server software.

The URLs are:

Http://www.objectlearn.com/

Http://www.hibernate.org/

Http://www.Jboss.org/

 

Step 2: (software installation and configuration)

A. J2SDK Installation
1.
Install J2SDK as instructed. You can select a path and a named folder. I name it JDK and the installation path is C: \ JDK.
* Note: Install with an independent JRE is not checked, because it is included in the JDK tool. This option is selected by default!

2.
Set the environment variables and go to my computer --> properties --> advanced --> environment variables. Create the following variables and assign values:
The variable name JAVA_HOME is assigned "<JDK installation path directory>", for example, "C: \ JDK ;";
Variable name CLASSPATH value ".; % JAVA_HOME % \ lib \ tools. jar; % JAVA_HOME % \ lib \ dt. jar ;";
* Note: % JAVA_HOME % can be replaced by <JDK installation path directory>. In fact, % JAVA_HOME % is the above variable.
Variable PATH value: "% JAVA_HOME % \ bin ;";

3.
Now we can test it. Enter "java-version" in the command prompt. If the JDK version is displayed
It indicates that your JDK software is correctly installed. For example, if you press enter, the first line is displayed as java version "1.4.2.08 ".

4.
Use NotePad to create a txt file under the "x: \" Directory (your drive letter, my name is "d: \"). The input content is as follows:

Public class Test // Test is the class name
{
Public static void main (String [] args)
{
System. out. print ("This is a test! ");
}
}

Save it as the Test. java file in the "x: \" Directory (for example, my file is "d: \ Test. java ").
* Note: The file name must be the same as the class name, that is, the file name must be case sensitive and the input content must be the same.
Enter the directory of your "Test. java" file, that is, the "x: \" drive letter, and enter "javac Test. java ",
Press enter. If a prompt message is displayed, it may be caused by an error in environment variable settings or an error in the "Test. java" file.
If the drive letter "x: \" appears again, the "Test. java" file is correctly compiled. You can see the "Test. class" file in the "x: \" directory.
Now you can enter "java Test" at the input prompt and the result is "This is a test !".


B. TOMCAT installation

1.
The installation of Tomcat is very simple. Just follow the prompts one step! Select your preferred installation path ("D: \ tomcat ").
Tomcat automatically selects the independent jre attached to JDK installation. Here I select "C: \ jdk \ jre" in JDK ".
All other items are default and are not modified.
Start Tomcat now. Open IE and enter "http: // localhost: 8080/" in the address bar to view the welcome page of Tomcat.
* Note: If "http: // localhost: 8080/" is not available, you can use "http: // 127.0.0.1: 8080/". The two are the same.

2.
Set the environment variable again and go to my computer --> properties --> advanced --> environment variable,
The variable TOMCAT_HOME is assigned the value "<Tomcat installation path directory> ";
Select the variable CLASSPATH and append "% TOMCAT_HOME % \ common \ lib;" to the end ;",
After modification, the value of CLASSPATH should be ".; % JAVA_HOME % \ lib \ tools. jar; % JAVA_HOME % \ lib \ dt. jar; % atat_home % \ common \ lib ;".
* % TOMCAT_HOME % can also be replaced by <Tomcat installation path directory>.

3.
Now you can find the webapps folder in the Tomcat installation path directory, find the ROOT folder, and create a txt file in it. The input content is as follows:

<title>hello_html</title>
<body>
<center></body>

The hello.html file is created in the "% atat_home % \ webapps \ ROOT \ folder" directory.
For example, after creating a file, the file location is "D: \ tomcat \ webapps \ ROOT \ hello.html ".
Start (or restart) Tomcat again, open IE, and enter "http: // localhost: 8080/hello.html" in the address bar.
Displays the page with the "Hello Html" character.

4.
Return to the webapps directory to create a folder named jsp_test, create a new WEB-INF folder in the jsp_test folder, and create a txt file in it,
The input content is as follows:

<?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>JspTest</display-name>
<description> A test. </description>
</web-app>

Save as the web. xml file, that is, a new web. xml file is created in the "% TOMCAT_HOME % \ webapps \ jsp_test \ WEB-INF" directory.
For example, after I create a file, the file location is "D: \ tomcat \ webapps \ jsp_test \ WEB-INF \ web. xml ".
Return to the webapps directory to create a txt file. The input content is as follows:

<title>hello_jsp</title>
<body>
<center>
Now time is: <%=new java.util.Date()%>
<p>
<% out.print("Hello Jsp"); %>
</p>
</center>
</body>

Save it as the "index. jsp" file, that is, a new "index. jsp" file is created in the "% TOMCAT_HOME % \ webapps \ jsp_test \" directory.
For example, after creating a file, the file is located at "D: \ tomcat \ webapps \ jsp_test \ index. jsp ". Start (or restart) Tomcat and open IE
Enter "http: // localhost: 8080/jsp_test/" in the address bar. Now time is: "current time" is displayed in the center"
And Hello Jsp pages. Later, you can put your JSP file in this folder for debugging!

5.
Return to the WEB-INF folder in the jsp_test folder, and create a new class folder and a lib folder in the WEB-INF. For example, after creating a folder,
PATH is "D: \ tomcat \ webapps \ jsp_test \ WEB-INF \ class (and lib )". Create servlets in the class folder
And useBeans folder (used later ). Modify the web. xml file. The modified content is as follows:

<?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>JspTest</display-name>
<description> A test. </description>
<!--servlets-->
<servlet>
<servlet-name>servlets</servlet-name>
<servlet-class>servlets</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>servlets</servlet-name>
<url-pattern>/servlets/</url-pattern>
</servlet-mapping>
<!--servlets-->
<!--useBeans-->
<servlet>
<servlet-name>useBeans</servlet-name>
<servlet-class>useBeans</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>useBeans</servlet-name>
<url-pattern>/useBeans/</url-pattern>
</servlet-mapping>
<!--servlets-->
</web-app>

Create the "HelloTomcat. java" file in the "x: \" directory. The content is as follows:

Package servlets; // The package name in the servelts directory.

Import java. io. IOException;
Import java. io. PrintWriter;
Import javax. servlet. http. HttpServlet;
Import javax. servlet. ServletException;
Import javax. servlet. http. HttpServletRequest;
Import javax. servlet. http. HttpServletResponse;

Public class HelloTomcat extends HttpServlet
{
Protected void doGet (HttpServletRequest arg0, HttpServletResponse arg1)
Throws ServletException, IOException
{
PrintWriter out = arg1.getWriter ();
Out. print ("<center> ");
Out. print ("Hello Tomcat! ");
Out. print ("<br/> ");
Out. print ("This is my first servlet! ");
Out. print ("</center> ");
}
}

Compile with the following statement under the "x: \" drive letter:
"Javac-classpath.; <% TOMCAT_HOME %> \ common \ lib \ servlet-api.jar HelloTomcat. java"
* Note that "<% TOMCAT_HOME %>" must be in the actual directory of your Tomcat (for example, my directory is "D: \ tomcat ").
You can also append '<% TOMCAT_HOME %> \ common \ lib \ servlet-api.jar;' after the environment variable classpath ;".
Another convenient way is to repeat the servlet-api.jar file to '% atat_home % \ webapps \ jsp_test \ WEB-INF \
Lib ", the classes required by servlets in the jsp_test folder can be put in this file for reference! Other JSP or
This is also true for servelt applications.
After the completion, put your compiled servlet class file to "D: \ tomcat \ webapps \ jsp_test \ WEB-INF \
Servlets. Start (or restart) Tomcat, open IE, and enter "http: // localhost: 8080/jsp_test/" in the address bar /"
We should be able to see "Hello Tomcat!" displayed in the center !" And "This is my first servlet !" Page.

6.
Put the InstanceOfUseBean.html file and InstanceOfUseBean. jsp in the jsp_test directory InstanceOfUseBean. class
Under D: \ tomcat \ webapps \ jsp_test \ WEB-INF \ class \ useBeans.

Content of the InstanceOfUseBean.html file:

<?xml version="1.0" encoding="gb2312"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=gb2312"/>
<title>InstanceOfUseBean</title>
<body>
<table width="100%" height="100%" border="0">
<tr>
<td width="40%" valign="top">
<div align="left">
<p style="font-size:18;color:#0066CC;font-weight:bold;">Enterthe property for
the JSP page :</p>
<form method="post" action="InstanceOfUseBean.jsp" target="show_JSP">
<p>BackgroundColor: <input type="text" name="backgroundColor"></p>
<p>FontColor&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:<input type="text" name="fontColor"></p>
<p>FontSize&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :
<select name="fontSize">
<option value="1">1</option>
<option value="2">2</option>
<option value="3" selected="selected">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
</select>
</p>
<p><input type="submit" name="submit" value="submit"></p>
</form>
</div>
</td>
<td bgcolor="#0066CC"></td>
<td width="60%" valign="top">
<p style="font-size:18;color:#0066CC;font-weight:bold;">Hereis the viwe : </p>
<iframe align="right" id="show_JSP" name="show_JSP"width="100%" height="100%" frameborder="0" marginheight="0"marginwidth="0">show JSP</iframe>
</td>
</tr>
</table>
</body>

Content of InstanceOfUseBean. jsp file:

<?xml version="1.0" encoding="gb2312"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ page contentType = "text/html;charset=gb2312" %>
<%@ page language = "java" import="useBeans.InstanceOfUseBean"%>
<jsp:useBean id="InstanceOfUseBean" class="useBeans.InstanceOfUseBean"scope="request" />
<jsp:setProperty name="InstanceOfUseBean" property="backgroundColor"param="backgroundColor" />
<jsp:setProperty name="InstanceOfUseBean" property="fontColor"param="fontColor" />
<jsp:setProperty name="InstanceOfUseBean" property="fontSize"param="fontSize" />
<title>InstanceOfUseBean</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312"/>
<body bgcolor=<% out.print(InstanceOfUseBean.getBackgroundColor());%>>
<p align="center">

</p>
<center>
<font color = "<% out.print(InstanceOfUseBean.getFontColor()); %>"
size = "<% out.print(InstanceOfUseBean.getFontSize()); %>"
>
How are you?
</font>
<br>
<font color="red" size="7">
<jsp:getProperty name="InstanceOfUseBean" property="fontSize"/>
</font>
</center>

Content of the InstanceOfUseBean. java file:

package useBeans;

public class InstanceOfUseBean
{
private String backgroundColor = "";
private String fontColor = "";
private int fontSize = 1;
public InstanceOfUseBean()
{
}
public void setBackgroundColor(String backgroundColor)
{
this.backgroundColor = backgroundColor;
}
public String getBackgroundColor()
{
return this.backgroundColor;
}
public void setFontColor(String fontColor)
{
this.fontColor = fontColor;
}
public String getFontColor()
{
return this.fontColor;
}
public void setFontSize(int fontSize)
{
this.fontSize = fontSize;
}
public int getFontSize()
{
return this.fontSize;
}
}

Compile it into a class file. In fact, this is similar to servlets. How can I open IE by myself,
Enter "http: // localhost: 8080/jsp_test/InstanceOfUseBean.html !!!


C. installation and connection of MYSQL

1.
First extract the MYSQL-CONNERCTOR-JAVA to your favorite drive letter directory, and then enter the MYSQL-CONNERCTOR-JAVA after decompression of the file
Find a "mysql-connector-java-3.x.x-bin.jar" in the folder and copy it to "% TOMCAT_HOME % \ common \ lib \"
Under ".. \ WEB-INF \ lib \" in your JSP program folder (for example, "D: \ tomcat \ webapps \ jsp_test \ WEB-INF \ lib ").

2.
Windows is easy to install and can be installed as instructed. I also installed it under d: \. There is nothing to say, and there are run and configuration in the Start Menu.
Run the decompressed version by entering "D: \ MySQL \ bin \" in the Start Menu;

3.
After the installation and configuration are complete, run MYSQL. If the password is set, enter the password first. Otherwise, you can directly use it.
Run the following command to view the database:

"Mysql> show databases ;"
It is usually displayed in the dotted table box. The first database and the second database are mysql and test.
* Note: "mysql>" is a prompt. You do not need to enter it again. The last Semicolon ";" must exist. It indicates the end of the command line.

After you know which database you have, you can select the database test:
"Mysql> use test ;"

The table is added later:
"Mysql> create table t (id int (11) not null auto_increment, name varchar (50)
Not null, primay key (id ));"

Display table:
"Mysql> show tables ;"

Add a record for table t:
"Mysql> insert into t (id, name )"
"Mysql> value (123456, 'Hello ');"
Add more records using this method!

View records:
"Mysql> select * from t ;"

You can see the table record. It is generally normal to follow the above steps.

4.
Start TOMCAT, enter "http: // 127.0.0.1: 8080/admin/" in the IE column, and enter the account and password.
If the account has not been changed during TOMCAT installation, it should be "admin" and the password is blank.
Select Resources> Data sources to go to the Data Source configuration page, and select Create New Data Source in the drop-down box in the upper right corner,
Go to the configuration details page. The content is as follows:

JNDI Name: jdbc/mysql (available as your favorite Name)
Data Source URL: jdbc: mysql: // localhost: 3306/test (here is the name of the test Database above)
JDBC Driver Class: com. mysql. jdbc. Driver
User Name: root (your MYSQL User Name, default: root)
Password: ******** (your MYSQL Password)
Max. Active Connections: 4
Max. Idle Connections: 2
Max. Wait for Connection: 500
Validation Query:

The required information, such as the JNDI Name, can be entered based on your needs, except JDBC DriverClass.
* Note that the content of the Data Source URL may be: "jdbc: mysql: // <MYSQL Server IP address or name>/<Your Database Name> ".
Finally, click Save to Save and Commit Change to submit the changes. In this way, you can proceed to the next step.
Find the web. xml file under "% atat_home % \ conf \" and edit it to add content:

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app ....>
.
.
.
.
<resource-ref>
<description>DB Connection</description>
<res-ref-name>jdbc/mysql</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>

The vertex number is the original content that is omitted, and the added content is <resource-ref> to </resource-ref>.
At first glance, we can see that "jdbc/mysql" in <res-ref-name> jdbc/mysql </res-ref-name> must be consistent
The JNDI Name must be consistent when TOMCAT configures the data source.

5.

Everything is ready. You can write the code for testing as follows:

<%@ page contentType="text/html;charset=gb2312"%> 
<%@ page import="java.sql.*"%>

<body>
<%
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
String url ="jdbc:mysql://localhost/test?user=root&password=123456&useUnicode=true&characterEncoding=8859_1";
Connection conn= DriverManager.getConnection(url);
Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE);
String sql="select * from t";
ResultSet rs=stmt.executeQuery(sql);
out.println("<table align=\"center\" border=\"1\" width=\"100%\">");
while(rs.next())
{
if (!flag)
{
color ="#d3d3d3";
}
else
{
color ="#ffffff";
}
flag =!flag;
out.println("<tr bgcolor=\""+color+"\">");
String name = rs.getString("name");
int id = rs.getInt("id");
out.println("<td>"+rs.getRow()+"</td>");
out.println("<td>"+id+"</td>");
out.println("<td>"+name+"</td>");
out.println("</tr>");
}
out.println("</table>");
rs.close();
stmt.close();
conn.close();
}
catch(Exception e)
{

out.print("Exception:"+e.getMessage());
}
%>
</body>

Save it as MySQLTest. jsp and put it in the "../test/" directory. Restart TOMCAT to test it!
The result is a table.


Integration of D.ECLIPSE-SDK and TOMCAT

1.
Decompress the ECLIPSE-SDK of windows to "x: \" and change it to eclipse;
Tomcat (tomcatpluginv31.zip) Sysdeo Eclipse Tomcat Launcher plugin
Decompress the package to "x: \" and copy it to "eclipse \ plugins.

2.
If ECLIPSE is started for the first time, you are required to set the "workspace" path;
Check whether there are three TOMCAT icons. If not, check as shown above.

3.
Set the JRE Virtual Machine, select Window> Preferences> Java from the ECLIPSE menu, and select Installed JREs.
Add a JRE by clicking the Add button, select the JDK, and click OK to check the newly added items in the table.

4.
Set Tomcat and select Window> Preferences> Tomcat from the ECLIPSE menu (this will not happen if the Tomcat plug-in is not installed ),
Set, Tomcat Version: Your tomcat Version, Tomcat Home: <tomcat installation directory>;
Expand Tomcat items with the Advances option. Similarly, Tomcat base is the installation directory of tomcat;

5.
Restart ECLIPSE. Can I see the Tomcat Project in FILE> NEW? Now, create a new one!
Change the Project name to your favorite one (I will change it to tomcat_jsp ),
Can update server. xml file must be checked.

6.
Well, for convenience, place the web. xml (in the jsp_test \ WEB-INF folder) created for the jsp application in the jsp_test folder above ),
Copy to "eclipse \ workspace \ tomcat_jsp \ WEB-INF", this is the Project you just created. Also copy the index. jsp above
Go to "eclipse \ workspace \ tomcat_jsp \" to restart ECLIPSE. Expand tomcat_jsp in the navigation window on the left, and double-click index. jsp.
Open it in ECLIPSE and see the code! Start TOMCAT in the menu bar,
In IE, input "http: // localhost: 8080/tomcat_jsp/index. jsp". Is the same result as above?

7.
Try creating a JSP file! Right-click tomcat_jsp in the navigation window and select NEW-> FILE. The name must have the suffix ". jsp,
Content customization. Restart TOMCAT to test the function.
* Note: You must restart TOMCAT to modify the. xml file and create a new JSP file, but modify the existing JSP file without restarting.
The Servlet in ECLIPSE is similar to that in JSP. Refer to the above!

8.
To connect ECLIPSE to MYSQL, you only need to go to Project> Properties> Java Build path> Libraries in the ECLIPSE menu bar.
Press Add ExternalJARs to Add the MYSQL-CONNERCTOR-JAVA driver.

Now that the configuration is complete, you can start your JSP journey!

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.