java如何将图像从servlet显示到jsp
我想做的是,我想在JSP页面上显示患者搜索的医生的图像
耐心。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();
}
}
}
}
现在我希望,无论何时患者搜索特定的医生,他都会得到带有图像的适当样式的细节。我还在检索医生的图像,但我无法显示它。如果你有任何其他想法,除了这个,我在这里实施,请随时告诉我。我对一切都开放
共 (0) 个答案