从数据库JSP获取数据

时间:2014-04-30 10:45:51

标签: java jsp servlets

你好我想使用servlet从数据库中获取数据到我的JSP页面。因此,当我运行我的代码并登录时,JSP页面为空。这是代码

users.java 豆类

public class users {

    private String name;
    private String lastname;
    private String username;
    private String password;
    private Date birth_date;
    private String email;



    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getLastname() {
        return lastname;
    }

    public void setLastname(String lastname) {
        this.lastname = lastname;
    }

    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 Date getBirth_date() {
        return birth_date;
    }

    public void setBirth_date(Date birth_date) {
        this.birth_date = birth_date;
    }

    public String getEmail() {
        return email;
    }

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

DBUtils.java

public abstract class DBUtils {

    public abstract Connection getConnection() throws SQLException, NamingException;

    protected DataSource getDataSource(String jndi_name) throws NamingException{
    Context initContext = null;
        DataSource ds = null;
        try {

            initContext = new InitialContext();
            Context envContext = (Context) initContext.lookup("java:/comp/env");
            ds = (DataSource) envContext.lookup(jndi_name);

        } finally {
            if (initContext != null) {
                try {
                    initContext.close();
                }finally {
                    initContext = null;
                }
            }
        }
        return ds;
    }

    public void closeConnection(Connection conn){
        try{
            conn.close();
        }catch(Exception exp){}
    }
}

DerbyUtils.java

    public class DerbyUtils extends DBUtils{

        private static final String JNDI_NAME_DERBY = "jdbc/services";


        @Override
        public Connection getConnection() throws SQLException, NamingException {

            return super.getDataSource(JNDI_NAME_DERBY).getConnection();

        }
    }

UserInfo.java servlet

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = null;
        Connection conn = null;
        DBUtils dbUtils = null;
        try{
            out = response.getWriter();     
            String username = null;
            String password = null;

            //setting cookies for session
            Cookie[] cookies = request.getCookies();
            if (cookies != null) {
                for (Cookie cookie : cookies) {
                if(cookie.getName().equals("username"))
                    username = cookie.getValue();
            }
            }


            dbUtils = new DerbyUtils();
            conn = dbUtils.getConnection();
            UserInfoDAO dao = new UserInfoDAO(conn);

            dao.userInfo(username);


            request.setAttribute("username", username);
            RequestDispatcher dispatcher =
            getServletContext().getRequestDispatcher("/userinfo.jsp");
            dispatcher.forward(request, response);
        }

UserInfoDAO.java

public class UserInfoDAO {
   private Connection conn;

   public UserInfoDAO(Connection conn){
       conn = this.conn;

   }

   users info = new users();

    public void userInfo(String username) throws SQLException, ParseException{
            ResultSet rset = null;
        try {

            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

            String sql = "select *from users where username like '"+username+"'";

            PreparedStatement ps = this.conn.prepareStatement(sql);

            rset = ps.executeQuery();

            while(rset.next()){

            info.setName(rset.getString("name"));
            info.setLastname(rset.getString("lastname"));
            info.setUsername(rset.getString("username"));
            info.setPassword(rset.getString("password"));
            info.setBirth_date(format.parse(rset.getString("birth_date")));
            info.setEmail(rset.getString("email"));

            }
        }finally{
            if(rset != null){
                rset.close();
            }
        }

    }

userinfo.jsp

<%@page contentType="text/html" pageEncoding="UTF-8" isELIgnored="false"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>

        <h1>Hello ${username}</h1>
    </body>
</html>

1 个答案:

答案 0 :(得分:0)

您在JSP中包含JSTL Core库,如下所示

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

并在应用程序li​​b文件夹中添加jstl.jar