服务器的搭建,我们是用的是MyEclipse,(记住一句话,eclipse 是用来做客户端的,MyEclipse 是用来搭建服务器端的,记忆方法,谐音记忆,My读音买, 买服务器,就是MyEclipse);
注意 MyEclipse 里面已经有Tomcat 了,所以我们不用自己去下载安装Tomcat了。
步骤如下:
0:下载MyEclipse,可以通过baidu 软件中心下载: 安装好了以后,运行
1:新建一个 web project,记住了一定必须要是Web Project
输入Project的名字,点击Finish
2:选中刚才新建的工程,右键new建立一个Servlet。
输入Servlet的名字,Package 可以填写也可以不填写,一些想要创建的方法最好保持默认的就好了,不要修改。然后点击Next
然后要注意是Servlet/JSP Mapping URL: 这里面的东西,可以自定义,但是最好保持它原有的,记住这里的地址,
到时候访问的时候要用到的。访问的地址就是http://localhost:8080/工程名/这个Mapping URL
例如我们这里就是http://localhost:8080/AndroidServerDemo/servlet/HelloAndroid
3:在页面中下部的窗口中,找到Servers,然后点击该Servers,可以看到有几个Tomcat的Server的列表,启动顺便一个,这里我启动了tomcat 7
一定要显示Server startup 才说明已经启动了Tomcat了
4:Deploy MyEclipse J2EE Project to server....
5:在Project 里面选择刚才的工程,然后点击add
6:server 下拉菜单选择刚才启动的Tomcat,例如我的就是MyEclipse Tomcat 7,然后点击
7:这个时候会显示 Successful Deployed,以后每次更新后可以点击Redeploy
这个时候整个工程就都放在了tomcat 的webapps下面了
8:访问的时候和包名没有关系 但是和工程名有关。这个也就是第2步里面说的地址。
http://localhost:8080/AndroidServerDemo/servlet/HelloAndroid
这种访问是GET的方法,
9:
在Android 中可以通过HttpURLConnection来进行POST和GET的请求。
请求查看Android 中HttpURLConnection 的使用
10: 在doPost的方法里面可以添加相应的接收客户端发来的信息,然后进行判断,返回相应的信息给客户端.
例如 这里可以判断客户端发来的用户名和密码是否正确的
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginServlert extends HttpServlet {
/**
* Constructor of the object.
*/
public LoginServlert() {
super();
}
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the GET method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
request.setCharacterEncoding("utf-8");//请求方式是 post
//先得到客户端提交的用户名和密码
//因为客户端信息被封装在 request对象中,所以需要从request对象中获取用户名密码
String uname = request.getParameter("username");//?username= &psw=
//String uname = fun(name);
String psw = request.getParameter("password");
//验证
if(uname!=null&&uname.trim()!=""&&psw!=null&&psw.trim()!="")
{
if("哈哈".equals(uname) && "123".equals(psw))
{
out.println("登陆成功");
}
else
out.println("登陆失败");
}else{
out.println("参数有问题");
}
out.flush();
out.close();
/**
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print(this.getClass());
out.println(", using the POST method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
*/
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}
+++++++++++++++++++++++++++++++++++++++++++++++++++++
仅步骤:
0:下载MyEclipse,可以通过baidu 软件中心下载: 安装好了以后,运行
1:新建一个 web project,记住了一定必须要是Web Project
输入Project的名字,点击Finish
2:选中刚才新建的工程,右键new建立一个Servlet。
输入Servlet的名字,Package 可以填写也可以不填写,一些想要创建的方法最好保持默认的就好了,不要修改。然后点击Next
然后要注意是Servlet/JSP Mapping URL: 这里面的东西,可以自定义,但是最好保持它原有的,记住这里的地址,
到时候访问的时候要用到的。访问的地址就是http://localhost:8080/工程名/这个Mapping URL
例如我们这里就是http://localhost:8080/AndroidServerDemo/servlet/HelloAndroid
3:在页面中下部的窗口中,找到Servers,然后点击该Servers,可以看到有几个Tomcat的Server的列表,启动顺便一个,这里我启动了tomcat 7
一定要显示Server startup 才说明已经启动了Tomcat了
4:Deploy MyEclipse J2EE Project to server....
5:在Project 里面选择刚才的工程,然后点击add
6:server 下拉菜单选择刚才启动的Tomcat,例如我的就是MyEclipse Tomcat 7,然后点击
7:这个时候会显示 Successful Deployed,以后每次更新后可以点击Redeploy
这个时候整个工程就都放在了tomcat 的webapps下面了
8:访问的时候和包名没有关系 但是和工程名有关。这个也就是第2步里面说的地址。
http://localhost:8080/AndroidServerDemo/servlet/HelloAndroid
这种访问是GET的方法,
9:在Android 中可以通过HttpURLConnection来进行POST和GET的请求。