如何在JSP页面显示mysql数据库内容
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在JSP页面显示mysql数据库内容相关的知识,希望对你有一定的参考价值。
下面是比较传统的办法,一步一步的~首先,在servlet中通过jdbc把数据库里的内容查出来放到List里
然后,把List放到request的attribute中
最后,在jsp页面中用jstl标签+EL取出
要是不明白的话就去快快去补习一下吧 参考技术A 把它查出来,存在一个对象中,再显示出来。
如何在jsp页面上显示来自mysql数据库的图像? [复制]
【中文标题】如何在jsp页面上显示来自mysql数据库的图像? [复制]【英文标题】:how to display an image from mysql database on a jsp page? [duplicate] 【发布时间】:2013-03-27 14:23:00 【问题描述】:我想显示存储在 MySQL 数据库中的图像。为了检索图像,我使用了一个 servlet。请告诉我如何在jsp页面中显示它。这是流程:
login.html ---> dologin.jsp
login.html 和 servlet 几乎准备就绪。请告诉我如何完成 dologin.jsp。
登录.html
<html>
<head>
<style type="text/css">
html, body, div, h1, h2, h3, h4, h5, h6, p, img, dl,
dt, dd, ol, ul, li, table, tr, td, form, object, embed,
article, aside, command, details, fieldset,
figcaption, figure, footer, group, header, hgroup, legend
margin: 0;
padding: 0;
border: 0;
html
font: 82.5% verdana, helvetica, sans-serif;
background: #fff;
color: #333;
line-height: 1;
direction: ltr;
html, body
position: absolute;
height: 100%;
min-width: 100%;
table
border-collapse: collapse;
border-spacing: 0;
.signin-header
padding: 10px 12px 5px;
background:#00AAFF;
border: 1px solid #e5e5e5;
width: 362px;
float: right;
.signin-box
padding: 20px 25px 15px;
background:#DEDEDE;
border: 1px solid #e5e5e5;
width: 335px;
float: right;
.button
min-width: 46px;
text-align: center;
color: #444;
font-size: 11px;
font-weight: bold;
height: 27px;
padding: 0 8px;
line-height: 27px;
border-radius: 2px;
transition: all 0.218s;
border: 1px solid #dcdcdc;
background-color: #f5f5f5;
cursor: default;
*+html .button
min-width: 70px;
button.button,
input[type=submit].button
height: f1f1f1px;
line-height: 29px;
vertical-align: bottom;
margin: 0;
.button:hover
border: 1px solid #c6c6c6;
color: #333;
text-decoration: none;
transition: all 0.0s;
background-color: #f8f8f8;
box-shadow: 0 1px 1px rgba(0,0,0,0.1);
.button:active
background-color: #f6f6f6;
box-shadow: inset 0 1px 2px rgba(0,0,0,0.1);
.button:visited
color: #666;
.button-submit
border: 1px solid #3079ed;
color: #fff;
text-shadow: 0 1px rgba(0,0,0,0.1);
background-color: #4d90fe;
.button-submit:hover
border: 1px solid #2f5bb7;
color: #fff;
text-shadow: 0 1px rgba(0,0,0,0.3);
background-color: #357ae8;
button-submit:active
background-color: #357ae8;
box-shadow: inset 0 1px 2px rgba(0,0,0,0.3);
.footer-bar
position: absolute;
bottom: 0;
height: 35px;
width: 100%;
border-top: 1px solid #ebebeb;
overflow: hidden;
.footer
padding-top: 9px;
font-size: .85em;
white-space: nowrap;
line-height: 0;
.footer ul
color: #999;
float: left;
max-width: 80%;
.footer ul li
display: inline;
padding: 0 1.5em 0 0;
.footer a
color: #333;
.footer .lang-chooser-wrap
float: right;
max-width: 20%;
.footer .lang-chooser-wrap img
vertical-align: middle;
.footer .attribution
float: right;
.footer .attribution span
vertical-align: text-top;
.content
padding: 0 44px;
.table
padding: 0 55px
</style>
<script type="text/javascript">
function ccheck()
uid=document.f1.uid.value;
cpass=document.f1.cpass.value;
if(uid=="" || uid==null)
alert("Plz. Enter Your User ID");
document.f1.uid.focus();
return false;
if(cpass=="" || cpass==null)
alert("Plz. Enter Your Password");
document.f1.cpass.focus();
return false;
return true;
</script>
<title>Login Page</title>
</head>
<body onload="document.f1.uid.focus()" style="background-image:url('medical.jpg');background-position: center">
<form id="f1" name="f1" action="doDelete.jsp" method="get" onsubmit="return ccheck()">
<img src="header.png"><br><br><br><br><br>
<table><tr><td>
<table class="table"><tr><td>
<table class="table"><tr><td>
<div class="content"><div class="signin-header"><h3>Welcome to DiaEmr</h3></div></div>
</td></tr>
<tr><td>
<div class="content">
<div class="signin-box">
<p class="one">
Welcome to all at the <b>"Workshop on Ileal Interposition".</b><br>
<b>Brazil</b> to inaugurate & launch this very important Data Registry<br>
Key features of the solution-<br>
</div>
</div>
</td></tr></table>
</td>
<td>image</td>
<td>
<table class="table"><tr><td>
<div class="content">
<div class="signin-header">
<h3>Portal Login</h3>
</div>
</div>
</td></tr>
<tr><td>
<div class="content">
<div align="center" class="signin-box">
<table class="table"><tr><td>
<b>User ID</b></td>
<td><input name="uid" type="text" /></td>
</tr>
<tr><td>
<b>Password</b></td>
<td><input name="cpass" type="password" /></td>
</tr>
<tr>
<td><input type="submit" class="button button-submit" value="Submit" /></td>
<td><input type="reset" class="button button-submit" value="Reset" /></td>
<tr>New User<a href ="UserRegistration.jsp">Register</a></tr><br>
</tr></table>
</div>
</div>
</td></tr></table>
</td></tr></table>
</td></tr></table>
<div class="footer-bar">
<img align="left" src="footer.png">
</div>
</form>
</body>
</html>
小服务程序
import java.sql.*;
import DB.DataBaseConnection;
import java.io.IOException;
import java.io.InputStream;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class imagetestServlet
*/
@WebServlet("/imagetestServlet")
public class imagetestServlet extends HttpServlet
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public imagetestServlet()
super();
// TODO Auto-generated constructor stub
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException,ServletException
Blob image = null;
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
DataBaseConnection db= new DataBaseConnection();
ServletOutputStream out = response.getOutputStream();
try
Class.forName("com.mysql.jdbc.Driver");
con=db.connet();
stmt = con.createStatement();
rs = stmt.executeQuery("select img from one where id = '4'");
if (rs.next())
image = rs.getBlob(1);
else
response.setContentType("text/html");
out.println("<font color='red'>image not found for given id</font>");
return;
response.setContentType("image/gif");
InputStream in = image.getBinaryStream();
int length = (int) image.length();
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
while ((length = in.read(buffer)) != -1)
out.write(buffer, 0, length);
in.close();
out.flush();
catch (Exception e)
response.setContentType("text/html");
out.println("<html><head><title>Unable To Display image</title></head>");
out.println("<body><h4><font color='red'>Image Display Error=" + e.getMessage() +
"</font></h4></body></html>");
return;
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
// TODO Auto-generated method stub
【问题讨论】:
你有没有尝试自己实现一些东西? @Funtik 我试过这种方式 渡//EN" "w3.org/TR/html4/loose.dtd">你为什么不这样尝试:
我有一个 getImageDetails.jsp 来根据 user_id 或 name 或其他一些约束从数据库中检索图像数据,如下所示(将代码放在 jsp/servlet 页面中)
int img_id = Integer.parseInt(request.getParameter("your_id"));
Connection con = //get your connection object here ;
ResultSet rs = null;
PreparedStatement pstmt = null;
OutputStream oImage;
try
pstmt = con.prepareStatement("your sql statement for retriving the image column data");
pstmt.setInt(1, img_id);
rs = pstmt.executeQuery();
if(rs.next())
byte barray[] = rs.getBytes(1);
response.setContentType("image/gif");
oImage=response.getOutputStream();
oImage.write(barray);
oImage.flush();
oImage.close();
catch(Exception ex)
//ex.printStackTrace();
finally
try
if(con!=null)
con.close();
catch(Exception ex)
// ex.printStackTrace();
然后我从 html 或任何其他 jsp 页面调用此页面,如下所示
请尝试这样你会得到答案
让我知道状态 快乐编码
【讨论】:
它抛出 java.nullpointer 异常。好吧,我正在检查它 你能把总错误信息发给我吗,这样我会告诉你问题出在哪里,还有一件事是你用你的数据库详细信息更新了代码检查一次 您的 帮助了我,因为我使用 servlet.thanks 进行开发 不管是jsp还是servlet都不是什么大问题。您可以将代码复制到 servlet。我想你知道这个概念是对的。尝试将其更改为servlet。如果您有任何问题,请告诉我 我有一个表单,其中包含姓名、年龄等字段和一个获取图像的字段。我有 2 个表,一个用于存储详细信息,另一个用于存储图像。表单操作调用一个 servlet,它存储除图像之外的详细信息。我设计了一个将图像存储到数据库中的 servlet。请告诉我如何调用这个servlet。一个提交调用2个servlet。以上是关于如何在JSP页面显示mysql数据库内容的主要内容,如果未能解决你的问题,请参考以下文章
如何将mysql数据库表中的内容显示在Web页面中,用啥软件实现呢?