假设JDK安装到C:\JDK6, TOMCAT安装到c:\Tomcat6
配置JAVA
1. 系统变量PATH变量后添加C:\JDK6\bin
2. 系统环境变量添加
JAVA_HOME: c:\JDK6
CLASSPATH: c:\JDK6\lib\tools.jar; c:\JDK6\lib\dt.jar;
TOMCAT配置
1. 环境变量添加TOMCAT_HOME: c:\Tomcat6
2. 将c:\Tomcat6\common\lib\servlet-api.jar添加到CLASSPATH
3. Eclipse配置Tomcat
连接JDBC
1. 把sqljdbc.jar拷贝到Tomcat的Lib, JAVA的CLASSPATH要添加此驱动
2. 示例代码
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ page import="java.sql.*"%>
<html>
<body>
<%
Connection dbConn;
String err = "";
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver")
.newInstance();
dbConn = DriverManager
.getConnection(
"jdbc:sqlserver://10.10.50.127:1433;DatabaseName=master",
"sa", "abc");
} catch (Exception ex) {
dbConn = null;
err = ex.toString();
}
if (dbConn != null) {
out.println("True");
} else {
out.println("False");
out.println(err);
}
%>
</body>
</html>
Servlet示例
HelloWorld.java
package com.LB;
import java.io.IOException;
import java.io.*;
import javax.servlet.http.*;
import javax.servlet.*;
public class HelloWorld extends HttpServlet
{
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
// TODO Auto-generated method stub
// super.doGet(req, resp);
PrintWriter out = resp.getWriter();
out.println("Hello World!");
out.println("This is my first servlet program");
}
}
Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>HelloWorld</servlet-name>
<servlet-class>com.LB.HelloWorld</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorld</servlet-name>
<url-pattern>/Hello</url-pattern>
</servlet-mapping>
</web-app>
本文转自敏捷的水博客园博客,原文链接http://www.cnblogs.com/cnblogsfans/archive/2009/08/11/1543946.html如需转载请自行联系原作者
王德水