我有一个从数据库中检索图像路径的程序。程序在eclipse浏览器中成功检索图像,但不能在任何Web浏览器中检索。我有3页1. login.jsp 2. Index.jsp 3.LoginServlet.java
------------------------------login.jsp-------------------------------------------------
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Please Login</title>
</head>
<body>
<form action="log" method="post">
<center>
<div>
<span>Email Id</span><input type="text" name="email" placeholder="Enter Email"><br>
<span>Password</span><input type="password" name="upass" placeholder="enter password"> <br>
<input type="submit" name="sub" value="Login" id="sub">
</div>
</center>
</form>
</body>
</html>
---------------------------------LoginServlet.java--------------------------------------------------------------
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.catalina.Session;
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public LoginServlet() {
super();
}
protected void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
String sessionName="sessionmail";
String getEmail=req.getParameter("email");
String getPass=req.getParameter("upass");
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","maneger");
PreparedStatement ps=con.prepareStatement("select * from final_social where uemail=? and upass=? ");
ps.setString(1, getEmail);
ps.setString(2, getPass);
ResultSet rs=ps.executeQuery();
while(rs.next()){
if(getEmail.equalsIgnoreCase(rs.getString(3)) && getPass.equals(rs.getString(2))){
System.out.println("unam and pass match");
HttpSession session=req.getSession(true);
session.setAttribute("sessionName", getEmail);
System.out.println(sessionName);
String nextJSP = "/Index.jsp";
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(nextJSP);
dispatcher.forward(req,res);
}
else{
System.out.println("not match");
}
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
----------------------------------------Index.jsp------------------------------------------------
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@page import="javax.xml.crypto.OctetStreamData"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.io.*"%>
<% String filename="";
Connection con=null;
PreparedStatement ps=null;
ResultSet rs=null;
%>
<%Class.forName("oracle.jdbc.driver.OracleDriver");
con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe","system","maneger");
//String sessionId=session.getAttribute("sessionName");
ps=con.prepareStatement("select uname,photo from final_social where uemail=?");
ps.setObject(1, session.getAttribute("sessionName"));
rs=ps.executeQuery();
%>
<!DOCTYPE html>
<html>
<head>
<title>Welcome</title>
<style>
#uimage{
width: 200px;
height: 200px;
border-radius: 120px;
}
</style>
</head>
<body>
<%while(rs.next())
{
filename=rs.getString(2);
}
%>
<img src="file:///<%=filename %>" name="uimage" id="uimage">
</body>
</html>
答案 0 :(得分:1)
不要使用像file:///<%=filename %>
这样的绝对路径,而是创建一个名为&#39; img&#39;的文件夹。在WebContent
文件夹中,将所有图片放入其中。所以你的文件夹应该是这样的:
WebContent
|_img
|_img1.jpg
|_img2.jpg
并且在您的数据库中只保存image names
,因为您已经知道在哪里可以找到图像。所以现在在jsp中引用时,请执行以下操作:
<img src="${pageContext.request.contextPath}/img/<%=filename %>" name="uimage" id="uimage">
PS:但是你不应该从你的jsps做你的CRUD操作,只是说...:)