JAVAWEBSMBMS 期末大作业
Posted ꧁༺空༒白༻꧂
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JAVAWEBSMBMS 期末大作业相关的知识,希望对你有一定的参考价值。
SMBMS javaweb期末大作业
1、项目部署准备
-
创建一个新的maven,选中maven的webapp,然后创建
-
新建项目结构
-
连接数据库并且创建实体类
-
配置pom.xml文件信息
<dependencies> <dependency> <groupId>javax.servlet</groupId> <artifactId>servlet-api</artifactId> <version>2.5</version> </dependency> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>javax.servlet.jsp-api</artifactId> <version>2.3.3</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.47</version> </dependency> <dependency> <groupId>javax.servlet.jsp.jstl</groupId> <artifactId>jstl-api</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>1.1.2</version> </dependency> </dependencies>
-
配置数据库链接驱动db.properties
driver=com.mysql.jdbc.Driver url=jdbc:mysql://localhost:3306/smbms?useUnicode=true&characterEncoding=utf-8 user=root password=root
-
写一个BaseDao来获取数据库的连接信息
public class BaseDao { private static String driver; private static String url; private static String user; private static String password; //静态代码块,类加载的时候就初始化了 static { //通过类加载读取对应资源 Properties properties = new Properties(); InputStream is = BaseDao.class.getClassLoader().getResourceAsStream("db.properties"); try{ properties.load(is); } catch (IOException e) { e.printStackTrace(); } driver=properties.getProperty("driver"); url=properties.getProperty("url"); user=properties.getProperty("user"); password=properties.getProperty("password"); } //获取数据库的连接 public static Connection getConnection(){ Connection connection=null; try{ Class.forName(driver); connection= DriverManager.getConnection(url,user,password); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } return connection; } //编写查询的公共类 public static ResultSet execute(Connection connection, String sql, Object[] params, ResultSet resultSet, PreparedStatement preparedStatement) throws SQLException { //预编译sql,在后面直接执行即可 preparedStatement = connection.prepareStatement(sql); for (int i = 0; i < params.length; i++) { preparedStatement.setObject(i + 1, params[i]);//占位符是从1开始的 } resultSet = preparedStatement.executeQuery(sql); return resultSet; } public static int execute(Connection connection, String sql, Object[] params, PreparedStatement preparedStatement) throws SQLException { //增删改查公共方法 preparedStatement = connection.prepareStatement(sql); for (int i = 0; i < params.length; i++) { preparedStatement.setObject(i + 1, params[i]);//占位符是从1开始的 } int updateRows=preparedStatement.executeUpdate(); return updateRows; } //释放资源 public static boolean closeResources(Connection connection, ResultSet resultSet, PreparedStatement preparedStatement){ boolean flag=true; //回收资源 if(resultSet!=null){ try{ resultSet.close(); } catch (SQLException e) { e.printStackTrace(); flag=false; } } if(connection!=null){ try{ connection.close(); } catch (SQLException e) { e.printStackTrace(); flag=false; } } if(preparedStatement!=null){ try{ preparedStatement.close(); } catch (SQLException e) { e.printStackTrace(); flag=false; } } return flag; }
-
过滤器characterEndingFile
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); chain.doFilter(request,response); }
-
配置web.xml文件
<!-- 字符编码过滤器--> <filter> <filter-name>characterEndingFile</filter-name> <filter-class>com.kong.filter.characterEndingFile</filter-class> </filter> <filter-mapping> <filter-name>characterEndingFile</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
2、登录实现
1、控制层user
-
UserDao
public interface UserDao { //得到需要的用户 public User getLoginUser(Connection connection,String userCode) throws SQLException; }
-
UserDaoImpl
ublic class UserDaoImpl implements UserDao { public User getLoginUser(Connection connection, String userCode) throws SQLException { PreparedStatement pstm=null; ResultSet rs=null; User user=null; if(connection!=null) { String sql = "select * from smbms_user where userCode=?"; Object[] params = {userCode}; rs = BaseDao.execute(connection, pstm, rs, sql, params); if (rs.next()) { user = new User(); user.setId(rs.getInt("id")); user.setUserCode(rs.getString("userCode")); user.setUserName(rs.getString("userName")); user.setUserPassword(rs.getString("userPassword")); user.setGender(rs.getInt("gender")); user.setBirthday(rs.getDate("birthday")); user.setPhone(rs.getString("phone")); user.setAddress(rs.getString("address")); user.setUserRole(rs.getInt("userRole")); user.setCreatedBy(rs.getInt("createdBy")); user.setCreationDate(rs.getTimestamp("creationDate")); user.setModifyBy(rs.getInt("modifyBy")); user.setModifyDate(rs.getTimestamp("modifyDate")); } BaseDao.closeResources(null, pstm, rs); } return user; }
2、服务层service
-
UserService
public interface UserService { public User login(String userCode,String password); }
-
UserServiceImpl
public class UserServiceImpl implements UserService { //业务层会调用dao层,引入dao层操作 private UserDao userDao; public UserServiceImpl() { userDao = new UserDaoImpl(); } public User login(String userCode, String userPassword) { Connection connection = null; User user = null; try { connection = BaseDao.getConnection(); //通过业务层调用对应的具体数据库操作 user = userDao.getLoginUser(connection,userCode); } catch (SQLException e) { e.printStackTrace(); }finally { BaseDao.closeResources(connection,null,null); } return user; } @Test public void test(){ UserServiceImpl userService=new UserServiceImpl(); User admin = userService.login("admin", "123456"); System.out.println(admin.getUserPassword()); }
3、servlet层
- Loginservlet
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("你好");
//获取用户和密码
String userCode=req.getParameter("userCode");
String userPassword=req.getParameter("userPassword");
//和数据库中的密码进行对比,调用业务层
UserService userService=new UserServiceImpl();
User user=userService.login(userCode,userPassword);//把登录的人查出来
if(user!=null){
//查有此人,可以登录
req.getSession().setAttribute(Constants.USER_SESSION,user);
resp.sendRedirect("jsp/frame.jsp");
}else{
req.setAttribute("error","用户名或者密码不正确");
//失败转发回登录页面,并且获取error消息
req.getRequestDispatcher("login.jsp").forward(req,resp);
}
}
4、写一个工具类放在util方便修改
-
Constants
public class Constants { public final static String USER_SESSION="userSession"; }
5、配置web.xml
<servlet>
<servlet-name>LoginServlet</servlet-name>
<servlet-class>com.kong.servlet.user.Loginservlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/login.do</url-pattern>
重定向(redirect)和请求转发(getRequestDispatcher)的区别
- 重定向:两次请求两次响应
请求转发:只有一次
-
重定向:需要给出响应的URL,也就是包含的项目名
请求转发:只需要给出转发的Servlet路径
-
重定向:显示后一个请求的地址,属于客户端行为,重新发送了请求,整个过程不在同一个请求中,因此会跳转
请求转发:地址不会发生变化,属于服务端的行为,整个过程是同一次请求
-
重定向:到了Servlet,再到其他项目
请求转发:只能是本项目其他Servlet
3、注销及权限过滤
注销功能:删除session,返回登录页面
- LoginOut
public class LoginOut extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//删除session
req.getSession().removeAttribute(Constants.USER_SESSION);
resp.sendRedirect(req.getContextPath()+"/login.jsp");
}
- 登录拦截实现
根据判断session是否为空,来做拦截的判断
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletRequest request1 = (HttpServletRequest) request;
HttpServletResponse response1 = (HttpServletResponse) response;
//过滤器,获取session用户
User user=(User) request1.getSession().getAttribute(Constants.USER_SESSION);
if(user==null){//已经移除或者注销,或者未登录
response1.sendRedirect("/error.jsp");
}else {
chain.doFilter(request,response);
}
}
注意每一写一步都要配置web.xml
<!-- 登录拦截-->
<filter>
<filter-name>loginFilter</filter-name>
<filter-class>com.kong.filter.loginFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>loginFilter</filter-name>
<url-pattern>/jsp/*</url-pattern>
</filter-mapping>
<!-- 注销-->
<servlet>
<servlet-name>LoginOut</servlet-name>
<servlet-class>com.kong.servlet.user.LoginOut</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginOut</servlet-name>
<url-pattern>/jsp/logout.do</url-pattern>
</servlet-mapping>