JSP(JavaServer Pages)是一种基于Java技术的动态网页开发技术,它允许开发者在HTML或XML文档中嵌入Java代码片段和表达式,从而实现动态内容的生成。下面是一个简单的JSP网上相册源代码示例,包括基本的页面结构和功能。
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>我的相册</title>
</head>
<body>
<h1>我的相册</h1>
<a href="upload.jsp">上传新照片</a>
<br><br>
<%
// 假设使用JDBC连接数据库并获取图片列表
String url = "jdbc:mysql://localhost:3306/photogallery";
String user = "root";
String password = "password";
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url, user, password);
stmt = conn.createStatement();
rs = stmt.executeQuery("SELECT * FROM photos");
while (rs.next()) {
String photoPath = rs.getString("path");
String description = rs.getString("description");
%>
<img src="<%= photoPath %>" alt="<%= description %>" width="200">
<p><%= description %></p>
<%
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try { if (rs != null) rs.close(); } catch (Exception e) {}
try { if (stmt != null) stmt.close(); } catch (Exception e) {}
try { if (conn != null) conn.close(); } catch (Exception e) {}
}
%>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>上传照片</title>
</head>
<body>
<h1>上传新照片</h1>
<form action="uploadProcess.jsp" method="post" enctype="multipart/form-data">
照片描述: <input type="text" name="description"><br><br>
选择文件: <input type="file" name="photo"><br><br>
<input type="submit" value="上传">
</form>
</body>
</html>
<%@ page import="java.io.*, java.util.*, javax.servlet.http.*, javax.servlet.*" %>
<%@ page import="org.apache.commons.fileupload.*, org.apache.commons.fileupload.disk.*, org.apache.commons.fileupload.servlet.*" %>
<%@ page import="org.apache.commons.io.output.*" %>
<%
boolean isMultipart = ServletFileUpload.isMultipartContent(request);
if (!isMultipart) {
out.println("表单必须包含 enctype=multipart/form-data");
return;
}
DiskFileItemFactory factory = new DiskFileItemFactory();
ServletFileUpload upload = new ServletFileUpload(factory);
List<FileItem> items = upload.parseRequest(request);
String description = "";
FileItem photoItem = null;
for (FileItem item : items) {
if (item.isFormField()) {
if (item.getFieldName().equals("description")) {
description = item.getString();
}
} else {
photoItem = item;
}
}
if (photoItem != null) {
String fileName = photoItem.getName();
String filePath = application.getRealPath("/") + "photos/" + fileName;
File uploadedFile = new File(filePath);
photoItem.write(uploadedFile);
// 将文件路径和描述保存到数据库
String url = "jdbc:mysql://localhost:3306/photogallery";
String user = "root";
String password = "password";
Connection conn = null;
PreparedStatement pstmt = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(url, user, password);
pstmt = conn.prepareStatement("INSERT INTO photos (path, description) VALUES (?, ?)");
pstmt.setString(1, "photos/" + fileName);
pstmt.setString(2, description);
pstmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
try { if (pstmt != null) pstmt.close(); } catch (Exception e) {}
try { if (conn != null) conn.close(); } catch (Exception e) {}
}
response.sendRedirect("index.jsp");
}
%>
通过以上代码和说明,你可以构建一个基本的JSP网上相册系统。如果有更多具体问题或需要进一步的优化建议,请提供详细信息。
领取专属 10元无门槛券
手把手带您无忧上云