Develop a dynamic WEB Project Application "Go" with eclipse

Source: Internet
Author: User
Tags stmt

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 the 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 (); %>

<H1>today's date is<%= d.tostring ()%></H1>

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 thefollowing code before </Context >:

 < resourcename= "JDBC/BOOKDS" auth= "Container" type= "Javax.sql.DataSource" maxactive" 100 "

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

3. Thedata source access code in the 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.

Develop a dynamic WEB Project Application "Go" with eclipse

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.