代码改变世界

JAVA Web开发环境配置

2009-08-11 21:42  敏捷的水  阅读(4876)  评论(0编辑  收藏  举报

假设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

clip_image002

3.       Eclipse配置Tomcat

clip_image004

clip_image006

clip_image008

连接JDBC

1.       sqljdbc.jar拷贝到TomcatLib, JAVACLASSPATH要添加此驱动

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示例

clip_image010

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>