my code

2017-05-05  本文已影响0人  hackywit

1.最简单的doGet

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        res.setContentType("text/html;charset=GB2312");
        PrintWriter out = res.getWriter();
        out.println("<html><head><title>HelloServlet</title></head>");
        out.println("<body>");
        out.println("你好:kinco");
        out.println("</body></html>");
    }

2.最简单的读取web项目下的文件

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        ServletContext context = getServletContext();
        BufferedReader reader = new BufferedReader(new InputStreamReader(context.getResourceAsStream("/main.html")));
        PrintWriter out = res.getWriter();
        String temp = "";
        StringBuffer sb = new StringBuffer();
        while((temp = reader.readLine()) != null) {
            sb.append(temp);
        }
        out.println(sb.toString());
        reader.close();
        out.close();
    }

3.最简单的读取数据库

package cn.hackywit.jdbc;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

public class JdbcUtil {
    private static String className;
    private static String url;
    private static String user;
    private static String password;
    static{
        try {
            InputStream in = JdbcUtil.class.getClassLoader().getResourceAsStream("dbinfo.properties");
            Properties props = new Properties();
            props.load(in);
            className = props.getProperty("className");
            url = props.getProperty("url");
            user = props.getProperty("user");
            password = props.getProperty("password");
            Class.forName(className);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static Connection getConnection() throws Exception{
        return DriverManager.getConnection(url,user,password);
    }
    public static void release(ResultSet rs,Statement stmt,Connection conn){
        if(rs!=null){
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            rs = null;
        }
        if(stmt!=null){
            try {
                stmt.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            stmt = null;
        }
        if(conn!=null){
            try {
                conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            conn = null;
        }
    }
}
package cn.hackywit.jdbc;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

public class mysql {
    public static void main(String[] args) {
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            conn = JdbcUtil.getConnection();
            // 3.创建sql语句对象Statement
            stmt = conn.createStatement();
            // 4.将sql语句对象发送给数据库处理并得到返回结果ResultSet
            rs = stmt.executeQuery("select password from user where username='hackywit'");
            // 5.遍历结果
            while(rs.next()){
                String pwd = (String) rs.getObject("password");
                if(pwd.equals("123456")){
                    System.out.println(pwd);
                }
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            throw new RuntimeException(e);
        } finally {
            // 6.释放所有的连接
            JdbcUtil.release(rs, stmt, conn);
        }
    }
}

dbinfo.properties

className=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/kincodb?characterEncoding=utf8&useSSL=false
user=root
password=123

4.读取.properties配置文件

//利用ServletContext读取a1.properties
    private void test1() throws IOException, FileNotFoundException {
        ServletContext sc = getServletContext();
        String path = sc.getRealPath("/WEB-INF/a1.properties");
        Properties props = new Properties();
        props.load(new FileInputStream(path));
        String value = props.getProperty("username");
        System.out.println(value);
    }
    //利用ServletContext读取a2.properties
    private void test2() throws IOException, FileNotFoundException {
        ServletContext sc = getServletContext();
        String path = sc.getRealPath("/WEB-INF/classes/a2.properties");
        Properties props = new Properties();
        props.load(new FileInputStream(path));
        String value = props.getProperty("username");
        System.out.println(value);
    }
    //利用ServletContext读取a3.properties
    private void test3() throws IOException, FileNotFoundException {
        ServletContext sc = getServletContext();
        String path = sc.getRealPath("/WEB-INF/classes/cn/itcast/resources/a3.properties");
        Properties props = new Properties();
        props.load(new FileInputStream(path));
        String value = props.getProperty("username");
        System.out.println(value);
    }
    //------------------------------------------------------------------
    //利用ResourceBundle读取配置文件a2.properties
    private void test4() throws IOException, FileNotFoundException {
        ResourceBundle rb = ResourceBundle.getBundle("a2");//基名
        String value = rb.getString("username");
        System.out.println(value);
    }
    //利用ResourceBundle读取配置文件a3.properties
    private void test5() throws IOException, FileNotFoundException {
        ResourceBundle rb = ResourceBundle.getBundle("cn.itcast.resources.a3");//基名
        String value = rb.getString("username");
        System.out.println(value);
    }
    //------------------------------------------------------------------
    //利用类加载器读取配置文件a2.properties
    private void test6() throws IOException, FileNotFoundException {
        ClassLoader cl = ServletDemo7.class.getClassLoader();//得到ServletDemo7的类加载器
        InputStream in = cl.getResourceAsStream("a2.properties");
        Properties props = new Properties();
        props.load(in);
        String value = props.getProperty("username");
        System.out.println(value);
    }
    //利用类加载器读取配置文件a3.properties
    private void test7() throws IOException, FileNotFoundException {
        ClassLoader cl = ServletDemo7.class.getClassLoader();//得到ServletDemo7的类加载器
        InputStream in = cl.getResourceAsStream("cn/itcast/resources/a3.properties");
        Properties props = new Properties();
        props.load(in);
        String value = props.getProperty("username");
        System.out.println(value);
    }
上一篇下一篇

猜你喜欢

热点阅读