本文主要是介绍9.5javaweb项目总结,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
继承BaseServlet
作用:获取请求类型,查找对应方法,然后进行参数的注入,注入完后执行方法,最好响应给前端。
String uri = req.getRequestURI(); // 获取请求的 URISystem.out.println("uri = " + uri);String methodName = new String(uri.substring(uri.lastIndexOf("/") + 1)).toString(); // 从 URI 中提取方法名System.out.println("方法名:" + methodName);Method method = null;Method[] methods = this.getClass().getDeclaredMethods(); // 获取当前类中所有声明的方法String contentType = req.getContentType(); // 获取请求的内容类型System.out.println("请求类型为:" + contentType);
然后进入方法循环,判断POST和方法名,GET也类似如此
// 如果方法的 POST 注解的 value 属性与 methodName 匹配if (postAnnotation.value().equals(methodName)) {System.out.println("存在相关方法!!!执行方法:");invokeMethod(m, req, resp,jsonNode);break;}
然后调用invokeMethod,进入该方法之后,调用resolveParmeter进行参数注入,用args来表示方法参数,最后调用handleMethodResponse方法响应客户端。
Object[] args = new Object[parameters.length]; // 用于存储方法参数的值System.out.println("参数个数:"+parameters.length);for (int i = 0; i < parameters.length; i++) {args[i] = resolveParameter(parameters[i], req, resp,jsonNode);}Object returnValue=null;try {returnValue=method.invoke(this, args);} catch (IllegalAccessException | InvocationTargetException e) {throw new RuntimeException("方法调用失败: " + e.getMessage(), e);}//正在响应客户端!!handleMethodResponse(method,args,req,resp,returnValue);
判断方法的返回值响应客户端
Res resAnnotation = method.getAnnotation(Res.class);if (resAnnotation.value().equals(ReturnValue.JSON)) {System.out.println("正在响应客户端!!" + returnValue);resp.setContentType("application/json");try {if (returnValue != null) {resp.getWriter().write(returnValue.toString());// JSON 对象} else {resp.getWriter().write("{}"); // 返回一个空的 JSON 对象}System.out.println("响应成功!");} catch (IOException e) {throw new RuntimeException(e);}}
通过注解,找到对应的方法,注入参数
发送验证码
public static String sendEmail(String to) throws MessagingException {//发送验证码String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";// QQ邮箱服务器String smtpHost = "smtp.qq.com";// 邮箱用户名,即QQ账号(自定义)String username = "2161672768";// 邮箱授权码(自定义)String password = "mtdjpbvxcwgjebhc";// 自己的邮箱(自定义)String from = "2161672768@qq.com";// 要发送的邮箱地址(自定义)String toAddress = to;//Transport transport;Properties props = new Properties();props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);props.setProperty("mail.smtp.socketFactory.fallback", "false");props.setProperty("mail.smtp.port", "465");props.setProperty("mail.smtp.socketFactory.port", "465");props.setProperty("mail.smtp.auth", "true");props.put("mail.smtp.host", smtpHost);props.put("mail.smtp.username", username);props.put("mail.smtp.password", password);Session session = Session.getDefaultInstance(props, new Authenticator() {protected PasswordAuthentication getPasswordAuthentication() {return new PasswordAuthentication(username, password);}});InternetAddress[] addresses = new InternetAddress[]{new InternetAddress(toAddress)};message = new MimeMessage(session);message.setFrom(new InternetAddress(from));message.setRecipients(MimeMessage.RecipientType.TO, addresses);message.setSubject("验证码");message.setSentDate(new Date());m = generateVerificationCode(5);message.setText(m);System.out.println("生成的验证码为:"+m);Transport transport = session.getTransport("smtp");System.out.println("邮箱账号密码验证");transport.connect(smtpHost, username, password);System.out.println("成功!");transport.sendMessage(message, message.getAllRecipients());// Transport.send(message);System.out.println("信息已经发送");transport.close();return m;}
这篇关于9.5javaweb项目总结的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!