如何显示从servlet到jsp的图像

问题描述

我想做的是,我想在JSP页面显示患者搜索过的医生的图像。

Patient.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<Meta charset="ISO-8859-1">
<title>Patient Dashboard</title>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
    <style type="text/css">
        <%@include file="/CSS/patient.css"%>
    </style>
<script>
    function searchDoc() {
        var request = new XMLHttpRequest();
        var name = document.form.docSearch.value;
        var url = "<%=request.getcontextpath()%>/PatientSearch?val=" + name;
        try {
            request.onreadystatechange = function() {
                if (request.readyState == 4 && this.status == 200) {
                    var val = request.responseText;
                    document.getElementById('mylocation').innerHTML = val;
                }
            }//end of function  
            request.open("GET",url,true);
            request.send();
        } catch (e) {
            alert("Unable to connect to server");
        }
    }
</script>
</head>
<body>

    <div id="myCarousel" class="carousel slide" data-ride="carousel">
        <!-- Indicators -->
        <ol class="carousel-indicators">
          <li data-target="#myCarousel" data-slide-to="0" class="active"></li>
          <li data-target="#myCarousel" data-slide-to="1"></li>
          <li data-target="#myCarousel" data-slide-to="2"></li>
        </ol>
      
        <!-- Wrapper for slides -->
        <div class="carousel-inner">
          <div class="item active">
            <img src="<%=request.getcontextpath() %>/IMG/offer1.jpg" alt="Los Angeles">
          </div>
      
          <div class="item">
            <img src="<%=request.getcontextpath() %>/IMG/offer2.jpg" alt="Chicago">
          </div>
      
          <div class="item">
            <img src="<%=request.getcontextpath() %>/IMG/offer3.jpg" alt="New York">
          </div>
        </div>
      
        <!-- Left and right controls -->
        <a class="left carousel-control" href="#myCarousel" data-slide="prev">
          <span class="glyphicon glyphicon-chevron-left"></span>
          <span class="sr-only">PrevIoUs</span>
        </a>
        <a class="right carousel-control" href="#myCarousel" data-slide="next">
          <span class="glyphicon glyphicon-chevron-right"></span>
          <span class="sr-only">Next</span>
        </a>
      </div>

    <%-- <h1>Patient's Dashboard</h1> --%>
    <div class="navbar">
        <div class="welcome-container">
            <p>Welcome Patient,${pFName} ${pLName}</p>
        </div>
        <img src="<%=request.getcontextpath() %>/IMG/patient.png" class="img" alt="Patient">
        <jsp:include page="/JSP/patientMenu.jsp"></jsp:include>
    </div>

    <div class="search-container">
        <form name="form" autocomplete="off">
            <input type="text" name="docSearch" placeholder="Search for any doctor by name..." style="width:30%">
            <button type="button" class="btn btn-success" onclick="searchDoc()">Search</button>
        </form>
    </div>
        <br>
        <span id="mylocation"></span>
        
    </body>
</html>

在这里一个输入字段,患者可以在这里搜索医生,并获取医生的所有详细信息,例如他的姓名,费用,经验,专业知识,学位等。因此,我在按钮上具有onclick功能,它会触发使用XMLHttpRequest()的函数。它从Servlet获取请求,并在jsp页面显示数据,而无需重新加载它。

    protected void doGet(HttpServletRequest request,HttpServletResponse response) throws servletexception,IOException {
    Connection connection = null;
    PreparedStatement ps,ps2 = null;
    ResultSet rs,rs2 = null;
    PrintWriter out=response.getWriter();
    HttpSession session=request.getSession(false);
    
    String name=request.getParameter("val");
    String sql ="select fName,lName,username,exp,fees,user_img from users where fName='"+name+"'";
    String sql2="select login_type from users where fName='"+name+"'";
    String login_type="";
    if(name==null||name.trim().equals(""))
        out.print("<p>Please enter name!<p>");
    else {
        try{
            connection = DBConnection.createConnection();
            ps=connection.prepareStatement(sql);
            ps2=connection.prepareStatement(sql2);
            rs = ps.executeQuery();
            rs2 = ps2.executeQuery();

            while(rs2.next())
                login_type=rs2.getString(1);                    

            if(login_type.equals("D")) {
                if(!rs.isBeforeFirst())
                    out.println("<p>No Record Found! <p>");
                else {
                    while(rs.next()) {
                        response.setContentType("image/jpeg");
                            out.print("<img src="+session.getAttribute("patientSearchImage")+rs.getString("user_img")+"</img>");

                        out.print("<table border='1' cellpadding='2' width='100%'>");
                        out.print("<tr><th>First Name</th><th>Last Name</th><th>Username</th> "+ 
                                "<th>Experience</th><th>Fees</th></tr>");
                        out.print("<tr><td>"+rs.getString("fName")+"</td><td>"+rs.getString("lName")+"</td>"+
                                "<td>"+rs.getString("username")+"</td><td>"+rs.getString("exp")+"</td><td>"+rs.getString("fees"));
                        
                    }
                    out.print("</table>");
                }
                connection.close();
            }else {
                out.println("<p>No Record Found! <p>");
            }
        } catch (Exception e) {
            e.printstacktrace();
            }
        }
    }
}

现在,我希望每当患者寻找特定的医生时,他也将获得适当的带有图像样式的细节。我也正在检索医生的图像,但无法显示。如果您除了我正在此处执行的其他想法之外,请随时告诉我。我对所有事物都开放。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)