I have seen two ways of server development under mac:
1. The method of php;
Mac comes with apache and php, how to open it?
1. How to use apache:
start up:
sudo apachectl start
Get the version number:
sudo apachectl -v
closure:
sudo apachectl stop
restart:
sudo apachectl restart
Type localhost in your browser and you'll see It Works!
2. How to use php:
Enter the command line,
cd /etc/apache2/
open .
Then you can see the httpd.conf file, you can use your favorite editing tool, vi, vim or sublime text, find #LoadModule php5_module libexec/apache2/libphp5.so
Remove the # sign in front.
On the command line:
sudo cp /etc/php.ini.default /etc/php.ini
cp, this command copies the specified source file to the target file or copies multiple source files to the target directory.
That is to say, copy php.ini.default to php.ini. At this time, you can modify php.ini to modify the configuration function.
;Adjust the maximum value of PHP submitted files through the following two items, such as the maximum value of imported data in phpMyAdmin
upload_max_filesize = 2M
post_max_size = 8M;
Use display_errors to control whether to display the error message of the PHP program, which is very useful when debugging the PHP program
display_errors = Off
sudo apachectl restart
Restart apache.
sudo cp /Library/WebServer/Documents/index.html.en /Library/WebServer/Documents/info.php
Copy the index.html.en file in the root directory of Apache and rename it to info.php.
If you modify this info.php file, add <?php phpinfo(); ?>
At this time, use http://localhost/info.php to see It Works! Add php information later.
3. How to use mysql
Download mysql , I downloaded mysql-5.6.15-osx10.7-x86_64.dmg, install it, and install mysql-5.5.27-osx10.6-x86_64.pkg in sequence - MySQL, the main installation package , in general, the installation file It will automatically install MySQL to /usr/local
the folder with the same name as below. If you run " mysql-5.5.27-osx10.6-x86_64.dmg
", it will install MySQL to " /usr/local/mysql-5.6.15-osx10.7-x86_64
".
Install the second file, MySQLStartupItem.pkg , and MySQL will automatically start at boot time.
Install the third file MySQL.prefPane , you will see the ICON of "MySQL" in the "System Preferences". You can control the switch of mysql.
4. How to use Apache
Apache's user-level root directory is: ~Sites, if not, create one, you can use the command line to create: sudo mkdir Sites
There are two ways to configure:
(1)/etc/apache2/users/下面看是否有“你的用户名.conf”这个文件没有的话就创建一个, ;
(2)/etc/apache2/httpd.conf修改这个文件中,
找到Directory,做如下修改,下面的是修改后的结果:
<Directory />
Options Indexes FollowSymLinks
AllowOverride All
Order deny,allow
Allow from all
</Directory>
然后重启apache,就可以通过http://localhost/~username/可以访问用户级目录网页了。这个~username意思就是,如果你的开机用户名是liuyun,那么你就应该写成~liuyun。
现在 PHP 应该已经开始工作了,你可以在用户级根目录下(~/Sites/)放一个PHP测试文件,代码如下:
<?php phpinfo(); ?>浏览器中打开这个文件即可以看见php的信息了。
5.phpMyAdmin的使用方式
phpMyAdmin是用PHP开发的管理MySQL的程序,非常的流行和实用。下载以后,改名为“phpmyadmin”,然后可以放在用户级根目录(~/Sites)下也可以放在“/Library/WebServer/Documents/
”下.
(1)用户级(~/Sites)
调用http://localhost/~username/就可以通过浏览器访问你的用户级目录网页。
(2)系统级(/Library/WebServer/Documents/)
复制“/Library/WebServer/Documents/phpmyadmin/
”中的config.sample.inc.php,并命名为config.inc.php.
调用http://localhost/phpmyadmin/就可以进入phpmyadmin的管理页面了。
二、使用java的方式;
在这里是使用eclipse+tomcat+mysql的方式。
1、软件下载
(1) eclipse
我使用的是mac,因为本人是苹果客户端开发者,所以目前只有苹果机器了。首先下载eclipse for mac,选择mac 64位,下载eclipse-jee-kepler-SR1-macosx-cocoa-x86_64.tar.gz, 解压后可以看到eclipse图标,双击,这些都不用说了。
(2)tomcat
mac自带java,可以打开mac命令行,输入java -version, 我的是java version "1.6.0_65",所以我选择使用tomcat 7下载,如果java是1.7以后,可以选择tomcat 8.
apache-tomcat-7.0.47.tar解压后进入命令行
cd 你的tomcat路径/apache-tomcat-7.0.47/bin
./startup.sh 或者 sh startup.sh
关闭则使用shutdown.sh。
打开浏览器输入http://localhost:8080,这个时候就可以看到tomcat的页面了。webapps文件夹就是它的用户文件夹。
(3)mysql
上面已经写清楚,不用再说了。
2、使用方式
(1)打开eclipse;
(2)点击菜单Eclipse->Preferences...->Server->Runtime Environments
(3)点击Add。。。
创建生成tomcat环境。
(4)创建项目
菜单选择File-》New。。。-》Project-》Web-》Dynamic Web Project
输入Project Name,选择Target Runtime,然后可以next也可以直接finish。然后就可以看到项目已经生成。我在next之后修改了content directory为WebRoot,所以项目显示为如下:
在WebRoot下new一个index.jsp, 修改如下:
<%@ page language="java" contentType="text/html; charset=GB18030"
pageEncoding="GB18030"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!-- <html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GB18030">
<title>First Web Project</title>
</head>
<body>
<center>Hello World!</center>
</body>
</html> -->
<form name="form1" action="index.jsp" method="post">
<br><br>
<input type="text" name="Vals">
<input type="text" name="Amount">
<input type="submit" name="Submit" value="Submit">
</form>
<%
int intLocal_Vals;
int intLocal_Amount;
if(request.getParameter("Vals") != null && request.getParameter("Amount") != null) {
intLocal_Vals = Integer.parseInt(request.getParameter("Vals"));
intLocal_Amount = Integer.parseInt(request.getParameter("Amount"));
intLocal_Vals=intLocal_Vals>>intLocal_Amount;
out.print("<br>位移后的值为:" +intLocal_Vals);
}
else {
out.print("位移值或位移量不能为空!");
}
%>
运行,
这时,出现
原来之前运行的tomcat还未关闭,只需要在命令行关闭之前的即可。
运行后默认是在eclipse嵌入方式显示,如果希望能在浏览器中显示,可以选择Window-》Web Browser,可以选择浏览器显示。如果不需要debug的话.
平常我们客户端都会有很多的接口,那么我们怎么实现接口的呢?下面就来详细介绍一下吧。
首先在项目里面创建一个servlet,
名字命名为HelloServlet,可以看到代码里面,
package com.example.servlets;
import java.io.IOException;
import java.sql.DriverManager;
import java.sql.SQLException;
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 HelloServlet
*/
@WebServlet("/HelloServlet")
public class HelloServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public HelloServlet() {
super();
// TODO Auto-generated constructor stub
java.sql.Connection conn = null; //连接
try {
Class.forName("com.mysql.jdbc.Driver");
try {
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/db","root","1234");
if(conn==null){
// out.println("get Conn Error");
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} //建立连接
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} //驱动
}
/**
* @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
response.getWriter().write("Hello, world post!");
}
}
参考资料:
http://dancewithnet.com/2010/05/09/run-apache-php-mysql-in-mac-os-x/#php
http://currentbottle.blog.163.com/blog/static/19429412820136263103728/