MailService
package mail.service; import java.util.List; import mail.dao.DaoFactory;
import mail.dao.MailDao;
import mail.dao.UserDao;
import mail.domain.Mail;
import mail.domain.User; public class MailService {
private UserDao userDao = DaoFactory.getUserDao();
private MailDao mailDao = DaoFactory.getMailDao(); public User login(User form) throws UserException {
/*
* 使用form中的username进行查询
*/
User user = userDao.findByUsername(form.getUsername());
/*
* 如果返回null说明用户名不存在,抛出异常,异常信息为"用户名不存在"
*/
if (user == null) {
throw new UserException("用户名不存在");
}
/*
* 比较user的password和form的password,如果不同抛出异常
*/
if (!form.getPassword().equals(user.getPassword())) {
throw new UserException("密码错误");
}
return user;
}
public List<Mail> findByName(String username){
List<Mail> mail = mailDao.findByName(username);
return mail; }
public Mail findById(int id){
Mail mail = mailDao.findById(id);
return mail; }
public void addMail(Mail mail) {
mailDao.addMail(mail);
}
public void deleteById(int id) {
mailDao.deleteById(i

MailServlet
package mail.web.servlet;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List; import javax.mail.Session;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.junit.Test; import mail.domain.Mail;
import mail.domain.User;
import mail.service.MailService;
import mail.service.UserException;
import cn.itcast.servlet.BaseServlet; public class MailServlet extends BaseServlet { public String login(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
User form = new User();
String username = request.getParameter("username");
String password = request.getParameter("password");
String email = request.getParameter("email"); form.setUsername(username);
form.setPassword(password);
form.setEmail(email); try {
MailService us = new MailService();
User user = us.login(form);
request.getSession().setAttribute("userName", user.getUsername()); return "r:/MailServlet?method=findByName";
} catch (UserException e) {
request.setAttribute("msg", e.getMessage());
request.setAttribute("user", form);
return "f:/Login/login.jsp";
} } public String findByName(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
MailService us = new MailService();
List<Mail> mails = us.findByName((String)request.getSession().getAttribute("userName"));
request.setAttribute("mails", mails);
return "f:/Mail/show.jsp"; } public String findById(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
MailService us = new MailService();
String param = request.getQueryString();
Mail findMail = us.findById(Integer.parseInt(param.split("&")[1]
.split("=")[1]));
request.getSession().setAttribute("fjr", findMail.getSendto());
request.getSession().setAttribute("bt", findMail.getTitle());
request.getSession().setAttribute("zw", findMail.getMsgcontent());
return "f:/Mail/showMail.jsp"; } public String addMail(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
MailService us = new MailService();
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
Mail mail = new Mail(request.getParameter("sjr"), request.getParameter("bt"), request.getParameter("zw"),
1, (String) request.getSession().getAttribute("userName"), df.format(new Date()));
us.addMail(mail);
return "f:/MailServlet?method=findByName";
} public String deleteById(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String param = (String) request.getSession().getAttribute("ID");
MailService us = new MailService();
us.deleteById(Integer.valueOf(param.split("&")[1].split("=")[1])); return "f:/MailServlet?method=findByName";
} @Test
public void fun() {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式
df.format(new Date());
}
JdbcMailDaoImp1
package mail.dao; import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List; import org.junit.Test; import mail.domain.Mail; import com.mysql.jdbc.Connection; public class JdbcMailDaoImpl implements MailDao { public List<Mail> findByName(String username) {
List<Mail> Mails = new ArrayList<Mail>();
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
// 得到数据库的连接
con = JdbcUtils.getConnercion();
// 定义sql语句 得到pstmt
String sql = "SELECT * FROM msg WHERE username=?";
pstmt = con.prepareStatement(sql);
pstmt.setString(1, username);
// 执行sql语句
rs = pstmt.executeQuery();
// 把rs转换成mail类 while (rs.next()) {
// 转换成mail类
Mail mail = new Mail();
mail.setMsgid(rs.getInt("msgid"));
mail.setUsername(rs.getString("username"));
mail.setTitle(rs.getString("title"));
mail.setMsgcontent(rs.getString("msgcontent"));
mail.setState(rs.getInt("state"));
mail.setSendto(rs.getString("sendto"));
mail.setMsg_create_date(rs.getString("msg_create_date"));
Mails.add(mail); }
return Mails;
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
try {
if (pstmt != null)
pstmt.close();
if (con != null)
con.close();
} catch (SQLException e) {
}
}
} public Mail findById(int id) {
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
// 得到数据库的连接
con = JdbcUtils.getConnercion();
// 定义sql语句 得到pstmt
String sql = "SELECT * FROM msg WHERE msgid=?";
pstmt = con.prepareStatement(sql);
// 给sql语句中的问好赋值
pstmt.setInt(1, id);
// 执行sql语句
rs = pstmt.executeQuery(); if (rs == null)
return null;
if (rs.next()) {
Mail mail = new Mail();
mail.setMsgid(rs.getInt("msgid"));
mail.setUsername(rs.getString("username"));
mail.setTitle(rs.getString("title"));
mail.setMsgcontent(rs.getString("msgcontent"));
mail.setState(rs.getInt("state"));
mail.setSendto(rs.getString("sendto"));
mail.setMsg_create_date(rs.getString("msg_create_date"));
return mail;
} else {
return null;
}
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
try {
if (pstmt != null)
pstmt.close();
if (con != null)
con.close();
} catch (SQLException e) {
}
}
} public void addMail(Mail mail) {
Connection con = null;
PreparedStatement pstmt = null;
try {
// 得到数据库的连接
con = JdbcUtils.getConnercion();
// 定义sql语句 得到pstmt
// insert into
// msg(username,title,msgcontent,state,sendto,msg_create_date)
String sql = "INSERT INTO msg(username,title,msgcontent,state,sendto,msg_create_date) VALUES(?,?,?,?,?,?)";
pstmt = con.prepareStatement(sql);
// 给sql语句中的问好赋值
pstmt.setString(1, mail.getUsername());
pstmt.setString(2, mail.getTitle());
pstmt.setString(3, mail.getMsgcontent());
pstmt.setInt(4, mail.getState());
pstmt.setString(5, mail.getSendto());
pstmt.setString(6, mail.getMsg_create_date());
// 执行sql语句
pstmt.executeUpdate(); } catch (Exception e) {
throw new RuntimeException(e);
} finally {
// 关闭(倒关)
try {
if (pstmt != null)
pstmt.close();
if (con != null)
con.close();
} catch (SQLException e) {
}
}
} public void deleteById(int id) {
Connection con = null;
PreparedStatement pstmt = null;
try {
// 得到数据库的连接
con = JdbcUtils.getConnercion();
// 定义sql语句 得到pstmt
// DELETE FROM 表名 [WHERE 条件
String sql = "DELETE FROM msg WHERE msgid=?";
pstmt = con.prepareStatement(sql);
// 给sql语句中的问好赋值
pstmt.setInt(1, id);
// 执行sql语句
pstmt.executeUpdate(); } catch (Exception e) {
throw new RuntimeException(e);
} finally {
// 关闭(倒关)
try {
if (pstmt != null)
pstmt.close();
if (con != null)
con.close();
} catch (SQLException e) {
}
}
}
}
JdbcUserDaoImp1.java
package mail.dao; import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException; import com.mysql.jdbc.Connection; import mail.domain.User; public class JdbcUserDaoImpl implements UserDao { @Override
public User findByUsername(String username) {
Connection con = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
// 得到数据库的连接
con = JdbcUtils.getConnercion();
// 定义sql语句 得到pstmt
String sql = "SELECT * FROM m_user WHERE username=?";
pstmt = con.prepareStatement(sql);
// 给sql语句中的问好赋值
pstmt.setString(1, username);
// 执行sql语句
rs = pstmt.executeQuery(); // 把rs转换成user类
if (rs == null)
return null;
if (rs.next()) {
// 转换成user类
User mail = new User();
mail.setUsername(rs.getString("username"));
mail.setPassword(rs.getString("password"));
mail.setEmail(rs.getString("email"));
return mail;
} else {
return null;
}
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
try {
if (pstmt != null)
pstmt.close();
if (con != null)
con.close();
} catch (SQLException e) {
}
}
} }

jsp第7个作业的更多相关文章

  1. JSP第十一次作业

    1.第十二周上机作业(邮件功能)的控制层代码改用为servlet实现.2.学习通发布了考试,截止到本周六.  com.gd.dao  BaseDao 1 package com.gd.dao; 2 3 ...

  2. jsp内置对象作业3-application用户注册

    1,注册页面 zhuCe.jsp <%@ page language="java" contentType="text/html; charset=UTF-8&qu ...

  3. jsp内置对象作业2-留言簿

    1.留言簿页面:liuYan.jsp <%@ page language="java" contentType="text/html; charset=UTF-8& ...

  4. jsp内置对象作业1-用户登录

    题目:编写一个jsp程序,实现用户登录,当用户输入的用户名或密码错误时,将页面重定向到错误提示也,并在该页面显示30秒后,自动返回到用户登录页面. 1.用户登录页面 <%@ page langu ...

  5. jsp第七周作业

    1.p78-p79的例4-9 <%@ page language="java" import="java.util.*" pageEncoding=&qu ...

  6. jsp第六次作业

    1. <%@ page language="java" import="java.util.*" pageEncoding="utf-8&quo ...

  7. jsp第三次作业

    1.在jsp页面中使用include动态标记加载音频 <%@ page language="java" import="java.util.*" page ...

  8. JSP第八次作业

    数据库test 中建个表 stu(stuid 主键 自动增长 ,用户名,密码,年龄) 1.设计一个注册页面,实现用户注册功能2.设计一个登陆页面,实现用户名密码登陆3.两个页面可以互相超链接 1 pa ...

  9. JSP第七次作业

    1.做一个图书类Book id,name,price ,get,set访问器,构造方法2个,1个无参,1个有参做一个测试类,在main中创建3个图书对象,放到list集合中.做一个菜单,可以添加,删除 ...

  10. JSP第五次作业

    1.教材P78-79  例4-9 1 <%@ page language="java" import="java.util.*" pageEncoding ...

随机推荐

  1. POWER BI - 根据条件,改变数值的颜色Conditional formatting

    在visual块,右键Column选择Conditional formatting 举例,比如difference > 0 显示红色,否则<0显示绿色 如何根据 measure 写特定条件 ...

  2. WEB的安全性测试要素【转】

    原文链接:http://www.cnblogs.com/zgqys1980/archive/2009/05/13/1455710.html WEB的安全性测试要素 1.SQL Injection(SQ ...

  3. Vue+element ui 笔记

    1)可以直接拿过来就用的样式 https://www.cnblogs.com/xiao987334176/p/14188413.html 2)对Table里面的每一项全部设定为选中 mounted() ...

  4. vue-购物车的部分

    定义一个局部组件 分成三个部分 Vue.component('my-cart',{ template: ` <div class='cart'> <cart-title>< ...

  5. 小程序modal弹窗

    [注意]css放的位置可能影响效果 参考a-level competitionFilterCover 1.容器:position: absolute; top: 100vh; animation: m ...

  6. 看K线学炒股(0803)

    高位板块的高位个股要回避,即便走反抽行情也要慎重.像容捷股份. 同是房地产行业的荣盛发展和西藏城投,差别怎么这么大?荣盛发展市净率0.4,西藏城投是4.8,市盈率更是一个天上一个地下,但股价呢?所以股 ...

  7. dynamics 365/crm 导入解决方案报 发生 sql server 错误

    dynamics 365/crm 导入解决方案报 发生 sql server 错误.{1}{0} 错误代码 80044150. 帮助我解决此问题. 这时候,可以检查数据库服务器的日志看看,可能会找到S ...

  8. Vue3学习笔记

    为什么需要Composition API ? 主要原因:当一个组件的变得逻辑复杂的时候,痛点:多种逻辑代码被分散到组件的各个部分,比如代码的相关逻辑可能会在 data: {...},computed: ...

  9. cublas fp16

    编译选项: nvcc 4.cpp -o test_gemm  -lcudart -lcuda -lcublas -std=c++11 #include <sys/time.h> #incl ...

  10. 【批处理】启动 VMware 服务的简单批处理脚本

    早些时候编写的一个启动VMware服务的简单批处理脚本. 主要是记录一下自动提升运行权限的命令,不用再右键"以管理员身份运行". @echo off mode con cols=9 ...