Я пытаюсь вызвать класс сервлетов, используя URL, но он не вызывается.
метод init класса сервлетов выполняется, но не получает метод.Ниже приведен пример кода класса сервлетов.
Я хочу вызывать класс сервлетов по URL-адресу, например http://localhost:8090/myApp/downloadDoc?id=1
, при проверке запроса в сети выдается 302 ответа в виде запроса
URL: http://localhost:8090/myApp/downloadDoc
Request Method: GET
Status Code: 302
Remote Address: [::1]:8090
Referrer Policy: no-referrer-when-downgrade
@WebServlet("/downloadDoc")
public class FileDownloadServlet extends HttpServlet{
/**
*
*/
private static final long serialVersionUID = 1L;
//@Inject
private FileUploadService fileUploadService;
public void init(ServletConfig config) throws ServletException{
System.out.println("==================test============");
}
//@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
System.out.println("==================do get============");
String id = request.getParameter("id");
// Find this file id in database to get file name, and file type
FileDetails fileDetails = fileUploadService.findFileById(Integer.parseInt(id));
ServletOutputStream out = response.getOutputStream();
ServletContext context = getServletConfig().
getServletContext();
// String mimetype = context.getMimeType(fileDetails.getFilePath());
if(fileDetails!= null) {
// You must tell the browser the file type you are going to send
// for example application/pdf, text/plain, text/html, image/jpg
response.setContentType(fileDetails.getFileType());
// Make sure to show the download dialog
response.setHeader("Content-disposition","attachment; filename=\""+fileDetails.getFileName()+"\"");
// Assume file name is retrieved from database
// For example D:\\file\\test.pdf
File my_file = new File(fileDetails.getFilePath());
// This should send the file to browser
// OutputStream out = response.getOutputStream();
FileInputStream in = new FileInputStream(my_file);
byte[] buffer = new byte[4096];
int length;
while ((length = in.read(buffer)) > 0){
out.write(buffer, 0, length);
}
in.close();
out.flush();
}
}
// @Override
public void service(ServletRequest req, ServletResponse resp) throws ServletException, IOException {
System.out.println("====================service");
}
//@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("======================doPost");
}
// @Override
public void destroy() {
System.out.println("==================Destroy servlet");
}
}