执行Jsp servlet crud示例时出错

时间:2015-09-08 10:08:43

标签: jsp servlets

我正在使用Jsp,Servlet和Mysql执行Simple crud示例。我通过查看示例手动键入代码。当我尝试执行它时,我收到此错误。     以下是我收到的错误消息的描述。     如果您需要任何java / jsp文件进行分析,请告诉我。

HTTP Status 500 - javax.servlet.ServletException: Error instantiating servlet class com.daniel.controller.UserController


type Exception report

message javax.servlet.ServletException: Error instantiating servlet class com.daniel.controller.UserController

description The server encountered an internal error that prevented it from fulfilling this request.

exception
org.apache.jasper.JasperException: javax.servlet.ServletException: Error instantiating servlet class com.daniel.controller.UserController
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:555)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:461)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)


root cause
**javax.servlet.ServletException**: Error instantiating servlet class com.daniel.controller.UserController
    org.apache.jasper.runtime.PageContextImpl.doForward(PageContextImpl.java:742)
    org.apache.jasper.runtime.PageContextImpl.forward(PageContextImpl.java:712)
    org.apache.jsp.index_jsp._jspService(index_jsp.java:99)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:438)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)


root cause
**`java.lang.NullPointerException`**
    java.util.Properties$LineReader.readLine(Unknown Source)
    java.util.Properties.load0(Unknown Source)
    java.util.Properties.load(Unknown Source)
    com.daniel.util.DbUtil.getConnection(DbUtil.java:27)
    com.daniel.dao.UserDao.<init>(UserDao.java:20)
    com.daniel.controller.UserController.<init>(UserController.java:25)
    sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    java.lang.reflect.Constructor.newInstance(Unknown Source)
    java.lang.Class.newInstance(Unknown Source)
    org.apache.jasper.runtime.PageContextImpl.doForward(PageContextImpl.java:742)
    org.apache.jasper.runtime.PageContextImpl.forward(PageContextImpl.java:712)
    org.apache.jsp.index_jsp._jspService(index_jsp.java:99)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:438)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

添加com.daniel.controller.UserController文件

package com.daniel.controller;

import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.daniel.dao.UserDao;
import com.daniel.model.User;


public class UserController extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private static String INSERT_OR_EDIT = "/user.jsp";
    private static String LIST_USER = "/listUser.jsp";
    private UserDao dao;   

    public UserController() {
        super();
        dao = new UserDao();

    }

    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException,IOException
    {
        String forward= " ";
        String action = req.getParameter("action");
        if(action.equalsIgnoreCase("delete"))
        {
            forward=LIST_USER;
            int userid = Integer.parseInt(req.getParameter("userid"));
            dao.deleteUser(userid);
            req.setAttribute("users",dao.getAllUsers());
        }
        else if(action.equalsIgnoreCase("edit"))
        {
            forward=INSERT_OR_EDIT;
            int userid = Integer.parseInt(req.getParameter("userid"));
            User user= dao.getUserById(userid);
            req.setAttribute("users",user);
        }
        else if(action.equalsIgnoreCase("listUser"))
        {
            forward=LIST_USER;
            req.setAttribute("users", dao.getAllUsers());
        }
        else
        {
            forward=INSERT_OR_EDIT;
        }
        RequestDispatcher rd = req.getRequestDispatcher(forward);
        rd.forward(req,resp);
    }

    public void doPost(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException
    {
        User user = new User();

        user.setFirstname(req.getParameter("firstname"));
        user.setLastname(req.getParameter("lastname"));
        try {
            user.setDob(new SimpleDateFormat("MM/dd/yyyy").parse(req.getParameter("dob")));
        } catch (ParseException e) {
            e.printStackTrace();
        }
        user.setEmailid(req.getParameter("emailid"));
        String userid = req.getParameter("userid");
        if(userid == null || userid.isEmpty())
        {
            dao.addUser(user);
        }else
        {
            user.setUserid(Integer.parseInt(userid));
            dao.updateUser(user);
        }
        RequestDispatcher rd = req.getRequestDispatcher(LIST_USER);
        req.setAttribute("users",dao.getAllUsers());
        rd.forward(req,resp);
    }

}

添加com.daniel.dao.UserDao

package com.daniel.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.daniel.model.User;
import com.daniel.util.DbUtil;

public class UserDao 
{
    private Connection con;

    public UserDao()
    {
        con= DbUtil.getconnection();
    }

    public void addUser(User user)
    {
        try
        {
        PreparedStatement ps = con.prepareStatement("insert into users(firstname,lastname,dob,emailid) values(?,?,?,?)");
        ps.setString(1,user.getFirstname());
        ps.setString(2,user.getLastname());
        ps.setDate(3,new java.sql.Date(user.getDob().getTime()));
        ps.setString(4,user.getEmailid());

        ps.executeUpdate();
        }catch (SQLException e)
        {
            e.printStackTrace();
        }
    }

    public void deleteUser(int userid)
    {
        try
        {
            PreparedStatement ps = con.prepareStatement("delete from users where userid=?");
            ps.setInt(1,userid);
            ps.executeUpdate();
        }catch (SQLException e)
        {
            e.printStackTrace();
        }
    }

    public void updateUser(User user)
    {
        try
        {
            PreparedStatement ps = con.prepareStatement("update user set firstname=?,lastname=?,dob=?,emailid=?"+ "where userid=?");
            ps.setString(1,user.getFirstname());
            ps.setString(2,user.getLastname());
            ps.setDate(3,new java.sql.Date(user.getDob().getTime()));
            ps.setString(4,user.getEmailid());
            ps.setInt(5,user.getUserid());

            ps.executeUpdate();
        }catch (SQLException e)
        {
            e.printStackTrace();
        }
    }

    public List<User> getAllUsers()
    {
        List<User> users = new ArrayList<User>();
        try
        {
            Statement st = con.createStatement();
            ResultSet rs = st.executeQuery("select * from users");
            while(rs.next())
            {
                User user = new User();
                user.setUserid(rs.getInt("userid"));
                user.setFirstname(rs.getString("firstname"));
                user.setLastname(rs.getString("lastname"));
                user.setDob(rs.getDate("dob"));
                user.setEmailid(rs.getString("emailid"));
                users.add(user);
            }
        }catch (SQLException e)
        {
            e.printStackTrace();
        }
        return users;
    }   

        public User getUserById(int userid)
        {
            User user = new User();
            try
            {
                PreparedStatement ps = con.prepareStatement("select * from users where userid=?");
                ResultSet rs = ps.executeQuery();
                if(rs.next())
                {

                    user.setUserid(rs.getInt("userid"));
                    user.setFirstname(rs.getString("firstname"));
                    user.setLastname(rs.getString("lastname"));
                    user.setDob(rs.getDate("dob"));
                    user.setEmailid(rs.getString("emailid"));

                }
            }catch (SQLException e)
            {
                e.printStackTrace();
            }
            return user;
    }
}

添加com.daniel.util.DbUtil

package com.daniel.util;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class DbUtil 
{
    private static Connection con=null;

    public static Connection getconnection()
    {
    if(con!=null)
    {
        return con;
    }
    else
    {
        try
        {
            Properties prop = new Properties();
            InputStream ip = DbUtil.class.getClassLoader().getResourceAsStream("/db.properties");
            prop.load(ip);
            String driver = prop.getProperty("driver");
            String url = prop.getProperty("url");
            String user = prop.getProperty("user");
            String pwd = prop.getProperty("password");

            Class.forName(driver);

            con = DriverManager.getConnection(url, user, pwd);
        }
        catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return con;
    }
}

1 个答案:

答案 0 :(得分:0)

在DbUtil类上尝试在下面的getResourceAsStream方法中删除此/,并确保您的文件db.properties位于您的src目录中。

InputStream ip = DbUtil.class.getClassLoader().getResourceAsStream("db.properties");