无论是用哪种方式使Android与web后台进行数据交互,都需要在eclipes中写一个完整的操作流程,在这里,我是写了一个登录。(没有从数据库进行判断,而是直接把用户名和密码定死了),下面就是我的后台web端。你可以根据你的不同需求在jsp界面用你所需要的提交方式来写
login,jsp:
pageEncoding="UTF-8"%>
Insert title here用户名:
密码:
LoginServlet:
package com.lxj.servlet;
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 Login extends HttpServlet{
//@Override
//protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//String uname=req.getParameter("uname");
//String upass=req.getParameter("upass");
//
//String a=null;
//if("admin".equals(uname) && "123".equals(upass)){
//a="succes";
//System.out.println(a);
//}else{
//a="fail";
//System.out.println(a);
//}
//PrintWriter pw=resp.getWriter();
//pw.write(a);
//pw.close();
//}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String uname=req.getParameter("uname");
String upass=req.getParameter("upass");
String a=null;
if("admin".equals(uname) && "123".equals(upass)){
a="succes";
System.out.println(a);
}else{
a="fail";
System.out.println(a);
}
PrintWriter pw=resp.getWriter();
pw.write(a);
pw.close();
}
}
最后,还要记得在web.xml里面配置跳转路径
login
com.lxj.servlet.Login
login
/login.do
第一:GET提交方式:
activity_main.xml:
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.android_getandpost.MainActivity">
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入用户名"
android:id="@+id/uname"
/>
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入密码"
android:id="@+id/upass"
/>
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="登陆(Get)"
android:onClick="LoginGet"
/>
MainActivity:
package com.example