淘忆项目之忘记密码界面的修正归纳
忘记密码,这一部分目前简单处理一下,因为没有短信接口,所以就先用用户名作为唯一标识来处理,就是提交用户名和新密码,然后传到服务器端,服务器端通过更新密码来返回数据。
第一步:用photoshop来设计一下界面。
服务器端:
Com.elaine.forgetPwd.service写下如下代码:
package com.elaine.forgetPwd.service;
import java.util.List;
public interface ForgetPwdService {
public boolean isChangePwd(List
}
Com.elaine.forgetPwd.dao写下如下代码:
package com.elaine.forgetPwd.dao;
import java.sql.SQLException;
import java.util.List;
import com.elaine.forgetPwd.service.ForgetPwdService;
import com.elaine.jdbc.JdbcUtils;
public class ForgetPwdDao implements ForgetPwdService{
JdbcUtils jdbcUtils;
public ForgetPwdDao() {
// TODO Auto-generated constructor stub
jdbcUtils=new JdbcUtils();
}
@Override
public boolean isChangePwd(List
// TODO Auto-generated method stub
boolean flag=false;
try {
String sql="update userinfo set password=? where username=?"; //这是用于更新密码的语句
jdbcUtils.getConnection();
flag = jdbcUtils.updateByPreparedStatement(sql, params);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return flag;
}
}
Com.elaine.forgetPwd.action写下如下代码:
package com.elaine.forgetPwd.action;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.elaine.forgetPwd.dao.ForgetPwdDao;
import com.elaine.forgetPwd.service.ForgetPwdService;
import com.elaine.tools.jsonTool;
public class ForgetPwdAction extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
private ForgetPwdService service;
/**
* Constructor of the object.
*/
public ForgetPwdAction() {
super();
}
/**
* Destruction of the servlet.
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet.
*
* 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 {
this.doPost(request, response);
}
/**
* The doPost method of the servlet.
*
* 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 {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
String username=request.getParameter("username");
String password=request.getParameter("password");
List
params.add(password);
params.add(username);
boolean flag=service.isChangePwd(params);
if(flag){
String addThingMsg="密码修改成功";
String addThingMsgJson=jsonTool.creataJsonString("msg", addThingMsg);
out.print(addThingMsgJson);
}else{
String addThingMsg="用户名错误";
String addThingMsgJson=jsonTool.creataJsonString("msg", addThingMsg);
out.print(addThingMsgJson);
}
out.flush();
out.close();
}
/**
* Initialization of the servlet.
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
service=new ForgetPwdDao();
}
}
客户端传入两个数据,username和newPassword,然后解析返回的数据,显示。
首先是建立一个空的Activity,名为ForgetActivity.
第一步:将地址链接加入到UrlUtils,java中,
public static String getForgetPwdUrl() {
return URL + "/servlet/ForgetPwdAction";
}
第二步:将布局文件写好。
xml version="1.0" encoding="utf-8"?>
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f8f8f8"
android:orientation="vertical"
tools:context="com.elainetaylor.blog.ui.activity.ForgetActivity">
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff"
android:orientation="horizontal">
android:id="@+id/ib_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@android:color/transparent"
android:src="@mipmap/icon_back" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginEnd="50dp"
android:layout_marginRight="50dp"
android:gravity="center"
android:text="忘记密码"
android:textColor="#707070"
android:textSize="18sp" />
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#dadada" />
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="45dp"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="用户名"
android:textColor="#707070"
android:textSize="16sp" />
android:id="@+id/et_username"
android:layout_width="245dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginStart="5dp"
android:background="@android:color/transparent"
android:inputType="text"
android:textColor="#5000"
android:textSize="14sp" />
android:layout_width="300dp"
android:layout_height="1dp"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:background="#dadada" />
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="新密码"
android:textColor="#707070"
android:textSize="16sp" />
android:id="@+id/et_NewPassword"
android:layout_width="245dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginStart="5dp"
android:background="@android:color/transparent"
android:inputType="textPassword"
android:textColor="#5000"
android:textSize="14sp" />
android:layout_width="300dp"
android:layout_height="1dp"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:background="#dadada" />
第三步:将功能实现,这部分还是很简单的。
package com.elainetaylor.blog.ui.activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.elainetaylor.blog.R;
import com.elainetaylor.blog.common.BaseActivity;
import com.elainetaylor.blog.common.UrlUtils;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
//这里也是继承自BaseActivity,使用公用的方法比较简单
public class ForgetActivity extends BaseActivity implements View.OnClickListener {
private ImageButton ibBack;
private EditText etUsername, etNewPassword;
private Button btnRebuild;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_forget);
init();
initRequestQueue();
}
public void init() {
ibBack = (ImageButton) findViewById(R.id.ib_back);
etUsername = (EditText) findViewById(R.id.et_username);
etNewPassword = (EditText) findViewById(R.id.et_NewPassword);
btnRebuild = (Button) findViewById(R.id.btn_rebuild);
ibBack.setOnClickListener(this);
btnRebuild.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.ib_back:
finish();
break;
case R.id.btn_rebuild:
String username = etUsername.getText().toString();
String password = etNewPassword.getText().toString();
if (TextUtils.isEmpty(username)) {
Toast.makeText(this,"亲,请输入用户名哦",Toast.LENGTH_SHORT).show();
} else if (TextUtils.isEmpty(password)||password.length()<6) {
Toast.makeText(this,"亲,请输入六位以上的密码哦",Toast.LENGTH_SHORT).show();
} else {
showProgressDialog("修改密码中...");
reBuildPwd(username, password);
}
break;
}
}
public void reBuildPwd(final String username, final String password) {
StringRequest request = new StringRequest(Request.Method.POST, UrlUtils.getForgetPwdUrl(), new Response.Listener
@Override
public void onResponse(String s) {
try {
JSONObject object = new JSONObject(s);
String msg = object.getString("msg");
if (msg.equals("密码修改成功")) {
missProgressDialog();
finish();
}
missProgressDialog();
Toast.makeText(ForgetActivity.this, msg, Toast.LENGTH_SHORT).show();
} catch (JSONException e) {
e.printStackTrace();
missProgressDialog();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
missProgressDialog();
Toast.makeText(ForgetActivity.this, "服务器出错", Toast.LENGTH_SHORT).show();
}
}) {
@Override
protected Map
Map
params.put("username", username);
params.put("password", password);
return params;
}
};
addRequestQueue(request);
}
}