有一个错误 信息提示是这样的 响应状态码是500
浏览器给出的提示:(提交表单,注册之后提示)
HTTP Status 500 - 您调用的方法:regist抛出了异常!
type Exception report
message 您调用的方法:regist抛出了异常!
description The server encountered an internal error that prevented it from fulfilling this request.
exception
java.lang.RuntimeException: 您调用的方法:regist抛出了异常!
cn.itcast.servlet.BaseServlet.service(BaseServlet.java:93)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
root cause
java.lang.reflect.InvocationTargetException
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:606)
cn.itcast.servlet.BaseServlet.service(BaseServlet.java:61)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
root cause
java.lang.RuntimeException: javax.mail.AuthenticationFailedException: failed to connect
cn.itcast.bookstore.user.service.UserService.regist(UserService.java:104)
cn.itcast.bookstore.user.web.servlet.UserServlet.regist(UserServlet.java:123)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:606)
cn.itcast.servlet.BaseServlet.service(BaseServlet.java:61)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
root cause
javax.mail.AuthenticationFailedException: failed to connect
javax.mail.Service.connect(Service.java:322)
javax.mail.Service.connect(Service.java:172)
javax.mail.Service.connect(Service.java:121)
javax.mail.Transport.send0(Transport.java:190)
javax.mail.Transport.send(Transport.java:120)
cn.itcast.mail.MailUtils.send(MailUtils.java:91)
cn.itcast.bookstore.user.service.UserService.regist(UserService.java:102)
cn.itcast.bookstore.user.web.servlet.UserServlet.regist(UserServlet.java:123)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
java.lang.reflect.Method.invoke(Method.java:606)
cn.itcast.servlet.BaseServlet.service(BaseServlet.java:61)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.42 logs.
Apache Tomcat/7.0.42
数据已经保存到了数据库中
下面是regist代码
package cn.itcast.bookstore.user.service;
import java.io.IOException;
import java.io.InputStream;
import java.text.MessageFormat;
import java.util.Properties;
import javax.mail.Session;
import cn.itcast.bookstore.user.dao.UserDao;
import cn.itcast.bookstore.user.domain.User;
import cn.itcast.mail.Mail;
import cn.itcast.mail.MailUtils;
/*
* 用户业务层
*/
public class UserService {
private UserDao userDao=new UserDao();
/**
* 注册功能
* @param form
* @throws UserException
*/
public void regist(User form) throws UserException {
/*
* 1. 业务逻辑
* * 校验用户名
* * 校验邮箱
* * 添加用户
* 2. 发邮件
*/
/*
* 校验用户名
* * 使用用户名进行查询,如果返回的不是null,说明用户名已被注册过,抛出异常
*/
User user = userDao.findByUsername(form.getUsername());
if(user != null) throw new UserException("用户名已被注册!");
/*
* 校验Email
* * 使用Email进行查询,如果返回的不是null,说明Email已被注册过,抛出异常
*/
user = userDao.findByEmail(form.getEmail());
if(user != null) throw new UserException("邮箱已被注册!");
/*
* 走到这里,说明两项校验都通过了!
* 添加用户
*/
userDao.addUser(form);
/*****************************************************/
/*
* 发邮件
* 1. 得到Session
* * 主机名
* * 用户名
* * 密码
* 2. 创建Mail对象
* * 发件人
* * 收件人
* * 标题
* * 内容
* 3. 发送
*/
/*
* 加载配置文件信息
*/
Properties props = new Properties();
InputStream input = this.getClass().getResourceAsStream("/email_config.properties");
try {
props.load(input);
} catch (IOException e) {
throw new RuntimeException(e);
}
// 使用配置文件信息给七个参数赋值
String host = props.getProperty("host");
String username = props.getProperty("username");
String password = props.getProperty("password");
String from = props.getProperty("from");
String to = form.getEmail();
String subject = props.getProperty("subject");
/*
* 包含一个超链接,点击后完成激活
* 超链接中要有激活码这个参数!
*/
String content = props.getProperty("content");//其中包含点位符
content = MessageFormat.format(content, form.getCode());//把content中的点位符替代成当前用户的激活码
// 得到session
Session session = MailUtils.createSession(host, username, password);
// 创建Mail对象
Mail mail = new Mail(from, to, subject, content);
// 发送
try {
MailUtils.send(session, mail);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}