//java端
package cotroller;
import java.util.HashMap;
import java.util.Map;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import jdk.nashorn.api.scripting.JSObject;
import model.Student;
import model.Teacher;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import dao.Get;
import dao.StudentDAO;
//登陆servlet
@Controller
public class LoginCotroller {
/**
* 1. value="/doLogin/{username}/{password}" 拦截 xxx/doLogin/xx/xx
* 2. @ResponseBody 使用此注解将返回数据类型封装json
* 3. @PathVariable("username") 截取请求1.value中{username}的值
* 4. Map<String, Object> 服务端将值放入map中再封装为json,客户端方便通过key取出value
*/
StudentDAO studentDAO = new StudentDAO();//调用登陆判断方法
@RequestMapping(value="/doLogin/{username}/{password}",method=RequestMethod.GET)
@ResponseBody
public Map<String, Object> getTeacher(@PathVariable("username") Integer username, @PathVariable("password") String password){
System.out.println("拦截了客户端json请求");
Map<String, Object> map = new HashMap<String, Object>();
if(studentDAO.loginByStudent(username, password)){
System.out.println("密码正确");
map.put("result", "1");
return map; //封装为json返回给客户端
}
System.out.println("密码错误");
map.put("result", "0");
return map; //封装为json返回给客户端
}
}
//ios端
#import <Foundation/Foundation.h>
#import <stdio.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
char oldUsername[128];
char oldPassword[128];
NSLog(@"请输入用户名 :");
scanf("%s", oldUsername);
NSString *username = [NSString stringWithUTF8String:oldUsername]; //转换为NSString *
NSLog(@"请输入密码 :");
scanf("%s", oldPassword);
NSString *password = [NSString stringWithUTF8String:oldPassword]; //转换为NSString *
//访问springMVC后台并解析返回的json数据
//定义一个异常
NSError *error;
//定义请求action 使用stringWithFormat拼接字符串
NSString *url = [NSString stringWithFormat:@"http://154212l6t7.imwork.net:27063/partyOS_APP/doLogin/%@/%@", username, password];
//加载一个NSURL对象
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
//发送请求 将请求的url数据放到NSData对象中
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
//NSJSONSerialization从response请求中解析出数据放到字典中
NSDictionary *jsonResult = [NSJSONSerialization JSONObjectWithData:response options:NSJSONReadingMutableLeaves error:&error];
NSString *resultValue = [jsonResult objectForKey:@"result"];
NSLog(@"你的url是%@", url);
NSLog(@"服务端返回值%@", resultValue);
// oc字符串比较方法 resultValue isEqualToString:@"1"] 和java 的equlse类似
if([resultValue isEqualToString:@"1"]){
NSLog(@"登录成功!");
}else{
NSLog(@"登录失败,用户名或密码错误!");
}
}
return 0;
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有