假设当用户访问我的网站时,他会看到之前存储在数据库中的大量图像。我知道如何在JSP Scriptlet中做到这一点,并且我还知道当用户使用servlet提交表单时如何从JSTL中的数据库中获取和检索数据。但是如果没有用户提交表单,我不知道如何在JSTL中完成它。
答案 0 :(得分:6)
是的,您可以使用JSTL& EL。对于数据库访问,请使用JSTL SQL Tag library。
如何在JSP中显示存储在数据库中的图像?
我希望您使用BLOB
类型列将图像存储在数据库中。只需点击一个传递记录ID的Servlet,然后发送byte[]
作为响应。
我为每张图片创建了单独的请求,以便提供更好的用户体验。
注意:最好在Servlet中移动数据库代码。
JSP:
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<sql:setDataSource var="webappDataSource"
driver="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/test"
user="root" password="root" />
<sql:query dataSource="${webappDataSource}"
sql="select id,username from users" var="result" />
<table width="100%" border="1">
<c:forEach var="row" items="${result.rows}">
<tr>
<td>${row.id}</td>
<td>${row.username}</td>
<td>
<img src="${pageContext.servletContext.contextPath }/photoServlet?id=${row.id}" />
</td>
</tr>
</c:forEach>
</table>
Servlet(PhotoServlet.java):
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
final String DB_URL = "jdbc:mysql://localhost:3306/test";
final String User = "root";
final String Password = "root";
try {
Class.forName(JDBC_DRIVER);
Connection conn = DriverManager.getConnection(DB_URL, User, Password);
PreparedStatement stmt = conn.prepareStatement("select photo from users where id=?");
stmt.setLong(1, Long.valueOf(request.getParameter("id")));
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
response.getOutputStream().write(rs.getBytes("photo"));
}
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
的web.xml:
<servlet>
<servlet-name>PhotoServlet</servlet-name>
<servlet-class>com.server.servlet.PhotoServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>PhotoServlet</servlet-name>
<url-pattern>/photoServlet</url-pattern>
</servlet-mapping>
表结构:(用户)
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| username | varchar(30) | YES | | NULL | |
| password | varchar(20) | YES | | NULL | |
| photo | blob | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
答案 1 :(得分:0)
如果您的问题是关于使用JSTL标记在JSP页面上显示数据库内容,并且您使用的是本机样式JDBC数据库连接(不是jpa或hibernate),请查看:
// make a class for you database table mapping
// assume you have id,name,salary in employee database table , make a Java class for it
class Employee{
int id;
String name;
float salary;
// now getter setter for fields
public int getId(){
return this.id;
}
public void setId(int id){
this.id = id;
}
public String getName(){
return this.name;
}
public void setName(String name){
this.name = name;
}
public float getSalary(){
return this.salary;
}
public void setSalary(int salary){
this.salary = salary;
}
}
// now in your servlet class make a list of Employee class and set data from data base
List<Employee> empList = new ArrayList<Employee>();
// asume your db connection and query is here
ResultSet rs = your_result_Set
while(rs.next()){
Employee emp = new Employee();
emp.setId(rs.getInt("id"));
emp.setName(rs.getString("name"));
em.setSalary(rs.getFloat("slary"));
empList.add(em);
}
request.setAttribute("empList",empList);
request.getRequestDispacher("your_jsp_path").forward(request,response);
// now in your jsp add <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> on top of jsp
<table>
<c:forEach items="${empList}" var="e" >
<tr>
<td>${e.id}</td>
<td>${e.name}</td>
<td>${e.salary}</td>
</tr>
</c:forEach>
</table>
使用servlet和jsp自定义MVC。看看使用servlet 3.0或更高版本
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(name="login", urlPatterns={"/login"})
public class NewServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try{
// do your login logic here
request.setAttribute("message","login failed");
request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request, response);
}
catch(Exception exp){
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
// now in login.jsp placed in WEB-INF/jsp
<span>${message}</span><!-- message is request attribute here assigned in servlet-->
现在每当你调用登录url请求时,第一个登陆servlet后转移到jsp