# Experiment4 **Repository Path**: calivin9287/Experiment4 ## Basic Information - **Project Name**: Experiment4 - **Description**: No description available - **Primary Language**: Java - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2019-05-15 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 《JAVA EE企业级架构》课程实验报告 院(系)名称:网络空间安全学院 专业班级: 17软卓2班 学号: 201741412208 姓名: 吴永满 实验题目:实验4 JDBC 实验日期:2019.4.27 实验(上机)学时: 2 成绩: ## 一、实验内容、要求 *改写用户注册/登录模块,使用JDBC或JPA技术实现用户数据的持久化,大致功能如下: >1、 设计用户实体Entity与莞工登录用户Entity,并设置关联。 2、 Entity需要校验用户数据的合法性。 3、 用户照片保存在数据库中;前端显示用户照片时,改为读取数据库。 4、 任何数据库操作发生错误时,请导向error.jsp,并回滚数据库事务。 5、 增加绑定莞工中央认证账号的功能。本地账号登录的用户,可以在用户中心绑定莞工认证账号。 绑定后,本地账号与莞工中央认证账号关联(一对一),并且使用莞工中央认证登录等价于本地账号登录。 > ## 二、所采用的Java EE技术规范 1. JSP的基础语法 2. Filter配置 3. Serlet配置 4. HttpClient组件 5. JPA的配置以及应用 6. web.xml的配置 7. jsonbean 8. html,css,js ## 三、实验的主要模块及其功能 ### loginservlet.java 获取前端网页上传请求中的信息,读取TXT文件中的用户信息,然后一一检验账号,密码是否符合 若正确则存入会话中 > HttpSession session = request.getSession(true); Map map = new HashMap(); map.put("用户名", userinformation.getUsername()); map.put("密码", userinformation.getPassword()); map.put("学号", userinformation.getStu_number()); map.put("手机号码", userinformation.getPhone_number()); map.put("电子邮箱", userinformation.getEmail()); map.put("头像", userinformation.getImg()); //HttpSession session = request.getSession(true); Map map2 = new HashMap(); DataDTO3 dataDTO3 = userinformation.getDataDTO3(); if (dataDTO3 != null) { map2.put("学号", dataDTO3.getUsername()); map2.put("姓名", dataDTO3.getName()); map2.put("学院", dataDTO3.getFaculty_title()); } else { map2.put("提示", "暂时无绑定信息,请前往绑定"); } session.setAttribute("userName", map); session.setAttribute("userBone", map2); session.setAttribute("token", "not null"); response.sendRedirect("/show.jsp"); > 错误则提示用户账号或者密码错误 ### registerservlet.java 获取前端网页上传请求中的注册信息,使用jsonbean完成JSON的转化,接着判断该用户是否已存在,再转成字符串的形式存入TXT文件中 ```` 将莞工登录的账号和本地账号关联起来 ```` EntityManager em = getEntityManager(); EntityTransaction tx = em.getTransaction(); try { String username = request.getParameter("username"); String password = request.getParameter("password"); String phone_number = request.getParameter("phone_number"); String email = request.getParameter("email"); Userinformation userinformation = new Userinformation(username, password, "",phone_number, email,"",null); tx.begin(); em.persist(userinformation); tx.commit(); }catch (Exception ex) { throw new ServletException(); } finally { //close the em to release any resources held up by the persistebce provider em.close(); } response.setContentType("text/html"); response.setCharacterEncoding("UTF-8"); response.getWriter().println("注册成功"); response.sendRedirect("index.html"); ### joinmessage.java ```` static EntityManager em = Persistence.createEntityManagerFactory("MyJPADemoPU").createEntityManager(); //绑定 public static void join_message(DataDTO3 dataDTO3, HttpServletRequest request, HttpServletResponse response) throws IOException { String username=dataDTO3.getUsername(); EntityTransaction tx = em.getTransaction(); HttpSession session = request.getSession(true); Map map = new HashMap(); Map map2 = new HashMap(); Map userName = (Map) request.getSession().getAttribute("userName"); Userinformation userinformation=em.find(Userinformation.class,userName.get("用户名")); //未绑定但有用户 if(userinformation!=null) { if (userinformation.getUsername() != username) { tx.begin(); userinformation.setStu_number(username); userinformation.setDataDTO3(dataDTO3); em.persist(dataDTO3); tx.commit(); map.put("用户名", userinformation.getUsername()); map.put("密码", userinformation.getPassword()); map.put("学号", userinformation.getStu_number()); map.put("手机号码", userinformation.getPhone_number()); map.put("电子邮箱", userinformation.getEmail()); map.put("头像", userinformation.getImg()); if (userinformation.getDataDTO3() != null) { map2.put("学号", dataDTO3.getUsername()); map2.put("姓名", dataDTO3.getName()); map2.put("学院", dataDTO3.getFaculty_title()); } } } //绑定顺便创建用户 if(userinformation==null) { Userinformation u=new Userinformation(username,"123456",username,"","","",dataDTO3); tx.begin(); em.persist(u); em.persist(dataDTO3); tx.commit(); map.put("用户名", u.getUsername()); map.put("密码", u.getPassword()); map.put("学号", u.getStu_number()); map.put("手机号码", u.getPhone_number()); map.put("电子邮箱", u.getEmail()); map.put("头像", u.getImg()); if (u.getDataDTO3() != null) { map2.put("学号", dataDTO3.getUsername()); map2.put("姓名", dataDTO3.getName()); map2.put("学院", dataDTO3.getFaculty_title()); } } //HttpSession session = request.getSession(true); session.setAttribute("userName", map); session.setAttribute("userBone", map2); session.setAttribute("token", "not null"); response.sendRedirect("/show.jsp"); } } ```` ### ALLFilter.java 该过滤器通过request.getSession()得到一个HttpSession的对象,用于判断用户是否处于登录状态,如果是就重定向到的index.jsp,如果不是就直接登出。 ### DataDTO1.java 定义发送POST请求的对象对应的java类 ### DataDTO2.java 定义接收第一个POST请求返回的JSON数据对应的java类 ### DataDTO3.java 定义接收第一个POST请求返回的学生信息JSON数据对应的java类 ### dgut.java 接受令牌,生成一个dataDTO1类的对象,并通过QuickStart.postJson发送到https://cas.dgut.edu.cn/ssoapi/v2/checkToken ```的java String token = request.getParameter(“token”); String userip = request.getLocalAddr(); DataDTO1 dataDTO1 = new DataDTO1(token,“javaee”,“b3b52e43ccfd”,userip); ``` 将返回的JSON字符串保存在S1中 通过Jsonb类方法.fromJson将S1转化为DataDTO2类的实体对象 ```的java Jsonb jsonb = JsonbBuilder.create(); DataDTO2 dataDTO2 = jsonb.fromJson(s1,DataDTO2.class); ``` 接下来判断是否成功返回,将“access_token”的属性值和“openid”的属性值添加到其中,并通过QuickStart.post的方法发送到https://cas.dgut.edu .CN /的OAuth / getUserInfo中,同样的,将返回的JSON字符串转化成DataDTO3的实体对象 最终将对象dataDTO3的所有属性值保存到会话中 ```的java if(dataDTO2.getMessage()!= null && dataDTO2.getMessage()。equals(“success”)){ List list = new ArrayList <>(); list.add(new BasicNameValuePair(“access_token”,dataDTO2.getAccess_token())); list.add(new BasicNameValuePair(“openid”,dataDTO2.getOpenid())); String s2 = QuickStart.post(“https://cas.dgut.edu.cn/oauth/getUserInfo”,list); if(s2!= null && s2 ==“{\ n”+ “\”错误\“:1,\ n”+ “\”message \“:\”access-token不存在或已过期\“\ n”+ “}”) { 的System.out.println( “访问令牌不存在或已过期”); } 其他{ DataDTO3 dataDTO3 = jsonb.fromJson(s2,DataDTO3.class); HttpSession httpSession = request.getSession(); httpSession.setAttribute(“username”,dataDTO3.getUsername()); httpSession.setAttribute(“name”,dataDTO3.getName()); httpSession.setAttribute(“group”,dataDTO3.getGroup()); httpSession.setAttribute(“openid”,dataDTO3.getOpenid()); httpSession.setAttribute(“wx_openid”,dataDTO3.getWx_openid()); response.sendRedirect是( “HTTP://本地主机:8080 / index.jsp的”); } } ``` ##四,程序运行时的输入数据/输出结果 ###登陆界面 ![avatar](src/image/2.png) ### 登陆失败 ![avatar](src/image/3.png) ### 上传头像 ![avatar](src/image/4.png) ### 成功上传头像 ![avatar](src/image/5.png) ### 成功绑定 ![avatar](src/image/7.jpg) ![avatar](src/image/8.jpg) ### 错误界面 ![avatar](src/image/6.png)