如何在if条件下加载默认图片?

时间:2019-06-21 02:00:07

标签: java servlets

我有JSP Servlet个Web应用程序。它具有ViewUserServlet.java文件。看是否有条件。有时img2img3为空。在那时img2img3等于img。我想加载“网络内容”默认的img或另一个文件路径图像,而没有img2img3等于img。请帮我怎么做。

try {
        // This will load the MySQL driver, each DB has its own driver
        Class.forName("com.mysql.jdbc.Driver");

        // Setup the connection with the DB
        Connection connection = DriverManager.getConnection(Settings.connectionString);

        // Statements allow to issue SQL queries to the database
        Statement statement = connection.createStatement();

        // set query
        String query = "SELECT * FROM signup WHERE user_id = '"+userId+"'";

        // get result set
        ResultSet rs = statement.executeQuery(query);
        if(rs.next()) {
            //found
            // decode images
            java.sql.Blob img = rs.getBlob("image");
            byte[] imgData = img.getBytes(1,(int)img.length());
            byte[] encodeBase64 = Base64.encodeBase64(imgData);
            String base64Encoded = new String(encodeBase64, "UTF-8");

            java.sql.Blob img2 = rs.getBlob("nic");

            if(img2==null){img2 = rs.getBlob("image");
            }

            byte[] imgData2 = img2.getBytes(1,(int)img2.length());
            byte[] encodeBase64_2 = Base64.encodeBase64(imgData2);
            String base64Encoded2 = new String(encodeBase64_2, "UTF-8");


            java.sql.Blob img3 = rs.getBlob("certificate");

            if(img3==null){img3 = rs.getBlob("image");
            }

            byte[] imgData3 = img3.getBytes(1,(int)img3.length());
            byte[] encodeBase64_3 = Base64.encodeBase64(imgData3);
            String base64Encoded3 = new String(encodeBase64_3, "UTF-8");

            // get user data
            UserModel user = new UserModel(rs.getString("user_id"), rs.getString("first_name"), rs.getString("last_name"), rs.getString("email"),
                    rs.getString("user_name"), base64Encoded, rs.getString("address"), rs.getString("phone"), 
                    rs.getString("location"), rs.getString("company_name"), rs.getString("type"), base64Encoded2, base64Encoded3);

            // store user data and redirect to its related home page according to its type
            if(user.getType().equals("customer")){
                RequestDispatcher RequetsDispatcherObj =request.getRequestDispatcher("/view_user_customer.jsp");
                request.getSession().setAttribute("customer",user);
                RequetsDispatcherObj.forward(request, response);

            }else if(user.getType().equals("seller")){
                RequestDispatcher RequetsDispatcherObj =request.getRequestDispatcher("/view_user_seller.jsp");
                request.getSession().setAttribute("seller",user);
                RequetsDispatcherObj.forward(request, response);

            }
        }
        else{
            //not found
            PrintWriter out = response.getWriter();
            out.println("<script type=\"text/javascript\">");
            out.println("alert('Invalid Credentials!');");
            out.println("location='login.jsp';");
            out.println("</script>");
        }

        statement.close();
        connection.close();

    } catch (Exception e) {
        e.printStackTrace();
    } 

0 个答案:

没有答案