0

I just opened a "project" I made when I was studying Java some years ago, and although it runs fine, I get these errors and I don't know what they mean:

warning: [options] bootstrap class path not set in conjunction with -source 1.5 Note: D:***\src\java\reportes\ServletRLineaMunieca.java uses or overrides a deprecated API.

Note: Recompile with -Xlint:deprecation for details.

Note: D:***\src\java\reportes\ServletRLineaMunieca.java uses unchecked or unsafe operations.

Note: Recompile with -Xlint:unchecked for details.

1 warning


This web project includes many files, but I will only paste the one that gives the error:

package reportes;

import com.sql.ConectaDb;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.util.HashMap;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.JasperRunManager;
import net.sf.jasperreports.engine.design.JasperDesign;
import net.sf.jasperreports.engine.xml.JRXmlLoader;

public class ServletRLineaMunieca extends HttpServlet {

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

    try {
        HttpServletRequestWrapper srw =
                new HttpServletRequestWrapper(request);
        String fpath = srw.getRealPath("") +
                "/reporte/RLineaMunieca.jrxml";

        JasperDesign jasperDesign =
                JRXmlLoader.load(fpath);
        JasperReport jasperReport =
                JasperCompileManager.compileReport(
                jasperDesign);

        HashMap jasperParameter = new HashMap();
        jasperParameter.put("logo", "http://localhost:8084/tarea/images/logo.jpg");

        Connection cn = new ConectaDb().getConnection();
        byte[] bytes =
                JasperRunManager.runReportToPdf(
                jasperReport, jasperParameter, cn);
        cn.close();

        response.setContentType("application/pdf");
        response.setContentLength(bytes.length);
        ServletOutputStream out =
                response.getOutputStream();
        out.write(bytes, 0, bytes.length);
        out.flush();
        out.close();

    } catch (Exception e) {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<html>");
        out.println("<head><title>Reportes</title></head>");
        out.println("<body>");
        out.println("<pre>");
        e.printStackTrace(out);
        out.println("</pre>");
        out.println("</body>");
        out.println("</html>");
    }



} 

// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/** 
 * Handles the HTTP <code>GET</code> method.
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
    processRequest(request, response);
} 

/** 
 * Handles the HTTP <code>POST</code> method.
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
    processRequest(request, response);
}

/** 
 * Returns a short description of the servlet.
 * @return a String containing servlet description
 */
@Override
public String getServletInfo() {
    return "Short description";
}// </editor-fold>

}

Since I made this a long time ago I have forgotten a lot about Java. Please bear with me, this is also my first time posting here, if my code is pasted wrong please show me how to do it properly.

i forgot to add that this part:

String fpath = srw.getRealPath("") +

the getrealpath is strikeout (is that how you say it?)

4

3 回答 3

1

我已经解决了我的问题,我改变了它:

String fpath = srw.getRealPath("") +

String fpath = srw.getSession().getServletContext().getRealPath("") +

HashMap jasperParameter = new HashMap();

HashMap<String, String> jasperParameter;
jasperParameter = new HashMap<>(); 

谢谢大家:)

于 2013-04-03T13:39:32.147 回答
0

从 Java 5 开始,如果您使用没有类型说明符的集合(例如,HashMap() 而不是 HashMap())。这意味着编译器无法使用泛型检查您是否以类型安全的方式使用集合。

要消除警告,只需具体说明您在集合中存储的对象类型。所以,而不是

HashMap jasperParameter = new HashMap();

利用

HashMap<String, String> jasperParameter = HashMap<String, String>();
于 2013-04-03T01:27:03.023 回答
0

当 Oracle 的好心人因为存在问题或厌倦了更新它而将类或方法退休时,他们会将其标记为 @Deprecated。您正在使用的一种方法是这样标记的。

javac -Xlint:unchecked myFile.java将告诉您使用了哪种方法,此时您可以查找有关该方法的 javadoc 并找出它被淘汰的原因以及您是否关心。

你可能不会。例如,如果一个计时器方法因为对夏令时处理不当而被淘汰,那么您的应用程序可能不会以这种缺陷很重要的方式使用它。

在这种情况下,您没有为您的HashMap 检查下面的链接添加类型检查以了解泛型如何工作。值得学习,即使它不会改变这个程序的行为。

http://docs.oracle.com/javase/tutorial/java/generics/

于 2013-04-03T01:29:57.913 回答