栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Maven的JAVA WEB环境搭建

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Maven的JAVA WEB环境搭建

1.创建一个Mavem的基本项目

2.构建web工程

 

  • 配置pom.xml的文件
  • 
    
        4.0.0
    
        com.csi
        newsmgrsys
        1.0-SNAPSHOT
    
        
            8
            8
        
        war
           
            
            
                javax.servlet
                javax.servlet-api
                4.0.1
                provided
            
            
            
                javax.servlet.jsp
                javax.servlet.jsp-api
                2.3.3
                provided
            
            
            
                mysql
                mysql-connector-java
                8.0.30
            
            
            
                org.junit.jupiter
                junit-jupiter-api
                5.9.0
                test
            
    
    
        
    
        
            
                
                    org.apache.maven.plugins
                    maven-compiler-plugin
                    3.1
                    
                        1.8
                        1.8
                    
                
                
    
                    org.apache.tomcat.maven
                    tomcat7-maven-plugin
                    2.2
                    
                        8080
                        /newsmgrsys
                        UTF-8
                    
                
            
        
    
    6.1使用JAVA WEB 实现与数据库连接登录的功能

                1.建立数据库

create table test_user
(
userid int auto_increment
primary key,
username varchar(30) null,
password varchar(32) null,
gender char(2) null,
borndate date null,
power int default 0 null
);
 2.JDBCUtils
package com.csi.eshop.utils;

import java.io.IOException;
import java.sql.*;
import java.util.Properties;


public class JDBCUtil {
    private  Properties properties;

    {
        //构建对象
        properties =new Properties();
        try {
            properties.load(JDBCUtil.class.getClassLoader().getResourceAsStream("db.properties"));
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public static void main(String[] args) throws SQLException {
        System.out.println(new JDBCUtil().getConnection());
    }

    
    protected Connection getConnection() throws SQLException {
        try {
            Class.forName(getValue("classname"));
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

        //2.建立连接
        Connection connection = DriverManager.getConnection(getValue("url"),getValue("username"),getValue("password"));

        return connection;


    }

    
    public void release(ResultSet rs, PreparedStatement ps, Connection connection) {
        release(rs);
        release(ps);
        release(connection);
    }

    public void release(PreparedStatement ps, Connection connection) {
        release(ps);
        release(connection);
    }

    public void release(Connection connection){
        if (connection !=null){
            try {
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    public void release(Statement statement){
        if (statement!=null){
            try {
                statement.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }

    }

    public void release(ResultSet rs){
        if (rs!=null){
            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }

        }


    }
    public  String getValue(String key){
        return properties.getProperty(key);

    }

}
 Resource下的db.properties文件
classname=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/easyshop
username=root
password=root
 3.Userinfo对象
package com.csi.eshop.domain;

import java.io.Serializable;

public class UserInfo implements Serializable {
    private Integer id ;
    private String loginName ;
    private String userName ;
    private String password ;
    private int sex ;
    private String identityCode ;
    private String email ;
    private String mobile ;

    public UserInfo() {
    }

    public UserInfo(Integer id, String loginName, String userName, String password, int sex, String identityCode, String email, String mobile, int type) {
        this.id = id;
        this.loginName = loginName;
        this.userName = userName;
        this.password = password;
        this.sex = sex;
        this.identityCode = identityCode;
        this.email = email;
        this.mobile = mobile;
        this.type = type;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getLoginName() {
        return loginName;
    }

    public void setLoginName(String loginName) {
        this.loginName = loginName;
    }

    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 int getSex() {
        return sex;
    }

    public void setSex(int sex) {
        this.sex = sex;
    }

    public String getIdentityCode() {
        return identityCode;
    }

    public void setIdentityCode(String identityCode) {
        this.identityCode = identityCode;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public int getType() {
        return type;
    }

    public void setType(int type) {
        this.type = type;
    }

    private int type ;
}
4.UserInfoDao接口
package com.csi.eshop.dao;

import com.csi.eshop.domain.UserInfo;

import java.sql.SQLException;

public interface UserInfoDao {
    UserInfo login(String loginName, String password) throws SQLException;
    int register(String loginName, String password,String email,String mobile) throws SQLException;

    UserInfo selectByUsername(String loginName) throws SQLException;
}

5.UserInfoDaoImpl实现类

package com.csi.eshop.dao.Impl;

import com.csi.eshop.dao.UserInfoDao;
import com.csi.eshop.domain.UserInfo;
import com.csi.eshop.utils.JDBCUtil;

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

public class UserInfoDaoImpl extends JDBCUtil implements UserInfoDao {
    @Override
    public UserInfo login(String loginName, String password) throws SQLException {
        //1. 编写一条SQL语句
        final String SQL = "SELECT * FROM easybuy_user WHERe loginName = ? AND password = ?" ;
        //2. 创建JDBC连接
        Connection connection = this.getConnection();
        //3. 创建PreparedStatement对象
        PreparedStatement ps = connection.prepareStatement(SQL);
        //3.1 设置占位符
        ps.setString(1,loginName);
        ps.setString(2,password);
        //4. 发起查询,返回ResultSet
        ResultSet rs = ps.executeQuery() ;
        UserInfo userInfo = null ;
        //5. 判断结果集是否存在数据
        if(rs.next()) {
        //6. 如果存在数据,封装对象,返回对象
            userInfo = new UserInfo() ;
            userInfo.setId(rs.getInt("id"));
            userInfo.setUserName(rs.getString("username"));
            userInfo.setEmail(rs.getString("email"));
            userInfo.setLoginName(loginName);
            userInfo.setIdentityCode(rs.getString("identityCode"));
            userInfo.setMobile(rs.getString("mobile"));
            userInfo.setSex(rs.getInt("sex"));
        }
        //7. 关闭资源
        this.release(rs,ps,connection);
        return userInfo;
    }

    @Override
    public int register(String loginName, String password,String email,String mobile) throws SQLException {
        final String SQL = "INSERT INTO easybuy_user(loginName,password,email,mobile) VALUE(?,?,?,?)"; //2.创建JDBC连接
        Connection connection = this.getConnection();
        //3.创建PrepareStatement对象
        PreparedStatement ps = connection.prepareStatement(SQL);
//        int id = 3;

//        ps.setInt(1,3);
        ps.setString(1,loginName);
        ps.setString(2,password);
        ps.setString(3,email);
        ps.setString(4,mobile);

        int num = ps.executeUpdate();
        //7.关闭资源
        this.release(ps,connection);

        return num;
    }

    @Override
    public UserInfo selectByUsername(String loginName) throws SQLException {
        //1.编写一条SQL语句
        final String SQL = "SELECT * FROM easybuy_user WHERe loginName=? ";
        //2.创建JDBC连接
        Connection connection = this.getConnection();
        //3.创建PrepareStatement对象
        PreparedStatement ps = connection.prepareStatement(SQL);
        //3.1设置占位符
        ps.setString(1,loginName);
        //4.发起查询,返回ResultSet
        ResultSet rs = ps.executeQuery();
        UserInfo userInfo =null;
        //5.判断结果集是否存在数据
        if (rs.next()){
            //6.如果存在数据,封装对象,返回对象
            userInfo =new UserInfo();
            userInfo.setId(rs.getInt("id"));
        }

        //7.关闭资源
        this.release(rs,ps,connection);

        return userInfo;
    }
}
6.UserInfoService接口以及UserInfoService实现类
package com.csi.eshop.service;

import com.csi.eshop.domain.UserInfo;

import java.sql.SQLException;

public interface UserInfoService {
    UserInfo login(String loginName, String password) ;
    int register(String loginName, String password,String email,String mobile);

    UserInfo selectByUsername(String loginName);

//    UserInfo selectByUsername(String loginName);
}
6.UserInfoService接口以及UserInfoService实现类
package com.csi.eshop.service.Impl;

import com.csi.eshop.dao.Impl.UserInfoDaoImpl;
import com.csi.eshop.dao.UserInfoDao;
import com.csi.eshop.domain.UserInfo;
import com.csi.eshop.service.UserInfoService;
import com.csi.eshop.utils.MD5Utils;

import java.sql.SQLException;

public class UserInfoServiceImpl implements UserInfoService {
    public static void main(String[] args) {
        System.out.println(MD5Utils.str2MD5("123456"));
    }
    @Override
    public UserInfo login(String loginName, String password) {
         password = MD5Utils.str2MD5(password);

        //调用UserInfoDao的login方法
        UserInfoDao userInfoDao = new UserInfoDaoImpl() ;
        UserInfo userInfo = null;
        try {
            userInfo = userInfoDao.login(loginName, password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return userInfo;
    }

    @Override
    public int register(String loginName, String password, String email, String mobile)  {
        UserInfoDao userInfoDao =new UserInfoDaoImpl();
        int num = 0;
        try {
            num = userInfoDao.register(loginName,password,email,mobile);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return num;
    }

    @Override
    public UserInfo selectByUsername(String loginName) {
        //调用userInfoDao中的login方法
        UserInfoDao userInfoDao =new UserInfoDaoImpl();
        UserInfo userInfo=null;
        try {
            userInfo =userInfoDao.selectByUsername(loginName);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return userInfo;
    }
}
7.LoginController.jsp
<%@ page import="com.csi.eshop.service.UserInfoService" %>
<%@ page import="com.csi.eshop.service.Impl.UserInfoServiceImpl" %>
<%@ page import="com.csi.eshop.domain.UserInfo" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    //设置编码
    request.setCharacterEncoding("UTF-8");
    response.setCharacterEncoding("UTF-8");
    //获取用户名以及密码
    String loginName = request.getParameter("loginName");
    String password = request.getParameter("password") ;

    if ((loginName != null && password != null)
            && (!("".equals(loginName.trim()) && "".equals(password.trim())))) {
        //调用Service实现数据的查询
        UserInfoService userInfoService = new UserInfoServiceImpl();
        UserInfo userInfo = userInfoService.login(loginName, password);
        if (userInfo != null) {
            //获取到用户是否选中了免登录按钮
            String[] isChoice = request.getParameterValues("remember_me");
            if (isChoice != null) { //如果没有为null,说明用户选择7天免登录按 钮
                //创建cookie
                Cookie cookie = new Cookie("userinfo", userInfo.getLoginName());
                //设置cookie属性
                cookie.setPath("/");
                cookie.setMaxAge(3600 * 24 * 7);
                //写出cookie
                response.addCookie(cookie);

            }
            将用户信息存储到session范围内
            session.setAttribute("userinfo",userInfo);
            //重定向到首页
            response.sendRedirect("PageIndexController.jsp");
        }else {
            request.setAttribute("msg","登录失败!");
            //希望带值跳转,只能使用转发格式
            //request.getContextPath():获取到当前项目的上下文路径
            request.getRequestDispatcher("/login.jsp").forward(request,response);
        }
    }
%>
8.login.jsp
<%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

    


           
             
                  
               
             
             
               
                  <%
                      String username="";
                      Cookie[] cookies = request.getCookies();
                      for (Cookie cookie : cookies) {
                          if("userinfo".equals(cookie.getName())){
                              username  =  cookie.getValue();
                          }
                      }
                  %>
               
             
             
               
               
             
             
                  
               
             
             
                  
               
             
           
 
                    登录
                    还没有商城账号,立即注册
                    <%--新增--%>
                   

               

用户名
密    码
 
                    
                        
                   
                    忘记密码
               
 

           

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1040707.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号