Tomcat8 Java8 Eclipse4.4 servlet开发环境搭建

Tomcat8 Java8 Eclipse4.4 servlet开发环境搭建

 

概要

OS : Microsoft Windows [Version 6.0.6002]

eclipse. 4.4 : Version: Luna Service Release 2 (4.4.2) Build id: 20150219-0600

 

http://www.eclipse.org/

 

Java : java version "1.8.0_20" Java(TM) SE Runtime Environment (build 1.8.0_20-b26)

 

http://www.oracle.com/technetwork/java/javase/downloads/index-jsp-138363.html#javasejdk

 

Tomcat : Server version: Apache Tomcat/8.0.21 Server built:   Mar 23 2015 14:11:21 UTC

 

http://tomcat.apache.org/download-80.cgi

 

配置

new->server->Tomcat v8.0 Server

localhost

servername

Apache Tomcat v8.0

resource

 

编码UTF-8

File->New->Other

Web->Servlet

Create Servelt

 

例子

package example.part1;

 

import java.io.IOException;

import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

 

/**

 * Servlet implementation class HelloWorld

 */

@WebServlet("/HelloWorld")

public class HelloWorld extends HttpServlet {

     private static final long serialVersionUID = 1L;

     

    /**

     * @see  HttpServlet#HttpServlet()

     */

    public HelloWorld() {

        super();

        // TODO Auto-generated constructor stub

    }

 

     /**

      * @see  HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)

      */

     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

          // TODO Auto-generated method stub

          response.getWriter().write("Hello, World!");

     }

 

     /**

      * @see  HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)

      */

     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

          // TODO Auto-generated method stub

     }

 

}

 

New->Other

Web->JSP File

例子省略

 

SQLITE

 

http://www.sqlite.org/

 

sqlite 3.8.9

 

JDBC

 

https://bitbucket.org/xerial/sqlite-jdbc/downloads

 

sqlite3.exe testdb.sqlite

 

CREATE TABLE userinfo(

userid VARCHAR(10) PRIMARY KEY,

status INTEGER NOT NULL

);

 

INSERT INTO userinfo(userid, status) VALUES("aaaa1234", 0);

INSERT INTO userinfo(userid, status) VALUES("zzzz9876", 2);

 

select * FROM userinfo;

 

        Connection conn = null;

        Statement stmt = null;

        ResultSet rs = null;

 

        try {

          Class.forName("org.sqlite.JDBC");

          conn = DriverManager.getConnection("jdbc:sqlite:C:/sqlite/testdb.sqlite");

          stmt = conn.createStatement();

          rs = stmt.executeQuery("SELECT userid,status FROM userinfo");

 

          resp.setContentType("text/plain");

          while (rs.next()) {

              resp.getWriter().write("userid=" + rs.getString("userid") + ", ");

              resp.getWriter().write("status=" + rs.getString("status") + "\n");

          }

        } catch(Exception e) {

          e.printStackTrace();

        } finally {

          if (rs != null ) { try {rs.close(); } catch (SQLException e) {e.printStackTrace();} }

          if (stmt != null ) { try {stmt.close(); } catch (SQLException e) {e.printStackTrace();} }

          if (conn != null ) { try {conn.close(); } catch (SQLException e) {e.printStackTrace();} }

        }

 

 

Mysql

 

CREATE TABLE userinfo(

userid VARCHAR(10) PRIMARY KEY,

status INTEGER NOT NULL

)

ENGINE=InnoDB

;

 

INSERT INTO userinfo(userid, status) VALUES("aaaa1234", 0);

INSERT INTO userinfo(userid, status) VALUES("zzzz9876", 2);

 

Connection conn = null;

Statement stmt = null;

ResultSet rs = null;

 

try {

  Class.forName("com.mysql.jdbc.Driver").newInstance();

  conn = DriverManager.getConnection("jdbc:mysql://192.168.11.187/sampledb?user=username&password=xxxxx");

  stmt = conn.createStatement();

  rs = stmt.executeQuery("SELECT userid,status FROM userinfo");

 

  response.setContentType("text/plain");

  while (rs.next()) {

    response.getWriter().write("userid=" + rs.getString("userid") + ", ");

    response.getWriter().write("status=" + rs.getString("status") + "\n");

  }

} catch(Exception e) {

  e.printStackTrace();

} finally {

  if (rs != null ) { try {rs.close(); } catch (SQLException e) {e.printStackTrace();} }

  if (stmt != null ) { try {stmt.close(); } catch (SQLException e) {e.printStackTrace();} }

  if (conn != null ) { try {conn.close(); } catch (SQLException e) {e.printStackTrace();} }

}

 

(完)

 


你可能感兴趣的:(java8,tomcat8,servlet开发环境搭建,Eclipse4.4)