在JSP页面中显示图像和文本

时间:2017-12-17 13:35:14

标签: image jsp servlets

我正在尝试显示包含用户图像和用户信息的jsp页面。

这是JSP代码:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<%@page import="utils.MyUtils"%>
<%@ page import="java.sql.*" %> 
<%@ page import="java.io.*" %> 
<%

    Connection con = MyUtils.getStoredConnection(request);

    String strSQL = "SELECT IMAGE "
            + "FROM CUSTOMER "
            + "WHERE CUST_ID= 111";
    Statement stmt = con.createStatement();
    ResultSet rs = stmt.executeQuery(strSQL);
    rs.next();


    out.clear();
    OutputStream os = response.getOutputStream();
    String hello = "Hello";
    os.write(hello.getBytes());
    response.setHeader("expires", "0");
    response.setContentType("image/jpeg");

    os.write(rs.getBytes("IMAGE"));
    out.flush();
%> 

这是我的servlet:

package servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import beans.UserAccount;
import utils.MyUtils;

@WebServlet(urlPatterns = { "/userInfo" })
public class UserInfoServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public UserInfoServlet() {
        super();
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        HttpSession session = request.getSession();

        // Check User has logged on
        UserAccount loginedUser = MyUtils.getLoginedUser(session);

        // Not logged in
        if (loginedUser == null) {
            // Redirect to login page.
            response.sendRedirect(request.getContextPath() + "/login");
            return;
        }
        // Store info to the request attribute before forwarding.
        request.setAttribute("user", loginedUser);

        // If the user has logged in, then forward to the page
        // /WEB-INF/views/userInfoView.jsp
        RequestDispatcher dispatcher //
                = this.getServletContext().getRequestDispatcher("/WEB-INF/views/userInfoView.jsp");
        dispatcher.forward(request, response);

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doGet(request, response);
    }

}

但是当我运行我的代码时,页面只包含图像,并且它不会显示任何信息(这里我试图打印&#34; Hello&#34;在页面中作为信息)。我该怎么办?图像存储在我的电脑中。

0 个答案:

没有答案