parent
0ec5a4bdc8
commit
f24cd9e3b7
@ -0,0 +1,56 @@
|
|||||||
|
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;
|
||||||
|
|
||||||
|
|
||||||
|
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 account = request.getParameter("account").trim();
|
||||||
|
String password = request.getParameter("password").trim();
|
||||||
|
|
||||||
|
//密码验证结果
|
||||||
|
Boolean verifyResult = verifyLogin(account, 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);
|
||||||
|
}
|
||||||
|
//验证用户名密码是否正确
|
||||||
|
private Boolean verifyLogin(String userName, String password) {
|
||||||
|
User user = UserDAO.queryUser(userName);
|
||||||
|
//账户密码验证
|
||||||
|
return null != user && password.equals(user.getPassword());
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,51 @@
|
|||||||
|
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;
|
||||||
|
|
||||||
|
public class RegisterServlet extends HttpServlet {
|
||||||
|
@Override
|
||||||
|
protected void doGet(HttpServletRequest request, HttpServletResponse response)
|
||||||
|
throws ServletException, IOException {
|
||||||
|
doPost(request, response);
|
||||||
|
}
|
||||||
|
@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 account = request.getParameter("account").trim();
|
||||||
|
String password = request.getParameter("password").trim();
|
||||||
|
|
||||||
|
Map<String, String> params = new HashMap<>();
|
||||||
|
JSONObject jsonObject = new JSONObject();
|
||||||
|
if(isExist(account)){
|
||||||
|
params.put("Result", "账号已存在");
|
||||||
|
}else{
|
||||||
|
UserDAO.Register(account, password);
|
||||||
|
params.put("Result", "注册成功");
|
||||||
|
}
|
||||||
|
jsonObject.put("结果", params);
|
||||||
|
out.write(jsonObject.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//判断用户是否存在
|
||||||
|
private Boolean isExist(String account) {
|
||||||
|
User user = UserDAO.queryUser(account);
|
||||||
|
if(user.getAccount().isEmpty()){
|
||||||
|
return false;
|
||||||
|
}else{
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,79 @@
|
|||||||
|
private int socket_port=9999;
|
||||||
|
private boolean ifListen =true;
|
||||||
|
/**
|
||||||
|
* 开始监听
|
||||||
|
*/
|
||||||
|
private Thread socketThread = new Thread() {
|
||||||
|
public void run() {
|
||||||
|
while (true) {
|
||||||
|
try {
|
||||||
|
if (serverSocket == null && ifListen) {
|
||||||
|
serverSocket = new ServerSocket(socket_port);
|
||||||
|
// serverSocket.setSoTimeout(60*1000);
|
||||||
|
} else if (serverSocket != null) {
|
||||||
|
socket = serverSocket.accept();
|
||||||
|
if (socket != null) {
|
||||||
|
DataInputStream in = new DataInputStream(new BufferedInputStream(socket
|
||||||
|
.getInputStream()));
|
||||||
|
try {
|
||||||
|
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
|
||||||
|
byte[] buffer = new byte[1024];
|
||||||
|
int len = 0;
|
||||||
|
while ((len = in.read(buffer)) != -1) {
|
||||||
|
outStream.write(buffer, 0, len);
|
||||||
|
}
|
||||||
|
byte[] data = outStream.toByteArray();
|
||||||
|
dataString = new String(data, "utf-8");
|
||||||
|
AppLog.Log(dataString);
|
||||||
|
} catch (Exception e) {
|
||||||
|
AppLog.Log(AppLog.LogType.ERROR, "DataService read: " + e);
|
||||||
|
destorySocket();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (IOException e1) {
|
||||||
|
AppLog.Log(AppLog.LogType.ERROR, "DataService accept: " + e1);
|
||||||
|
destorySocket(); }
|
||||||
|
try {
|
||||||
|
Thread.sleep(Config.KEEP_ALIVE_RESPONSE_TIMEOUT);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
AppLog.Log(e.toString());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
public void startListen() {
|
||||||
|
ifListen = true;
|
||||||
|
if (!ifSocketThreadStart) {
|
||||||
|
ifSocketThreadStart = true;
|
||||||
|
socketThread.start();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void stopListen() {
|
||||||
|
ifListen = false;
|
||||||
|
destorySocket();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void destorySocket() {
|
||||||
|
AppLog.Log("destorySocket");
|
||||||
|
try {
|
||||||
|
if (serverSocket != null && !serverSocket.isClosed()) {
|
||||||
|
serverSocket.close();
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
AppLog.Log(e.toString());
|
||||||
|
} finally {
|
||||||
|
serverSocket = null;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
if (socket != null && !socket.isClosed()) {
|
||||||
|
socket.close();
|
||||||
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
AppLog.Log(e.toString());
|
||||||
|
} finally {
|
||||||
|
socket = null;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in new issue