百度云BAE上Java ssh项目的部署教程

时间:2022-02-18 07:18:29

首先一定要详细看百度开发者的java开发文档

开发者文档:http://developer.baidu.com/wiki/index.php?title=docs/cplat/rt/java&oldid=10053

 

.去百度开发者下载一个一键安装的eclipse(推荐)

   地址:http://developer.baidu.com/wiki/index.php?title=docs/cplat/ide/install

   或者自己安装插件(比较麻烦) 

2.打开之前下载好的eclipse,切换到SVN资源库,新建资源库. 

  url填上下面的SVN地址

                                         百度云BAE上Java ssh项目的部署教程

 

3.然后右击-检出为 

                                                       百度云BAE上Java ssh项目的部署教程

4.项目上右键->baidu->Convert to BAE(java) Project方法解决          

             百度云BAE上Java ssh项目的部署教程                           

5.等大概几分钟让它把导包加进去..这样就支持正常项目开发了,接下来就需要配置数据库连接,ssh框架的支持...

6.连接数据库

      数据库连接看这个jsp你就秒懂了,jdbc的几个参数配对就好了

<%@ page language="java" import="java.util.*,java.net.URL,java.sql.*,com.baidu.bae.api.util.BaeEnv" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Hello World</title>
</head>
<body>
<%
//(1)指定服务地址,其中dbname需要自己修改
//String dbUrl = "jdbc:mysql://sqld.duapp.com:4050/dbname";
//(2)直接从请求header中获取ip、端口、用户名和密码信息
//String host = request.getHeader("BAE_ENV_ADDR_SQL_IP");
//String port = request.getHeader("BAE_ENV_ADDR_SQL_PORT");
//String username = request.getHeader("BAE_ENV_AK");
//String password = request.getHeader("BAE_ENV_SK");
//(3)从线程变量BaeEnv接口获取ip、端口、用户名和密码信息
String host = BaeEnv.getBaeHeader(BaeEnv.BAE_ENV_ADDR_SQL_IP);
String port = BaeEnv.getBaeHeader(BaeEnv.BAE_ENV_ADDR_SQL_PORT);
String username = BaeEnv.getBaeHeader(BaeEnv.BAE_ENV_AK);
String password = BaeEnv.getBaeHeader(BaeEnv.BAE_ENV_SK);
String driverName = "com.mysql.jdbc.Driver";
String dbUrl = "jdbc:mysql://";
String serverName = host + ":" + port + "/";

//从平台查询应用要使用的数据库名
String databaseName = "DXvrkcWFOUvXbxGzzCMG";
String connName = dbUrl + serverName + databaseName;
String sql = "select * from guestbook";

Connection connection = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName(driverName);
//具体的数据库操作逻辑
connection = DriverManager.getConnection(connName, username,
password);
stmt = connection.createStatement();
rs = stmt.executeQuery(sql);
String id = "", name = "";
out.println("id    name<br/>");
while (rs.next()) {
id = rs.getString("gstId");
name = rs.getString("email");
out.println(id + "     " + name + "<br/>");
}
} catch (ClassNotFoundException ex) {
// 异常处理逻辑
throw ex;
} catch (SQLException e) {
// 异常处理逻辑
throw e;
} finally {
try {
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
throw e;
}
}
%>


haha<%=connName%><br>
<%=username%><br>
<%=password%>
</body>
</html>


7.添加strut2:

  7.1 确保导包都加进去,并且没有和bae的包有冲突

  7.2 在xml里面配置strut2(和本机添加strut2一样)

  7.2 添加ognl(struts2依赖ognl,添加一个listener)

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

import ognl.OgnlRuntime;

public class InitListener implements ServletContextListener,
HttpSessionListener, HttpSessionAttributeListener {

public InitListener() {
}

public void contextInitialized(ServletContextEvent sce) {
OgnlRuntime.setSecurityManager(null);
}

public void contextDestroyed(ServletContextEvent arg0) {
// TODO Auto-generated method stub
}

public void sessionCreated(HttpSessionEvent arg0) {
// TODO Auto-generated method stub
}

public void sessionDestroyed(HttpSessionEvent arg0) {
// TODO Auto-generated method stub
}

public void attributeAdded(HttpSessionBindingEvent arg0) {
// TODO Auto-generated method stub
}

public void attributeRemoved(HttpSessionBindingEvent arg0) {
// TODO Auto-generated method stub
}

public void attributeReplaced(HttpSessionBindingEvent arg0) {
// TODO Auto-generated method stub
}

}

    然后在web.xml里面加入listener

<listener>
<listener-class>InitListener</listener-class>
</listener>


  

8.添加hibernate

      jdbc.driverClassName=com.mysql.jdbc.Driver
      jdbc.url=jdbc:mysql://sqld.duapp.com:4050/
dbname
      jdbc.username=
ak
      jdbc.password=sk
 

  dbname是在应用管理的“云环境管理 > 服务管理 -> MySQL(数据库)”中创建的数据库名称,

  ak用户名为应用的API Key,

  sk密码为Secret Key。

 

9.添加spring


http://blog.csdn.net/df1012890048/article/details/14424401

 

配置成功。虽然有点慢,但是以后所有ssh2的项目都可以放在BAE上了,好开心~~~