Developing a dynamic WEB project application with Eclipse (2012-12-04 11:23:08)
Tags: gossip |
Category: Web development |
http://blog.csdn.net/blue_fire2008/article/details/7525557
Summary: This article simply describes the configuration and development of Jsp,servlet and Tomcat data sources based on the Eclipse development Dynamic Web Project application.
Software Environment:
Eclipse Java EE IDE for WEB developers compressed package Eclipse-jee-indigo-sr2-win32.zip
JDK installation package Jdk-7u1-windows-i586.exe
Tomcat installation package Apache-tomcat-7.0.27.exe
MySQL Compression pack Mysql-6.0.4-alpha-win32.zip
MySQL JDBC Driver Jar Pack Mysql-connector-java-5.1.18-bin.jar
One, create the server
Select File > New > Other>serverfrom the menu to create the Server, as shown in.
Click Next, and then click Finish.
Two, create Dynamic Web Project Projects
1, choose File > New > Dynamic WebProject via menu, new project, project name HelloWorld, other value default.
2, the project resources are as shown.
Third, create a JSP file
1. Create the index.jsp file, right-click on the project,New >jspfile, as shown in, clicking Finish.
2, insert the following code in the <body></body> Center:
<% java.util.Date d =new java.util.Date ();%>
3, right-click the project, Run...>run on server, select the newly created server. The effect is as follows:
Four, create a servlet file
1, create a new Helloworldservlet, right-click on the project,new >servlet, as shown in, clicking Finish.
2, add the following code in the Doget method:
Response.getwriter (). Write ("Hello, world!");
3. Run the servlet and restart the server. The results are as follows:
Five, the configuration and development of the data source.
1, copy the MySQL JDBC driver jar package to the Lib directory in the Tomcat installation directory.
2. Modify servers under Tomcat v7.0 Server at Localhost-config/context.xml file as shown in:
3, insert the following code before </Context>:
<resourcename="Jdbc/bookds"auth="Container"type="javax.sql.DataSource" maxactive= " the"
maxidle="maxwait="10000"username=" root"password=" root "
Driverclassname= "com.mysql.jdbc.Driver"url="jdbc:mysql://localhost/bookdb"/>
4, create the JSP test page newfile.jsp file, insert the following code in the <body></body> Center:
<%
Context ctx=New initialcontext ();
Connection conn=null;
DataSource ds= (DataSource) ctx.lookup ("Java:comp/env/jdbc/bookds");
Conn=ds.getconnection ();
Statement stmt=conn.createstatement ();
ResultSet rs=stmt.executequery ("SELECT * from book");
while (Rs.next ()) {
Out.println (rs.getstring (1) + "<br>");
Out.println (rs.getstring (2) + "<br>");
}
Rs.close ();
Stmt.close ();
Conn.close ();
%>
5, specify the character set in the JSP header and import the related package.
<%@ page language="java"contenttype="text/html; CHARSET=GBK "
pageencoding= "GBK" import="java.sql.*,javax.sql.*,javax.naming.*"%>
6. Run newfile.jsp as shown in:
PostScript language:
1. The Web-inf directory for theDynamic Web Project project does not have an. xml file.
2.mysql user name and password are root, the database name is BOOKDB, the data source is named jdbc/bookds
The data source access code in the 3.JSP file is not placed in Try...catch, which is not rigorous.
4. When you create a database and a table in MySQL, the character encoding is specified as GBK, so there is no garbled language in the JSP.
5. Server is just for the convenience of developing and debugging Web projects, and you should modify the Web-inf/web.xml files under the Conf/server.xml file and web-side projects in the Tomcat installation directory when you are actually installing the Web application.
Developing a dynamic Web Project application with Eclipse