參考Java Web的編寫實現了登陸界面並登錄成功html
import java.io.IOException; import java.io.PrintWriter; import java.util.HashMap; import java.util.Map; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import net.sf.json.JSONObject; /** * 測試登陸Servlet * * @author Implementist */ public class LoginServlet extends HttpServlet { @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 設置響應內容類型 response.setContentType("text/html;charset=utf-8"); request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); try (PrintWriter out = response.getWriter()) { //得到請求中傳來的用戶名和密碼 String accountNumber = request.getParameter("AccountNumber").trim(); String password = request.getParameter("Password").trim(); //密碼驗證結果 Boolean verifyResult = verifyLogin(accountNumber, password); Map<String, String> params = new HashMap<>(); JSONObject jsonObject = new JSONObject(); if (verifyResult) { params.put("Result", "success"); } else { params.put("Result", "failed"); } jsonObject.put("params", params); out.write(jsonObject.toString()); } } @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } /** * 驗證用戶名密碼是否正確 * * @param userName * @param password */ private Boolean verifyLogin(String userName, String password) { User user = UserDAO.queryUser(userName); //帳戶密碼驗證 return null != user && password.equals(user.getPassword()); } }
public void LoginRequest(final String accountNumber, final String password) { //請求地址 String url = "http://59.110.141.197:8080/MyFirstWebAPP/LoginServlet"; //注① String tag = "Login"; //注② //取得請求隊列 RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext()); //防止重複請求,因此先取消tag標識的請求隊列 requestQueue.cancelAll(tag); //建立StringRequest,定義字符串請求的請求方式爲POST(省略第一個參數會默認爲GET方式) final StringRequest request = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() { @Override public void onResponse(String response) { try { JSONObject jsonObject = (JSONObject) new JSONObject(response).get("params"); //注③ String result = jsonObject.getString("Result"); //注④ if (result.equals("success")) { //注⑤ Toast.makeText(getApplicationContext(),"登陸成功",Toast.LENGTH_LONG).show(); Intent intent = null; intent = new Intent(MainActivity.this,FunctionActivity.class); intent.putExtra("Username", username.getText().toString()); startActivity(intent); } else { Toast.makeText(getApplicationContext(),"帳號或密碼錯誤",Toast.LENGTH_LONG).show(); } } catch (JSONException e) { Toast.makeText(getApplicationContext(),"鏈接失敗",Toast.LENGTH_LONG).show(); Log.e("TAG", e.getMessage(), e); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { //作本身的響應錯誤操做,如Toast提示(「請稍後重試」等) Log.e("TAG", error.getMessage(), error); } }) { @Override protected Map<String, String> getParams() throws AuthFailureError { Map<String, String> params = new HashMap<>(); params.put("AccountNumber", accountNumber); //注⑥ params.put("Password", password); return params; } }; //設置Tag標籤 request.setTag(tag); //將請求添加到隊列中 requestQueue.add(request); }
<servlet> <servlet-name>LoginServlet</servlet-name> <servlet-class>chatask.LoginServlet</servlet-class> </servlet>
陸大嶽、彭霖:編寫簽到功能
胡智韜:編寫簽到頁面
牛浩遠:寫第四篇博客java
陸大嶽40
彭霖20
胡智韜0(未設計界面)
牛浩遠0(未寫博客)git