俩天利用空闲时间把书城项目的第二个阶段给弄了,过程很曲折,但最后还是搞好了,尽管看上去有点破碎,但总归是好的,下面利用外卖没到的时间总结一波,纪念下第一次搞这么大的项目!!!
首先是总体框架的:
搭建书城项目开发环境
第二个阶段只要是和数据库建立连接来判断用户注册登录的合法性!
首先是用户类的创建:
接着是数据库的连接,在进行之前我首先进行了book 数据库的创建,接着建立一张表,来放用户的数据,即id username passwor email 这些,创建的时候把id作为主键,其他字段的话用户名限制为unique ,not null 其他的设置为not null 就可以了。
完成这些准备以后就是数据库连接池的玩意了,我使用了德鲁伊连接池,在这里我遇到了一个非常麻烦的玩意那就是配置文件的读取,之前是一直胡乱放一个地方,跳坑里面去了,不应该这样子,最后我上网查,成功解决了,那就是把这玩意放在resours里面,才能读得到!
配置文件信息:
username=root password=root url=jdbc:mysql://localhost:3306/book?useSSL=false&serverTimezone=UTC driverClassName=com.mysql.cj.jdbc.Driver initialSize=5 maxActive=10
哦对了,还有就是jar包放入的位置也该放在web_Inf中,建立也该lib放他进去(注意也该将他们进行那玩意)
接着是dao层的实现了,这里主要涉及了一些反射和泛型的玩意
#basedao
package com.atguigudb.dao; import com.alibaba.druid.util.JdbcUtils; import com.atguigudb.utils.jdbcutils; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.BeanHandler; import org.apache.commons.dbutils.handlers.BeanListHandler; import org.apache.commons.dbutils.handlers.ScalarHandler; import java.sql.Connection; import java.sql.SQLException; import java.util.List; public abstract class basedao { private QueryRunner queryRunner=new QueryRunner(); public int updata(String sql,Object...args) throws SQLException { Connection connection = null; try { connection = jdbcutils.getConnection(); return queryRunner.update(connection,sql,args); } catch (SQLException throwables) { throwables.printStackTrace(); } finally { jdbcutils.close(connection); } return -1; } public <T> T queryForOne(Class<T> type,String sql,Object...args) throws SQLException { Connection connection=null; try { connection=jdbcutils.getConnection(); return queryRunner.query(connection,sql, new BeanHandler<T>(type),args); } catch (SQLException throwables) { throwables.printStackTrace(); } finally { jdbcutils.close(connection); } return null; } public <T> List<T> queryForlist(Class<T> type,String sql,Object...args) throws SQLException { Connection connection=jdbcutils.getConnection(); try { return queryRunner.query(connection,sql, new BeanListHandler<T>(type),args); } catch (SQLException throwables) { throwables.printStackTrace(); } finally { jdbcutils.close(connection); } return null; } public Object querylevalues(String sql,Object...args) throws SQLException {Connection connection=null; try { connection=jdbcutils.getConnection(); return queryRunner.query(connection,sql,new ScalarHandler(),args); } catch (SQLException throwables) { throwables.printStackTrace(); } finally { jdbcutils.close(connection); } return null; } }
#usedao
package com.atguigudb.dao; import com.atguigudb.pojo.User; import java.sql.SQLException; public class usedao extends basedao implements userdao { @Override public User queryByUsername(String username) throws SQLException { String sql="select id,username,password,email from t_user where username=?"; return queryForOne(User.class,sql,username); } @Override public User queryByUsernamepassword(String name,String password) throws SQLException { String sql="select id,username,password,email from t_user where username=? and password=?"; return queryForOne(User.class,sql,name,password); } @Override public int save(User user) throws SQLException { String sql="insert into t_user(username,password,email)values(?,?,?)"; return updata(sql,user.getUsername(),user.getPassword(),user.getEmail()); } }
#接口
package com.atguigudb.dao; import com.atguigudb.dao.basedao; import com.atguigudb.pojo.User; import java.sql.SQLException; public interface userdao { /** * 根据用户名查看合法性, * @return 如果不合法返回null */ public User queryByUsername(String name) throws SQLException; /** * 根据用户名查看合法性, * @return 如果不合法返回null,没有这个用户 */ public User queryByUsernamepassword(String name,String password) throws SQLException; public int save(User user) throws SQLException; }
package com.atguigudb.imp; import com.atguigudb.pojo.User; import java.sql.SQLException; public interface userser { /** * 注册用户 * @param user */ public void regiser(User user) throws SQLException; /** * * 登录密码和账户的检查 * @param user * @return */ public User login(User user) throws SQLException; /** * 查看用户名是不是存在 * @param user * @return */ public boolean exitname(String name) throws SQLException; }
package com.atguigudb.imp; import com.atguigudb.dao.usedao; import com.atguigudb.pojo.User; import java.sql.SQLException; public class useseve implements userser{ private usedao usedao=new usedao(); @Override public void regiser(User user) throws SQLException { usedao.save(user); } @Override public User login(User user) throws SQLException { return usedao.queryByUsernamepassword(user.getUsername(),user.getPassword()); } @Override public boolean exitname(String name) throws SQLException { if(usedao.queryByUsername(name)==null) { return false; } else return true; } }
package com.atguigudb.utils; import com.alibaba.druid.pool.DruidDataSource; import com.alibaba.druid.pool.DruidDataSourceFactory; import com.alibaba.druid.util.JdbcUtils; import java.io.InputStream; import java.sql.Connection; import java.sql.SQLException; import java.util.Properties; public class jdbcutils { private static DruidDataSource dataSource; static { Properties properties = new Properties(); try { InputStream resourceAsStream = JdbcUtils.class.getClassLoader().getResourceAsStream("jdbc.properties"); properties.load(resourceAsStream); //创建数据库连接池 dataSource = (DruidDataSource) DruidDataSourceFactory.createDataSource(properties); } catch (Exception e) { e.printStackTrace(); } } public static Connection getConnection() { Connection conn=null; try { conn = dataSource.getConnection(); } catch (SQLException throwables) { throwables.printStackTrace(); } return conn; } public static void close(Connection conn) throws SQLException { conn.close(); } }
接下来就是最重要的web层面的实现
在这里开始之前我们在这个工程下面采用base 固定先对路径
把注册表中action改为post请求
web层的请求也也该是post请求
<base href="http://localhost:8080//bookitem_war/">
改了以后,记得这时候是在工程路径下面了,在跳转其他链接,资源的时候千万千万不要加 /
那样子你会死的很惨,我就是,苦死我了阿
在注册,登录 跳转的时候要有/
下面是具体
login和register的玩意
public class register extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { useseve use=new useseve(); String name = req.getParameter("username"); String password = req.getParameter("password"); String email = req.getParameter("email"); String code = req.getParameter("code"); System.out.println("get"); if("abcd".equalsIgnoreCase(code)) { try { if(use.exitname(name)==true) { System.out.println("账号名已经存在"); req.getRequestDispatcher("/pages/user/regist.html").forward(req,resp); } else { use.regiser(new User(null,name,password,email)); req.getRequestDispatcher("/pages/user/regist_success.html").forward(req,resp); } } catch (SQLException throwables) { throwables.printStackTrace(); } } else { System.out.println("验证码错误"); req.getRequestDispatcher("/pages/user/regist.html").forward(req,resp); } } }
public class login extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { useseve use=new useseve(); String name=req.getParameter("username"); String password = req.getParameter("password"); try { if(use.login(new User(null,name,password,null))!=null) { req.getRequestDispatcher("/pages/user/login_success.html").forward(req,resp); } else { req.getRequestDispatcher("/pages/user/login.html").forward(req,resp); } } catch (SQLException throwables) { throwables.printStackTrace(); } } }
配置文件如下servele配置如下:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" version="4.0"> <servlet> <servlet-name>register</servlet-name> <servlet-class>com.atguigudb.web.register</servlet-class> </servlet> <servlet-mapping> <servlet-name>register</servlet-name> <url-pattern>/register</url-pattern> </servlet-mapping> <servlet> <servlet-name>login</servlet-name> <servlet-class>com.atguigudb.web.login</servlet-class> </servlet> <servlet-mapping> <servlet-name>login</servlet-name> <url-pattern>/login</url-pattern> </servlet-mapping> </web-app>
妈蛋,赶紧吃饭了
request.getParameter()方法:1.获取通过http协议提交过来的数据. 通过容器的实现来取得通过get或者post方式提交过来的数据
2.request.getParameter()方法传递的数据,会从web客户端传到web服务器端,代表HTTP请求数据,该方法返回String类型的数据
request.setAttribute()和getAttribute()只是在web容器内部流转,仅仅是请求处理阶段
request.getAttribute()方法返回request范围内存在的对象
request.setAttribute() 和 getAttribute() 方法传递的数据只会存在于Web容器内部
HttpServletRequest 类有 setAttribute() 方法,而没有setParameter() 方法
一般通过表单和链接传递的参数使用getParameter