2018-01-25 解决发布到服务器的问题,发现新的问题

23,24在天上和火车上,24晚上到家就去吃烧烤!零下42℃我也要去吃!

打开电脑,啥玩意

我好像很长时间之前重装了电脑,劳资的环境全都没了,写个毛线代码。
最令人开心的是,家里没网,只能蹭楼下wifi。
用了一天,装的差不多了

  • jdk,jre 版本9.0.4
  • tomcat 9.0.2
  • eclipse jee oxygen(主要用java ee和java)
  • navicat premium 12(破解的乱七八糟的)
  • putty
  • mysql 5.7

原来有的

  • Xsell 5
  • FlashFXP 5

连上服务器

弄好了tomcat上http跳转https,参考
http://blog.csdn.net/bao19901210/article/details/8768362
用yum装上了mysql服务,参考
http://www.linuxidc.com/Linux/2016-09/135288.htm

测试代码

emmm,傻敷敷的把整个项目包复制到/usr/local/apache-tomcat-9.0.2/webapps/ROOT/下,结果和之前的一样500,然后在问学姐的时候突然发现之前百度的war包发布的方式我放错了位置,重试一下
把项目在eclipse里export成war包,然后上传到服务器/usr/local/apache-tomcat-9.0.2/webapps/下,运行成功,放炮去了。

接下来是测试项目包cuz_welcome的文件:

  • src/cuz_welcome/ConnectMysql.java
package cuz_welcome;

import java.sql.*;
import java.util.LinkedList;

public class ConnectMysql
{

    // JDBC 驱动名及数据库 URL
    static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
    static final String DB_URL = "jdbc:mysql://localhost:3306/studentdb";

    // 数据库的用户名与密码,需要根据自己的设置
    static final String USER = "root";
    static final String PASS = "root";

    public static String getStu()
    {
        // TODO Auto-generated constructor stub
        Connection conn = null;
        Statement stmt = null;
        LinkedList list = new LinkedList<>();

        try
        {
            // 注册 JDBC 驱动
            Class.forName("com.mysql.jdbc.Driver");

            // 打开链接
            // System.out.println("连接数据库...");
            conn = DriverManager.getConnection(DB_URL, USER, PASS);

            // 执行查询
            // System.out.println(" 实例化Statement对...");
            stmt = conn.createStatement();
            String sql;
            sql = "SELECT stuid, name, sex FROM t_student";
            ResultSet rs = stmt.executeQuery(sql);

            // 展开结果集数据库
            while (rs.next())
            {
                // 通过字段检索
                Student stu = new Student();
                stu.stuId = rs.getInt("stuid");
                stu.name = rs.getString("name");
                stu.sex = rs.getString("sex");

                list.add(stu);

                // 输出数据
                // System.out.print("ID: " + stu.stuId);
                // System.out.print(", 姓名: " + stu.name);
                // System.out.print(", 性别: " + stu.sex);
                // System.out.print("\n");
            }

            // 完成后关闭
            rs.close();
            stmt.close();
            conn.close();
        }
        catch (SQLException se)
        {
            // 处理 JDBC 错误
            se.printStackTrace();
        }
        catch (Exception e)
        {
            // 处理 Class.forName 错误
            e.printStackTrace();
        }
        finally
        {
            // 关闭资源
            try
            {
                if (stmt != null)
                    stmt.close();
            }
            catch (SQLException se2)
            {
            } // 什么都不做
            try
            {
                if (conn != null)
                    conn.close();
            }
            catch (SQLException se)
            {
                se.printStackTrace();
            }
        }
        // System.out.println("Goodbye!");
        return TestMapToJson.testMysqlToJson(list);
    }
}
  • src/cuz_welcome/Student.java
package cuz_welcome;

public class Student
{
    int stuId;
    String name;
    String sex;
}
  • src/cuz_welcome/estMapToJson.java
package cuz_welcome;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;


import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class TestMapToJson
{

    public static void main(String[] args)
    {
        // TODO Auto-generated method stub
        test();
    }

    public static void testMapToJSON(){
        Map map1 = new HashMap();  
        map1.put( "name", "json" );  
        map1.put( "bool", Boolean.TRUE );  
        map1.put( "int", new Integer(1) );  
        map1.put( "arr", new String[]{"a","b"} );  
        map1.put( "func", "function(i){ return this.arr[i]; }" );  
          
        JSONObject jsonObject = JSONObject.fromObject( map1 );  
        
        Map map2 = new HashMap();  
        map2.put( "name", "json" );  
        map2.put( "bool", Boolean.TRUE );  
        map2.put( "int", new Integer(1) );  
        map2.put( "arr", new String[]{"a","b"} );  
        map2.put( "func", "function(i){ return this.arr[i]; }" );  
        
        jsonObject.fromObject(map2);
        
        
        System.out.println( jsonObject );  
    }
    
    
    public static void test()
    {
        LinkedList li = new LinkedList<>();

        for (int i = 0; i < 10; i++)
        {
            Student stu = new Student();
            stu.stuId = 10001 + i;
            stu.name = "aa";
            stu.sex = "man";
            
            Map map = new HashMap<>();
            map.put( "stuId", new Integer(stu.stuId) );
            map.put("name", stu.name);
            map.put("sex", stu.sex);
            JSONObject jsonObject = JSONObject.fromObject( map );
            
            li.add(jsonObject);     
        }
            
        JSONArray jsonArray = JSONArray.fromObject( li );  
        
        Map stuMap = new HashMap();
        stuMap.put("students", jsonArray);
        JSONObject stuJsonObject = JSONObject.fromObject( stuMap );  

        System.out.println( stuJsonObject );    

    }
    
    public static String testMysqlToJson(LinkedList list)
    {
        LinkedList li = new LinkedList<>();

        for (int i = 0; i < list.size(); i++)
        {
            Student stu = new Student();
            stu.stuId = list.get(i).stuId;
            stu.name = list.get(i).name;
            stu.sex = list.get(i).sex;
            
            Map map = new HashMap<>();
            map.put( "stuId", new Integer(stu.stuId) );
            map.put("name", stu.name);
            map.put("sex", stu.sex);
            JSONObject jsonObject = JSONObject.fromObject( map );
            
            li.add(jsonObject);     
        }
            
        JSONArray jsonArray = JSONArray.fromObject( li );  
        
        Map stuMap = new HashMap();
        stuMap.put("students", jsonArray);
        JSONObject stuJsonObject = JSONObject.fromObject( stuMap );  

        System.out.println( stuJsonObject );   
        return stuJsonObject.toString();
        
    }
}
  • WebContent/testMysql.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ page import= "cuz_welcome.*"%>




Insert title here



<%=ConnectMysql.getStu()%>



  • WebContent/testMysql2.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ page import= "cuz_welcome.*"%>
<%@ page import="java.sql.*"%>




Insert title here



<%
        Connection con = null;
        Statement st = null;
        ResultSet rs = null;
        try
        {
            Class.forName("com.mysql.jdbc.Driver");
        }
        catch (ClassNotFoundException e)
        {
            e.printStackTrace();
        }
        try
        {
            con = DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/studentdb?useUnicode=true&characterEncoding=UTF-8&useSSL=false", "root",
                    "root");
            st = con.createStatement();
            rs = st.executeQuery("select * from student");
            out.print("");
            out.print("");
            out.print("");
            out.print("");
            out.print("");
            out.print("");

            while (rs.next())
            {
                int stuId = rs.getInt(1);
                String stuName = rs.getString(2);
                String stuSex = rs.getString(3);
                out.print("");
                out.print("");
                out.print("");
                out.print("");
                out.print("");
            }
            out.print("
学号姓名性别
" + stuId + "" + stuName + "" + stuSex + "
"); } catch (SQLException e) { e.printStackTrace(); } finally { try { if (rs != null) { rs.close(); } if (st != null) { st.close(); } if (con != null) { con.close(); } } catch (SQLException e) { e.printStackTrace(); } } %>

最后测试成功,两个jsp可以取到数据库的数据,然后在微信开发工具中测试,发现取到的是整个页面有head有body,并不是只有返回的json,所以微信小程序上显示失败。

结果

思路要重新整理,学姐说逻辑有很大问题,代码很乱,jsp 只负责通过 el 表达式、jstl 表达式、ognl 表达式 展示数据。
所以,理清思路,使用Servlet返回响应数据给客户端。

你可能感兴趣的:(2018-01-25 解决发布到服务器的问题,发现新的问题)