<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>Write Email.jsp</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <body> <form action="dowrite.jsp" method="post"> 收件人:<input type="text" name="sjr" value="" /><br/> 标 题:<input type="text" name="title" /> <br/> 正 文:<br/> <textarea rows="10" cols="30" name="zw"></textarea> <br/> <input type="submit" value="发送" /> <a href="main.jsp">返回</a> </form> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ page import="com.ql.entity.Users" %> <%@ page import="com.ql.entity.Msg" %> <%@ page import="com.ql.dao.MsgDao" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <% request.setCharacterEncoding("utf-8"); Users u = (Users) session.getAttribute("user"); String uname = request.getParameter("sjr"); String title = request.getParameter("title"); String content = request.getParameter("zw"); Msg msg = new Msg(); msg.setUsernname(u.getUsername()); msg.setSendto(uname); msg.setTitle(title); msg.setMsgcontent(content); MsgDao md = new MsgDao(); md.addMail(msg); request.getRequestDispatcher("main.jsp").forward(request, response); %>
public boolean addMail(Msg msg){ Connection con=null; try{ //获取连接 con = getConnection(); //书写SQL语句 String sql = "insert into msg(username,title,msgcontent,state,sendto,msg_create_date) values(?,?,?,?,?,?)"; PreparedStatement st =con.prepareStatement(sql); st.setString(1, msg.getUsernname());// 给sql语句的问号赋值 st.setString(2, msg.getTitle()); st.setString(3, msg.getMsgcontent()); st.setInt(4, 0);// 刚插入的邮件为未读邮件,直接赋值0 st.setString(5, msg.getSendto()); st.setDate(6, new java.sql.Date(new Date().getTime())); int row = st.executeUpdate(); if(row>0){ //发送成功 return true; } closeAll(con, st, null); }catch(SQLException e){ e.printStackTrace(); } return false; }