我有JSP Servlet
个Web应用程序。它具有ViewUserServlet.java
文件。看是否有条件。有时img2
和img3
为空。在那时img2
和img3
等于img
。我想加载“网络内容”默认的img
或另一个文件路径图像,而没有img2
且img3
等于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();
}