这是我的包结构
1 package cn.login.service; 2 3 import cn.login.bean.User; 4 import cn.login.imp.UserImp; 5 6 public class LoginService { 7 //这是我的业务层 8 // 调用dao包中的方法 9 private UserImp dao; 10 11 public User login(User u) { 12 User existu = dao.denlu1(u.getUsername(), u.getPassword()); 13 if (existu == null) { 14 throw new RuntimeException("没有该用户"); 15 } 16 if (!existu.getPassword().equals(u.getPassword())) { 17 throw new RuntimeException("密码不正确"); 18 } 19 20 return existu; 21 22 } 23 }
package cn.login.web; import java.io.IOException; import java.io.PrintWriter; import java.lang.reflect.InvocationTargetException; 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 cn.login.bean.User; import cn.login.service.LoginService; public class LoginServlet extends HttpServlet { private LoginService dao; public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; //1.处理中文乱码这是我的web层 request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); //2.封装数据以及对数据进行强转 User u = new User(); try { BeanUtils.populate(u, request.getParameterMap()); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } //调用service User existu = null; //调用dao包中的方法 try { existu =dao.login(u); } catch (Exception e) { e.printStackTrace(); request.setAttribute("error", e.getMessage()); request.getRequestDispatcher("/index.jsp").forward(request, response); return; } // 4.向session中加入登录标识 request.getSession().setAttribute("user", existu); } }
JDBC操作没错可是Beanutils数据好像没封装进去求大佬解答!
报空指针异常
这是我的包结构
1 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9 <head> 10 <base href="<%=basePath%>"> 11 12 <title>My JSP 'index.jsp' starting page</title> 13 <meta http-equiv="pragma" content="no-cache"> 14 <meta http-equiv="cache-control" content="no-cache"> 15 <meta http-equiv="expires" content="0"> 16 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> 17 <meta http-equiv="description" content="This is my page"> 18 <!-- 19 <link rel="stylesheet" type="text/css" href="styles.css"> 20 --> 21 </head> 22 23 <body> 24 <form action="servlet/LoginServlet" method="post"> 25 <p align="center">用 户 名: <input type="text" name="username"></p><br><font color="red" >${requestScope.errors}</font> 26 <p align="center">密 码: <input type="password" name="password"></p><br> 27 <p align="center"><input type="submit" value="登陆"></p><br> 28 </form> 29 <font color="red">${requestScope.user}</font> 30 </body> 31 </html>
这是我的登陆页面
刚好你,报错的图片。看不见!!!我把你的代码在我的机器上跑了一遍。一切正常!
我怎么就看不到你的图片呢?
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.3</version>
</dependency>这是我用的beanutils的版本
@David5201: 我是想用下这个工具写了个简单的登录功能首先是写了个 登录查询操作=>然后给业务层做一些简单的检测=>再就是给servlet调可是beanutils好像没把数据封装到bean对象中我的beanutils的版本是1.8.3 这个47行就是existu =dao.login(u);
@751638623: 呵呵,这个NullPointerExcetion 是说的dao. 你看看你的类你里面,都没有对dao实例化。
@751638623:
我的变量名字叫Ls,你把Ls 换成Dao就可以了达到你的目标了。
@David5201: 抱歉这么晚才回
User existu = dao.denlu1(u.getUsername(), u.getPassword());
existu =dao.login(u);
可能一楼说的对没分装进去
@751638623: 你的DAO在哪里初始化的?如果没有初始化,他的值是不是NULL
@751638623: 一楼回答的不正确。地方都是错误的
@David5201: loginservice里调了dao数据库操作啊我把demo发你看下吧 能帮我看看吗 谢谢
@751638623:
//调用service User existu = null; //调用dao包中的方法 try { if (dao==null){ dao =new LoginService(); } existu =dao.login(u); } catch (Exception e) { e.printStackTrace(); request.setAttribute("error", e.getMessage()); request.getRequestDispatcher("/index.jsp").forward(request, response); return; }
@751638623: 初始化dao就OK了
@David5201: 我是找你这样写的还是有错啊,错误就我回你的上一条service 里调dao操作是填参数getusername getpassword 为null
@751638623: 你把你的程序发到我的箱吧。。。把数据库与发一下。306184384@qq.com ,或者你加断点调试一下
@David5201: 谢谢问题解决了