gpt4 book ai didi

java - 从jsp调用servlet并返回jsp

转载 作者:行者123 更新时间:2023-12-01 05:19:03 24 4
gpt4 key购买 nike

我尝试从 jsp 访问 servlet,然后如果数组列表传递为 0,则使用 Web 服务访问代码。它应该显示图像,如果传递为 1,则应该显示不同的 jsp。我能够生成代码,但它没有按预期工作。任何人都可以弄清楚问题出在哪里。

封装萨尔;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* Servlet implementation class Amberalerts
*/
public class Amberalerts extends HttpServlet {
private static final long serialVersionUID = 1L;


public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {

List list = getList();
if(list != null && list.size() != 0){
dispatchToShowJsp(req, resp, "/ShowImage.jsp");
}else{
dispatchToShowJsp(req, resp, "/ShowDefaultMsg.jsp");
}

// writeImageTOBuffer(resp);
}
private List getList() {
List<String> list = new ArrayList<String>();
list.add("Result");
return list;
}
private void dispatchToShowJsp(HttpServletRequest req,
HttpServletResponse resp, String jsplocation) throws IOException {
try {
getServletConfig().getServletContext().getRequestDispatcher(
jsplocation).forward(req,resp);
} catch (ServletException e) {

e.printStackTrace();
}
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/


private void writeImageTOBuffer(HttpServletResponse resp)
throws FileNotFoundException, IOException {
// Get the absolute path of the image
ServletContext sc = getServletContext();
String filename = sc.getRealPath("rsc/image.gif");

// Get the MIME type of the image
String mimeType = sc.getMimeType(filename);
if (mimeType == null) {
sc.log("Could not get MIME type of "+filename);
resp.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
return;
}

// Set content type
resp.setContentType(mimeType);

// Set content size
File file = new File(filename);
resp.setContentLength((int)file.length());

// Open the file and output streams
FileInputStream in = new FileInputStream(file);
OutputStream out = resp.getOutputStream();

// Copy the contents of the file to the output stream
byte[] buf = new byte[1024];
int count = 0;
while ((count = in.read(buf)) >= 0) {
out.write(buf, 0, count);
}
in.close();
out.close();
}


/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}

}

最佳答案

值得注意的是,您的代码将无法工作,因为您对 writeImageToBuffer 的调用已被注释掉,并且 doGet(req,resp) 中的 if/else 语句将始终调用 dispatchToShowJsp() 方法。

无论如何,使用 Apache Commons IO为了帮助一些繁忙的工作,您可以调用下面的sendImageToResponse方法来发送您的图像:

package com.stackoverflow.servlet;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;

public class ShowImageServlet extends HttpServlet {

public void sendImageToResponse(HttpServletResponse response) throws ServletException, IOException {

File imageFile = new File("PATH_TO_YOUR_IMAGE_FILE"); // configure this however you want

OutputStream outStream = null;
InputStream inStream = null;

try {

outStream = response.getOutputStream();

// Open image file
inStream = FileUtils.openInputStream(imageFile);

response.setContentType("image/jpeg");

System.out.println("SENDING " + imageFile);

// Send image file
IOUtils.copy(inStream, outStream);

System.out.println("SENT SIZE=" + imageFile.length());

} catch (Exception ex) {

// Handle your exception

} finally {

// close image file
IOUtils.closeQuietly(inStream);

try {

// flush stream
outStream.flush();

} catch (Exception ignore) {

// Ignore

} finally {

IOUtils.closeQuietly(outStream);
}
}
}
}

关于java - 从jsp调用servlet并返回jsp,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10714083/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com