java web 商城练习


package
com.oracle.tools; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; public class JDBCUtils { // 获取连接对象 public static Connection getConn() { // 1.注册驱动 Connection conn=null; try { Class.forName("com.mysql.jdbc.Driver"); // 2.获取连接对象 String url = "jdbc:mysql://localhost:3306/store_v1.0?characterEncoding=utf8"; String user = "root"; String pwd = "123456"; conn= DriverManager.getConnection(url, user, pwd); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } return conn; } //增删改释放资源 public static void close(Connection conn, PreparedStatement pst){ if(pst!=null){ try { pst.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(conn!=null){ try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } //查询释放资源 public static void close(Connection conn, PreparedStatement pst,ResultSet rs){ if(rs!=null){ try { rs.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(pst!=null){ try { pst.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(conn!=null){ try { conn.close(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } }
package com.oracle.tools;

import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;

public class MailUtils {
//授权密码:HJYEPEPIHOVMTSOU


    public static void sendMail(String email, String emailMsg)
            throws AddressException, MessagingException {
        // 1.创建一个程序与邮件服务器会话对象 Session

        Properties props = new Properties();
        props.setProperty("mail.transport.protocol", "SMTP");
        props.setProperty("mail.host", "smtp.126.com");
        props.setProperty("mail.smtp.auth", "true");// 指定验证为true

        // 创建验证器
        //HJYEPEPIHOVMTSOU
        Authenticator auth = new Authenticator() {
            public PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("a1605948162", "HJYEPEPIHOVMTSOU");
            }
        };

        Session session = Session.getInstance(props, auth);

        // 2.创建一个Message,它相当于是邮件内容
        Message message = new MimeMessage(session);

        message.setFrom(new InternetAddress("a1605948162@126.com")); // 设置发送者

        message.setRecipient(RecipientType.TO, new InternetAddress(email)); // 设置发送方式与接收者

        message.setSubject("用户激活");
        // message.setText("这是一封激活邮件,请<a href='#'>点击</a>");

        message.setContent(emailMsg, "text/html;charset=utf-8");

        // 3.创建 Transport用于将邮件发送

        Transport.send(message);
    }
}
package com.oracle.domain;

import java.util.Date;

public class Users {
    private String uid;
    private String username;
    private String password;
    private String name;
    private String email;
    private String telephone;
    private Date birthday;
    private String sex;
    private Integer state;
    private String code;
    public String getUid() {
        return uid;
    }
    public void setUid(String uid) {
        this.uid = uid;
    }
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        this.username = username;
    }
    public String getPassword() {
        return password;
    }
    public void setPassword(String password) {
        this.password = password;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getEmail() {
        return email;
    }
    public void setEmail(String email) {
        this.email = email;
    }
    public String getTelephone() {
        return telephone;
    }
    public void setTelephone(String telephone) {
        this.telephone = telephone;
    }
    public Date getBirthday() {
        return birthday;
    }
    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public Integer getState() {
        return state;
    }
    public void setState(Integer state) {
        this.state = state;
    }
    public String getCode() {
        return code;
    }
    public void setCode(String code) {
        this.code = code;
    }
    @Override
    public String toString() {
        return "Users [uid=" + uid + ", username=" + username + ", password=" + password + ", name=" + name + ", email="
                + email + ", telephone=" + telephone + ", birthday=" + birthday + ", sex=" + sex + ", state=" + state
                + ", code=" + code + "]";
    }
    
}
package com.oracle.web.user;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.Map;
import java.util.UUID;

import javax.mail.MessagingException;
import javax.mail.internet.AddressException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.converters.DateConverter;

import com.oracle.domain.Users;
import com.oracle.service.UserService;
import com.oracle.tools.MailUtils;

public class RegisterServlet extends HttpServlet {
private UserService userService=new UserService();
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //解决请求乱码
        request.setCharacterEncoding("UTF-8");
        //获取用户请求参数
        Map<String,String[]> map=request.getParameterMap();
        //创建Users对象
        Users user=new Users();
        //创建自定义转换器负责将String转换为Date类型
        DateConverter converter=new DateConverter();
        //设置转换规则
        converter.setPattern("yyyy-MM-dd");
        //用工具类进行转换
        ConvertUtils.register(converter,Date.class);
        //将map集合中的所有参数封装到user对象中
        try {
            BeanUtils.populate(user,map);
        } catch (IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        //封装uid(主键),随机产生36位不重复的字母和数字组合
        user.setUid(UUID.randomUUID().toString());
        //封装用户信息状态
        user.setState(0);
        //封装状态码
        String code=UUID.randomUUID().toString();
        user.setCode(code);
        //
        user.setTelephone("138643xxxxx");
        //调用service层方法
        int row=userService.resgister(user);
        if(row>0){
            //注册成功
            //发送激活邮件
            String emailIMsg="恭喜你注册成功!请点击已下链接激活用户<a href='http://localhost:8080/Market02/ActiveServlet?code="+code+"'>http://localhost:8080/Market02/ActiveServlet?code="+code+"</a>";
            //重新定向到登录页面
            try {
                MailUtils.sendMail(user.getEmail(),emailIMsg );
            } catch (AddressException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (MessagingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            response.sendRedirect(request.getContextPath()+"/login.jsp");
        }else{
            //重新定向到注册页面
            response.sendRedirect(request.getContextPath()+"/active.jsp");
        }
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
package com.oracle.dao;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import com.oracle.domain.Users;
import com.oracle.tools.JDBCUtils;

public class UserDao {
//注册
    public int resgister(Users user) throws SQLException{
        Connection conn=JDBCUtils.getConn();
        String sql="insert into users values(?,?,?,?,?,?,?,?,?,?)";
        PreparedStatement pst=conn.prepareStatement(sql);
        pst.setString(1,user.getUid());
        pst.setString(2,user.getUsername());
        pst.setString(3,user.getPassword());
        pst.setString(4,user.getName());
        pst.setString(5,user.getEmail());
        pst.setString(6,user.getTelephone());
        pst.setDate(7,new Date(user.getBirthday().getTime()));
        pst.setString(8,user.getSex());
        pst.setInt(9,user.getState());
        pst.setString(10,user.getCode());
        int row=pst.executeUpdate();
        JDBCUtils.close(conn, pst);
        return row;
    }
}
package com.oracle.service;

import java.sql.SQLException;

import com.oracle.dao.UserDao;
import com.oracle.domain.Users;

public class UserService {
private UserDao userDao=new UserDao();
public int resgister(Users user){
    int row=0;
    try {
        row=userDao.resgister(user);
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return row;
}
}

register.jsp文件:<form class="form-horizontal" style="margin-top: 5px;" action="/Market02/RegisterServlet" method="post">

原文地址:https://www.cnblogs.com/maxuefeng/p/13978764.html