各个成员今日完成的任务(如果完成的任务为开发或测试任务,需给出对应的Github代码签入记录截图;如果完成的任务为调研任务,需给出对应的调研总结博客链接;如果完成的任务为学习技术任务,需给出学习总结博客链接)
陆大岳彭霖:实现登录功能
参考Java Web的编写实现了登录界面并登陆成功
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);
}
各个成员遇到的问题
- 问题1:登录失败
- 解决方案:没有配置LoginServlet的URL,应在web.xml中配置
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>chatask.LoginServlet</servlet-class>
</servlet>
明日各个成员的任务安排
陆大岳、彭霖:编写签到功能
胡智韬:编写签到页面
牛浩远:写第四篇博客
?现阶段团队成员的贡献任务量(h)分别是:
陆大岳40
彭霖20
胡智韬0(未设计界面)
牛浩远0(未写博客)
原文地址:https://www.cnblogs.com/m1sty/p/10143236.html
时间: 2024-10-08 17:03:02