Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
图像不会显示在任何浏览器中,而是显示在jsp页面的eclipse默认浏览器中_Jsp_Servlets - Fatal编程技术网

图像不会显示在任何浏览器中,而是显示在jsp页面的eclipse默认浏览器中

图像不会显示在任何浏览器中,而是显示在jsp页面的eclipse默认浏览器中,jsp,servlets,Jsp,Servlets,我有一个从数据库中检索图像路径的程序。程序正在eclipse浏览器中成功检索图像,但未在任何web浏览器中检索。我有3页,第一页。login.jsp 2.Index.jsp 3.LoginServlet.java ------------------------------login.jsp------------------------------------------------- <%@ page language="java" contentType="text/html; ch

我有一个从数据库中检索图像路径的程序。程序正在eclipse浏览器中成功检索图像,但未在任何web浏览器中检索。我有3页,第一页。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>

不要使用像file:///这样的绝对路径,而是在WebContent文件夹中创建一个名为“img”的文件夹,并将所有图像放在其中。所以你的文件夹应该是这样的:

 WebContent
            |_img
                |_img1.jpg
                |_img2.jpg
   <img src="${pageContext.request.contextPath}/img/<%=filename %>" name="uimage" id="uimage">
在数据库中只保存图像名称,因为您已经知道在哪里可以找到图像。因此,现在在jsp中引用时,请执行以下操作:

 WebContent
            |_img
                |_img1.jpg
                |_img2.jpg
   <img src="${pageContext.request.contextPath}/img/<%=filename %>" name="uimage" id="uimage">
PS:但您不应该从JSP执行CRUD操作,只说..: